libaudiomgr_linux.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  1. #include"libaudiomgr_linux.h"
  2. #include "core_time.h"
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #ifndef RVC_PA_ADJUST_LATENCY_PROTOCOL_VERSION
  6. #define RVC_PA_ADJUST_LATENCY_PROTOCOL_VERSION 13
  7. #endif
  8. #ifndef RVC_MAX_VOLUME
  9. #define RVC_MAX_VOLUME 65536
  10. #endif
  11. static int sample_index = 0;
  12. // From pulsecore/macro.h
  13. #define pa_memzero(x,l) (memset((x), 0, (l)))
  14. #define pa_zero(x) (pa_memzero(&(x), sizeof(x)))
  15. static uint32_t latency_ms = 20; // requested initial latency in milisec: 0 use max
  16. static pa_usec_t latency = 0; //real latency in usec (for timestamping)
  17. static int sink_index = 0;
  18. static int source_index = 0;
  19. AudioMgrImpl::AudioMgrImpl(audiomgr_callback_t* pCallback)
  20. {
  21. m_audio_context = NULL;
  22. memcpy(&m_callback, pCallback, sizeof(audiomgr_callback_t));
  23. }
  24. AudioMgrImpl::~AudioMgrImpl()
  25. {
  26. if (NULL != m_audio_context){
  27. if (NULL != m_audio_context->list_input_devices) {
  28. free(m_audio_context->list_input_devices);
  29. m_audio_context->list_input_devices = NULL;
  30. }
  31. if (NULL != m_audio_context->list_output_devices) {
  32. free(m_audio_context->list_output_devices);
  33. m_audio_context->list_output_devices = NULL;
  34. }
  35. if (NULL != m_audio_context->capture_buff) {
  36. free(m_audio_context->capture_buff);
  37. m_audio_context->capture_buff = NULL;
  38. }
  39. free(m_audio_context);
  40. m_audio_context = NULL;
  41. }
  42. }
  43. int AudioMgrImpl::audio_mgr_destroy()
  44. {
  45. delete this;
  46. return 0;
  47. }
  48. /*
  49. * init pulseaudio api
  50. * args:
  51. * audio_ctx - pointer to audio context data
  52. *
  53. * asserts:
  54. * audio_ctx is not null
  55. *
  56. * returns: error code (0 = E_OK)
  57. */
  58. int AudioMgrImpl::audio_init_pulseaudio()
  59. {
  60. /*assertions*/
  61. assert(NULL != m_audio_context);
  62. if (pa_get_devicelist() < 0)
  63. {
  64. audiolog( "pulse audio failed to get audio device list from pulse server\n");
  65. return -1;
  66. }
  67. return 0;
  68. }
  69. /*
  70. * pa_mainloop will call this function when it's ready to tell us
  71. * about a source (input).
  72. * Since we're not threading when listing devices,
  73. * there's no need for mutexes on the devicelist structure
  74. * args:
  75. * c - pointer to pulse context
  76. * l - pointer to source info
  77. * eol - end of list
  78. * data - pointer to user data (audio context)
  79. *
  80. * asserts:
  81. * none
  82. *
  83. * returns: none
  84. */
  85. static void pa_sourcelist_cb(pa_context* c, const pa_source_info* l, int eol, void* data)
  86. {
  87. rvc_audio_context_t* audio_ctx = (rvc_audio_context_t*)data;
  88. int channels = 1;
  89. /*
  90. * If eol is set to a positive number,
  91. * you're at the end of the list
  92. */
  93. if (eol > 0)
  94. return;
  95. source_index++;
  96. if (l->sample_spec.channels < 1)
  97. channels = 1;
  98. else
  99. channels = l->sample_spec.channels;
  100. double my_latency = 0.0;
  101. int verbosity = 1;
  102. if (my_latency <= 0.0)
  103. my_latency = (double)latency_ms / 1000;
  104. if (l->monitor_of_sink == PA_INVALID_INDEX)
  105. {
  106. audio_ctx->num_input_dev++;
  107. /*add device to list*/
  108. audio_ctx->list_input_devices = (rvc_audio_device_t*)realloc(audio_ctx->list_input_devices, audio_ctx->num_input_dev * sizeof(rvc_audio_device_t));
  109. if (audio_ctx->list_input_devices == NULL)
  110. {
  111. exit(-1);
  112. }
  113. /*fill device data*/
  114. audio_ctx->list_input_devices[audio_ctx->num_input_dev - 1].id = l->index; /*saves dev id*/
  115. strncpy(audio_ctx->list_input_devices[audio_ctx->num_input_dev - 1].name, l->name, MAX_PATH-1);
  116. strncpy(audio_ctx->list_input_devices[audio_ctx->num_input_dev - 1].description, l->description, MAX_PATH-1);
  117. audio_ctx->list_input_devices[audio_ctx->num_input_dev - 1].channels = channels;
  118. audio_ctx->list_input_devices[audio_ctx->num_input_dev - 1].samprate = l->sample_spec.rate;
  119. audio_ctx->list_input_devices[audio_ctx->num_input_dev - 1].low_latency = my_latency; /*in seconds*/
  120. audio_ctx->list_input_devices[audio_ctx->num_input_dev - 1].high_latency = my_latency; /*in seconds*/
  121. }
  122. }
  123. void PaSetVolumeCallback(pa_context* c,
  124. int success,
  125. void* /*pThis*/) {
  126. if (!success) {
  127. //printf("failed to set volume\n");
  128. }
  129. else {
  130. //printf("success to set volume\n");
  131. }
  132. }
  133. static void pa_setsourcevolume_cb(pa_context* c, const pa_source_info* l, int eol, void* data)
  134. {
  135. rvc_volume_set_t* audio_volume = (rvc_volume_set_t*)data;
  136. /*
  137. * If eol is set to a positive number,
  138. * you're at the end of the list
  139. */
  140. if (eol > 0)
  141. return;
  142. if (l->monitor_of_sink == PA_INVALID_INDEX)
  143. {
  144. if (strstr(l->description, audio_volume->strdevicename)) {
  145. pa_cvolume cvolumes;
  146. // Set the same volume for all channels
  147. pa_cvolume_set(&cvolumes, l->channel_map.channels, audio_volume->ivolume);
  148. pa_operation* paOperation = NULL;
  149. if (!(paOperation = pa_context_set_source_volume_by_index(c, l->index, &cvolumes, PaSetVolumeCallback, NULL))) {
  150. audio_volume->volume_flag = 1;
  151. }
  152. pa_operation_unref(paOperation);
  153. }
  154. }
  155. }
  156. static void pa_setsinkvolume_cb(pa_context* c, const pa_sink_info* l, int eol, void* data)
  157. {
  158. rvc_volume_set_t* audio_volume = (rvc_volume_set_t*)data;
  159. /*
  160. * If eol is set to a positive number,
  161. * you're at the end of the list
  162. */
  163. if (eol > 0)
  164. return;
  165. if (strstr(l->description, audio_volume->strdevicename)) {
  166. pa_cvolume cvolumes;
  167. // Set the same volume for all channels
  168. pa_cvolume_set(&cvolumes, l->channel_map.channels, audio_volume->ivolume);
  169. pa_operation* paOperation = NULL;
  170. if (!(paOperation = pa_context_set_sink_volume_by_index(c, l->index, &cvolumes, PaSetVolumeCallback, NULL))) {
  171. audio_volume->volume_flag = 1;
  172. }
  173. pa_operation_unref(paOperation);
  174. }
  175. }
  176. static void pa_sourcevolume_cb(pa_context* c, const pa_source_info* l, int eol, void* data)
  177. {
  178. rvc_audio_volume_t* audio_volume = (rvc_audio_volume_t*)data;
  179. /*
  180. * If eol is set to a positive number,
  181. * you're at the end of the list
  182. */
  183. if (eol > 0)
  184. return;
  185. if (l->monitor_of_sink == PA_INVALID_INDEX)
  186. {
  187. if (strstr(l->description, audio_volume->strdevicename)){
  188. audio_volume->cvolumes = l->volume;
  189. audio_volume->volume_flag = 1;
  190. }
  191. }
  192. }
  193. static void pa_sinkvolume_cb(pa_context* c, const pa_sink_info* l, int eol, void* userdata)
  194. {
  195. rvc_audio_volume_t* audio_volume = (rvc_audio_volume_t*)userdata;
  196. /*
  197. * If eol is set to a positive number,
  198. * you're at the end of the list
  199. */
  200. if (eol > 0)
  201. return;
  202. if (strstr(l->description, audio_volume->strdevicename)) {
  203. audio_volume->cvolumes = l->volume;
  204. audio_volume->volume_flag = 1;
  205. }
  206. }
  207. /*
  208. * pa_mainloop will call this function when it's ready to tell us
  209. * about a source (input).
  210. * This callback is pretty much identical to the previous
  211. * but it will only print the output devices
  212. * args:
  213. * c - pointer to pulse context
  214. * l - pointer to sink info
  215. * eol - end of list
  216. * data - pointer to user data (audio context)
  217. *
  218. * asserts:
  219. * none
  220. *
  221. * returns: none
  222. */
  223. static void pa_sinklist_cb(pa_context* c, const pa_sink_info* l, int eol, void* userdata)
  224. {
  225. rvc_audio_context_t* audio_ctx = (rvc_audio_context_t*)userdata;
  226. /*
  227. * If eol is set to a positive number,
  228. * you're at the end of the list
  229. */
  230. if (eol > 0)
  231. return;
  232. sink_index++;
  233. double my_latency = 0.0;
  234. int verbosity = 1;
  235. if (my_latency <= 0.0)
  236. my_latency = (double)latency_ms / 1000;
  237. audio_ctx->num_output_dev++;
  238. /*add device to list*/
  239. audio_ctx->list_output_devices = (rvc_audio_device_t*)realloc(audio_ctx->list_output_devices, audio_ctx->num_output_dev * sizeof(rvc_audio_device_t));
  240. if (audio_ctx->list_output_devices == NULL)
  241. {
  242. printf("AUDIO: FATAL memory allocation failure (pa_sinklist_cb): %s\n", strerror(errno));
  243. exit(-1);
  244. }
  245. /*fill device data*/
  246. audio_ctx->list_output_devices[audio_ctx->num_output_dev - 1].id = l->index; /*saves dev id*/
  247. strncpy(audio_ctx->list_output_devices[audio_ctx->num_output_dev - 1].name, l->name, MAX_PATH-1);
  248. strncpy(audio_ctx->list_output_devices[audio_ctx->num_output_dev - 1].description, l->description, MAX_PATH-1);
  249. audio_ctx->list_output_devices[audio_ctx->num_output_dev - 1].channels = l->channel_map.channels;
  250. audio_ctx->list_output_devices[audio_ctx->num_output_dev - 1].samprate = l->sample_spec.rate;
  251. audio_ctx->list_output_devices[audio_ctx->num_output_dev - 1].low_latency = my_latency; /*in seconds*/
  252. audio_ctx->list_output_devices[audio_ctx->num_output_dev - 1].high_latency = my_latency; /*in seconds*/
  253. }
  254. /*
  255. * This callback gets called when our context changes state.
  256. * We really only care about when it's ready or if it has failed
  257. * args:
  258. * c -pointer to pulse context
  259. * data - pointer to user data
  260. *
  261. * asserts:
  262. * none
  263. *
  264. * retuns: none
  265. */
  266. static void pa_state_cb(pa_context* c, void* data)
  267. {
  268. pa_context_state_t state;
  269. int* pa_ready = (int*)data;
  270. state = pa_context_get_state(c);
  271. switch (state)
  272. {
  273. // These are just here for reference
  274. case PA_CONTEXT_UNCONNECTED:
  275. break;
  276. case PA_CONTEXT_CONNECTING:
  277. case PA_CONTEXT_AUTHORIZING:
  278. case PA_CONTEXT_SETTING_NAME:
  279. break;
  280. case PA_CONTEXT_FAILED:
  281. case PA_CONTEXT_TERMINATED:
  282. *pa_ready = 2;
  283. break;
  284. case PA_CONTEXT_READY:
  285. *pa_ready = 1;
  286. break;
  287. default:
  288. break;
  289. }
  290. }
  291. /*
  292. * clean up and disconnect
  293. * args:
  294. * pa_ctx - pointer to pulse context
  295. * pa_ml - pointer to pulse mainloop
  296. *
  297. * asserts:
  298. * none
  299. *
  300. * returns:
  301. * none
  302. */
  303. static void finish(pa_context* pa_ctx, pa_mainloop* pa_ml)
  304. {
  305. /* clean up and disconnect */
  306. pa_context_disconnect(pa_ctx);
  307. pa_context_unref(pa_ctx);
  308. pa_mainloop_free(pa_ml);
  309. }
  310. /*
  311. * iterate the main loop until all devices are listed
  312. * args:
  313. * audio_ctx - pointer to audio context
  314. *
  315. * asserts:
  316. * audio_ctx is not null
  317. *
  318. * returns: error code
  319. */
  320. int AudioMgrImpl::pa_get_devicelist()
  321. {
  322. /*assertions*/
  323. assert(m_audio_context != NULL);
  324. /* Define our pulse audio loop and connection variables */
  325. pa_mainloop* pa_ml;
  326. pa_mainloop_api* pa_mlapi;
  327. pa_operation* pa_op = NULL;
  328. pa_context* pa_ctx;
  329. /* We'll need these state variables to keep track of our requests */
  330. int state = 0;
  331. int pa_ready = 0;
  332. /* Create a mainloop API and connection to the default server */
  333. pa_ml = pa_mainloop_new();
  334. pa_mlapi = pa_mainloop_get_api(pa_ml);
  335. pa_ctx = pa_context_new(pa_mlapi, "getDevices");
  336. /* This function connects to the pulse server */
  337. if (pa_context_connect(pa_ctx, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0)
  338. {
  339. audiolog("AUDIO: PULSE - unable to connect to server: pa_context_connect failed!");
  340. finish(pa_ctx, pa_ml);
  341. return -1;
  342. }
  343. /*
  344. * This function defines a callback so the server will tell us
  345. * it's state.
  346. * Our callback will wait for the state to be ready.
  347. * The callback will modify the variable to 1 so we know when we
  348. * have a connection and it's ready.
  349. * If there's an error, the callback will set pa_ready to 2
  350. */
  351. pa_context_set_state_callback(pa_ctx, pa_state_cb, &pa_ready);
  352. /*
  353. * Now we'll enter into an infinite loop until we get the data
  354. * we receive or if there's an error
  355. */
  356. for (;;)
  357. {
  358. /*
  359. * We can't do anything until PA is ready,
  360. * so just iterate the mainloop and continue
  361. */
  362. if (pa_ready == 0)
  363. {
  364. pa_mainloop_iterate(pa_ml, 1, NULL);
  365. continue;
  366. }
  367. ///* We couldn't get a connection to the server, so exit out */
  368. if (pa_ready == 2)
  369. {
  370. finish(pa_ctx, pa_ml);
  371. return -1;
  372. }
  373. /*
  374. * At this point, we're connected to the server and ready
  375. * to make requests
  376. */
  377. switch (state)
  378. {
  379. /* State 0: we haven't done anything yet */
  380. case 0:
  381. /*
  382. * This sends an operation to the server.
  383. * pa_sinklist_cb is our callback function and a pointer
  384. * o our devicelist will be passed to the callback
  385. * (audio_ctx) The operation ID is stored in the
  386. * pa_op variable
  387. */
  388. pa_op = pa_context_get_sink_info_list(
  389. pa_ctx,
  390. pa_sinklist_cb,
  391. (void*)m_audio_context);
  392. /* Update state for next iteration through the loop */
  393. state++;
  394. break;
  395. case 1:
  396. /*
  397. * Now we wait for our operation to complete.
  398. * When it's complete our pa_output_devicelist is
  399. * filled out, and we move along to the next state
  400. */
  401. if (pa_operation_get_state(pa_op) == PA_OPERATION_DONE)
  402. {
  403. pa_operation_unref(pa_op);
  404. /*
  405. * Now we perform another operation to get the
  406. * source(input device) list just like before.
  407. * This time we pass a pointer to our input structure
  408. */
  409. pa_op = pa_context_get_source_info_list(
  410. pa_ctx,
  411. pa_sourcelist_cb,
  412. (void*)m_audio_context);
  413. /* Update the state so we know what to do next */
  414. state++;
  415. }
  416. break;
  417. case 2:
  418. if (pa_operation_get_state(pa_op) == PA_OPERATION_DONE)
  419. {
  420. /*
  421. * Now we're done,
  422. * clean up and disconnect and return
  423. */
  424. pa_operation_unref(pa_op);
  425. finish(pa_ctx, pa_ml);
  426. return 0;
  427. }
  428. break;
  429. default:
  430. /* We should never see this state */
  431. audiolog("AUDIO: Pulse audio in state %d", state);
  432. return -1;
  433. }
  434. /*
  435. * Iterate the main loop and go again. The second argument is whether
  436. * or not the iteration should block until something is ready to be
  437. * done. Set it to zero for non-blocking.
  438. */
  439. pa_mainloop_iterate(pa_ml, 1, NULL);
  440. }
  441. finish(pa_ctx, pa_ml);
  442. return 0;
  443. }
  444. /*
  445. * get the number of available audio devices
  446. *
  447. * args:
  448. * binput - is input device
  449. * asserts:
  450. * audio_ctx is not null
  451. *
  452. * returns: number of listed audio devices
  453. */
  454. int AudioMgrImpl::audio_get_device_count(bool binput)
  455. {
  456. /*assertions*/
  457. assert(m_audio_context != NULL);
  458. if (binput){
  459. return m_audio_context->num_input_dev;
  460. }
  461. else {
  462. return m_audio_context->num_output_dev;
  463. }
  464. }
  465. /*
  466. * get the audio device referenced by index
  467. * args:
  468. * index - index of audio device
  469. *
  470. * asserts:
  471. * audio_ctx is not null
  472. *
  473. * returns: audio device referenced by index
  474. */
  475. rvc_audio_device_t* AudioMgrImpl::audio_get_input_device(int index)
  476. {
  477. /*assertions*/
  478. assert(m_audio_context != NULL);
  479. if (index >= m_audio_context->num_input_dev){
  480. index = m_audio_context->num_input_dev - 1;
  481. }
  482. if (index < 0){
  483. index = 0;
  484. }
  485. return &m_audio_context->list_input_devices[index];
  486. }
  487. /*
  488. * get the output audio device referenced by index
  489. * args:
  490. * index - index of output audio device
  491. *
  492. * asserts:
  493. * audio_ctx is not null
  494. *
  495. * returns: output audio device referenced by index
  496. */
  497. rvc_audio_device_t* AudioMgrImpl::audio_get_output_device( int index)
  498. {
  499. /*assertions*/
  500. assert(m_audio_context != NULL);
  501. if (index >= m_audio_context->num_output_dev){
  502. index = m_audio_context->num_output_dev - 1;
  503. }
  504. if (index < 0){
  505. index = 0;
  506. }
  507. return &m_audio_context->list_output_devices[index];
  508. }
  509. int AudioMgrImpl::audio_get_device_name(char* pstrbuf, size_t ulen, bool binput, int index)
  510. {
  511. int iret = -1;
  512. //模拟立体声
  513. const char stranalogstereo[] = { 0x20,0xe6,0xa8,0xa1,0xe6,0x8b,0x9f,0xe7,0xab,0x8b,0xe4,0xbd,0x93,0xe5,0xa3,0xb0,0 };
  514. //数字立体声
  515. const char strdigitalstereo[] = { 0x20,0xe6,0x95,0xb0,0xe5,0xad,0x97,0xe7,0xab,0x8b,0xe4,0xbd,0x93,0xe5,0xa3,0xb0,0 };
  516. //立体声
  517. const char strstereo[] = { 0x20,0xe7,0xab,0x8b,0xe4,0xbd,0x93,0xe5,0xa3,0xb0,0 };
  518. //多声道
  519. const char strmultistereo[] = { 0x20,0xe5,0xa4,0x9a,0xe5,0xa3,0xb0,0xe9,0x81,0x93,0 };
  520. rvc_audio_device_t* audio_device = NULL;
  521. if (binput){
  522. audio_device = audio_get_input_device(index);
  523. }
  524. else{
  525. audio_device = audio_get_output_device(index);
  526. }
  527. size_t unamelen = strlen(audio_device->description);
  528. if (ulen > unamelen){
  529. memcpy(pstrbuf, audio_device->description, ulen);
  530. char* pindex = NULL;
  531. if ((pindex = strstr(pstrbuf, stranalogstereo))||(pindex = strstr(pstrbuf, strdigitalstereo))){
  532. *pindex = 0;
  533. }
  534. if ((pindex = strstr(pstrbuf, strstereo)) || (pindex = strstr(pstrbuf, strmultistereo))) {
  535. *pindex = 0;
  536. }
  537. iret = 0;
  538. }
  539. return iret;
  540. }
  541. int AudioMgrImpl::audio_get_device_id(const char* pstrname, bool binput)
  542. {
  543. int iret = -1;
  544. if (NULL == pstrname){
  545. return iret;
  546. }
  547. int icount = audio_get_device_count(binput);
  548. if (icount > 0){
  549. for (int index = 0; index < icount; index++){
  550. rvc_audio_device_t* audio_device = NULL;
  551. if (binput) {
  552. audio_device = audio_get_input_device(index);
  553. }
  554. else {
  555. audio_device = audio_get_output_device(index);
  556. }
  557. if (NULL != audio_device){
  558. if (strstr(audio_device->description, pstrname)){
  559. iret = index;
  560. break;
  561. }
  562. }
  563. }
  564. }
  565. return iret;
  566. }
  567. rvc_audio_device_t* AudioMgrImpl::audio_get_device_infos(bool binput, int index)
  568. {
  569. rvc_audio_device_t* audio_device = NULL;
  570. if (binput) {
  571. audio_device = audio_get_input_device(index);
  572. }
  573. else {
  574. audio_device = audio_get_output_device(index);
  575. }
  576. return audio_device;
  577. }
  578. int AudioMgrImpl::audio_get_device_volume(int* ivolume, const char* pstrname, bool binput)
  579. {
  580. audiopulse_get_device_volume(ivolume, pstrname, binput);
  581. float fvol = (*ivolume) * 1000 / RVC_MAX_VOLUME;
  582. int ivol = fvol;
  583. int ilast = ivol % 10;
  584. int inum = ivol / 10;
  585. if (ilast >= 5) {
  586. inum++;
  587. }
  588. *ivolume = inum;
  589. return 0;
  590. }
  591. int AudioMgrImpl::audiopulse_get_device_volume(int* ivolume, const char* pstrname, bool binput)
  592. {
  593. int iret = -1;
  594. pa_mainloop* pa_ml;
  595. pa_mainloop_api* pa_mlapi;
  596. pa_operation* pa_op = NULL;
  597. pa_context* pa_ctx;
  598. /* We'll need these state variables to keep track of our requests */
  599. int state = 0;
  600. int pa_ready = 0;
  601. rvc_audio_volume_t rvc_volume = { 0 };
  602. strcpy(rvc_volume.strdevicename, pstrname);
  603. /* Create a mainloop API and connection to the default server */
  604. pa_ml = pa_mainloop_new();
  605. pa_mlapi = pa_mainloop_get_api(pa_ml);
  606. pa_ctx = pa_context_new(pa_mlapi, "get audio volume");
  607. /* This function connects to the pulse server */
  608. if (pa_context_connect(pa_ctx, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0)
  609. {
  610. audiolog("AUDIO: PULSE - unable to connect to server: pa_context_connect failed");
  611. finish(pa_ctx, pa_ml);
  612. return -1;
  613. }
  614. /*
  615. * This function defines a callback so the server will tell us
  616. * it's state.
  617. * Our callback will wait for the state to be ready.
  618. * The callback will modify the variable to 1 so we know when we
  619. * have a connection and it's ready.
  620. * If there's an error, the callback will set pa_ready to 2
  621. */
  622. pa_context_set_state_callback(pa_ctx, pa_state_cb, &pa_ready);
  623. /*
  624. * Now we'll enter into an infinite loop until we get the data
  625. * we receive or if there's an error
  626. */
  627. for (;;)
  628. {
  629. /*
  630. * We can't do anything until PA is ready,
  631. * so just iterate the mainloop and continue
  632. */
  633. if (pa_ready == 0)
  634. {
  635. pa_mainloop_iterate(pa_ml, 1, NULL);
  636. continue;
  637. }
  638. ///* We couldn't get a connection to the server, so exit out */
  639. if (pa_ready == 2)
  640. {
  641. finish(pa_ctx, pa_ml);
  642. return -1;
  643. }
  644. /*
  645. * At this point, we're connected to the server and ready
  646. * to make requests
  647. */
  648. if (0 == state){
  649. if (binput) {
  650. pa_op = pa_context_get_source_info_list(
  651. pa_ctx,
  652. pa_sourcevolume_cb,
  653. (void*)& rvc_volume);
  654. }
  655. else
  656. {
  657. pa_op = pa_context_get_sink_info_list(
  658. pa_ctx,
  659. pa_sinkvolume_cb,
  660. (void*)& rvc_volume);
  661. }
  662. state++;
  663. }
  664. if (rvc_volume.volume_flag){
  665. *ivolume = rvc_volume.cvolumes.values[0];
  666. pa_operation_unref(pa_op);
  667. break;
  668. }
  669. if (pa_operation_get_state(pa_op) == PA_OPERATION_DONE)
  670. {
  671. /*
  672. * Now we're done,
  673. * clean up and disconnect and return
  674. */
  675. pa_operation_unref(pa_op);
  676. finish(pa_ctx, pa_ml);
  677. return 0;
  678. }
  679. /*
  680. * Iterate the main loop and go again. The second argument is whether
  681. * or not the iteration should block until something is ready to be
  682. * done. Set it to zero for non-blocking.
  683. */
  684. pa_mainloop_iterate(pa_ml, 1, NULL);
  685. }
  686. finish(pa_ctx, pa_ml);
  687. return iret;
  688. }
  689. int AudioMgrImpl::audio_set_device_volume(int ivolume, const char* pstrname, bool binput)
  690. {
  691. int ivol = ivolume * RVC_MAX_VOLUME / 100;
  692. return audiopulse_set_device_volume(ivol, pstrname, binput);
  693. }
  694. int AudioMgrImpl::audiopulse_set_device_volume(int ivolume, const char* pstrname, bool binput)
  695. {
  696. int iret = -1;
  697. pa_mainloop* pa_ml;
  698. pa_mainloop_api* pa_mlapi;
  699. pa_operation* pa_op = NULL;
  700. pa_context* pa_ctx;
  701. /* We'll need these state variables to keep track of our requests */
  702. int state = 0;
  703. int pa_ready = 0;
  704. rvc_volume_set_t rvc_volume = { 0 };
  705. strcpy(rvc_volume.strdevicename, pstrname);
  706. rvc_volume.ivolume = ivolume;
  707. /* Create a mainloop API and connection to the default server */
  708. pa_ml = pa_mainloop_new();
  709. pa_mlapi = pa_mainloop_get_api(pa_ml);
  710. pa_ctx = pa_context_new(pa_mlapi, "set audio volume");
  711. /* This function connects to the pulse server */
  712. if (pa_context_connect(pa_ctx, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0)
  713. {
  714. audiolog("AUDIO: PULSE - unable to connect to server: pa_context_connect failed");
  715. finish(pa_ctx, pa_ml);
  716. return -1;
  717. }
  718. /*
  719. * This function defines a callback so the server will tell us
  720. * it's state.
  721. * Our callback will wait for the state to be ready.
  722. * The callback will modify the variable to 1 so we know when we
  723. * have a connection and it's ready.
  724. * If there's an error, the callback will set pa_ready to 2
  725. */
  726. pa_context_set_state_callback(pa_ctx, pa_state_cb, &pa_ready);
  727. /*
  728. * Now we'll enter into an infinite loop until we get the data
  729. * we receive or if there's an error
  730. */
  731. for (;;)
  732. {
  733. /*
  734. * We can't do anything until PA is ready,
  735. * so just iterate the mainloop and continue
  736. */
  737. if (pa_ready == 0)
  738. {
  739. pa_mainloop_iterate(pa_ml, 1, NULL);
  740. continue;
  741. }
  742. ///* We couldn't get a connection to the server, so exit out */
  743. if (pa_ready == 2)
  744. {
  745. finish(pa_ctx, pa_ml);
  746. return -1;
  747. }
  748. /*
  749. * At this point, we're connected to the server and ready
  750. * to make requests
  751. */
  752. if (0 == state) {
  753. if (binput) {
  754. pa_op = pa_context_get_source_info_list(
  755. pa_ctx,
  756. pa_setsourcevolume_cb,
  757. (void*)&rvc_volume);
  758. }
  759. else
  760. {
  761. pa_op = pa_context_get_sink_info_list(
  762. pa_ctx,
  763. pa_setsinkvolume_cb,
  764. (void*)&rvc_volume);
  765. }
  766. state++;
  767. }
  768. if (rvc_volume.volume_flag) {
  769. pa_operation_unref(pa_op);
  770. break;
  771. }
  772. if (pa_operation_get_state(pa_op) == PA_OPERATION_DONE)
  773. {
  774. /*
  775. * Now we're done,
  776. * clean up and disconnect and return
  777. */
  778. pa_operation_unref(pa_op);
  779. finish(pa_ctx, pa_ml);
  780. return 0;
  781. }
  782. /*
  783. * Iterate the main loop and go again. The second argument is whether
  784. * or not the iteration should block until something is ready to be
  785. * done. Set it to zero for non-blocking.
  786. */
  787. pa_mainloop_iterate(pa_ml, 1, NULL);
  788. }
  789. finish(pa_ctx, pa_ml);
  790. return iret;
  791. }
  792. int AudioMgrImpl::audio_mgr_initialize()
  793. {
  794. int iret = -1;
  795. m_audio_context = (rvc_audio_context_t*)calloc(1, sizeof(rvc_audio_context_t));
  796. if (NULL == m_audio_context){
  797. audiolog("%s:%d couldn't allocate audio context.", __FUNCTION__, __LINE__);
  798. return iret;
  799. }
  800. iret = audio_init_pulseaudio();
  801. /*force a valid number of channels*/
  802. if (m_audio_context->channels > 2) {
  803. m_audio_context->channels = 2;
  804. }
  805. return iret;
  806. }
  807. /*
  808. * close and clean audio context for pulse audio api
  809. *
  810. * asserts:
  811. * none
  812. *
  813. * returns: none
  814. */
  815. int AudioMgrImpl::audio_close_pulseaudio()
  816. {
  817. int iret = -1;
  818. if (m_audio_context == NULL){
  819. return iret;
  820. }
  821. if (m_audio_context->stream_flag == AUDIO_STRM_ON) {
  822. stop_audio_capture();
  823. }
  824. if (NULL != m_audio_context->list_input_devices) {
  825. free(m_audio_context->list_input_devices);
  826. m_audio_context->list_input_devices = NULL;
  827. }
  828. if (NULL != m_audio_context->list_output_devices){
  829. free(m_audio_context->list_output_devices);
  830. m_audio_context->list_output_devices = NULL;
  831. }
  832. if (NULL != m_audio_context->capture_buff) {
  833. free(m_audio_context->capture_buff);
  834. m_audio_context->capture_buff = NULL;
  835. }
  836. free(m_audio_context);
  837. m_audio_context = NULL;
  838. iret = 0;
  839. return iret;
  840. }
  841. /*
  842. * stop and join the main loop iteration thread
  843. *
  844. * asserts:
  845. * audio_ctx is not null
  846. *
  847. * returns: error code
  848. */
  849. int AudioMgrImpl::stop_audio_capture()
  850. {
  851. /*assertions*/
  852. assert(m_audio_context != NULL);
  853. m_audio_context->stream_flag = AUDIO_STRM_OFF;
  854. if (0 != __THREAD_JOIN(m_readthread)){
  855. audiolog("read thread join failed!");
  856. }
  857. return 0;
  858. }
  859. int AudioMgrImpl::audio_mgr_terminate()
  860. {
  861. int iret = -1;
  862. /*assertions*/
  863. assert(m_audio_context != NULL);
  864. /* thread must be joined before destroying the mutex
  865. * so no need to unlock before destroying it
  866. */
  867. iret = audio_close_pulseaudio();
  868. return iret;
  869. }
  870. /*
  871. * update pulse audio latency
  872. * args:
  873. * s - pointer to pa_stream
  874. *
  875. * asserts:
  876. * none
  877. *
  878. * returns:none
  879. */
  880. static void get_latency(pa_stream* s)
  881. {
  882. pa_usec_t l;
  883. int negative;
  884. pa_stream_get_timing_info(s);
  885. if (pa_stream_get_latency(s, &l, &negative) != 0)
  886. {
  887. return;
  888. }
  889. latency = l; /*can only be negative in monitoring streams*/
  890. }
  891. /*
  892. * audio record callback
  893. * args:
  894. * s - pointer to pa_stream
  895. * length - buffer length
  896. * data - pointer to user data
  897. *
  898. * asserts:
  899. * none
  900. *
  901. * returns: none
  902. */
  903. static void stream_request_cb(pa_stream* s, size_t length, void* data)
  904. {
  905. rvc_audio_context_t* audio_ctx = (rvc_audio_context_t*)data;
  906. if (audio_ctx->channels == 0){
  907. return;
  908. }
  909. if (audio_ctx->samprate == 0){
  910. return;
  911. }
  912. int64_t ts = 0;
  913. while (pa_stream_readable_size(s) > 0)
  914. {
  915. const void* inputBuffer;
  916. size_t length;
  917. /*read from stream*/
  918. if (pa_stream_peek(s, &inputBuffer, &length) < 0){
  919. return;
  920. }
  921. if (length == 0){
  922. return; /*buffer is empty*/
  923. }
  924. get_latency(s);
  925. ts = ns_time_monotonic() - (latency * 1000);
  926. if (audio_ctx->last_ts <= 0)
  927. audio_ctx->last_ts = ts;
  928. audio_ctx->audio_param.on_audio_callback(inputBuffer, length, audio_ctx->audio_param.user_data);
  929. pa_stream_drop(s); /*clean the samples*/
  930. }
  931. }
  932. /*
  933. * Iterate the main loop while recording is on.
  934. * This function runs under it's own thread called by audio_pulse_start
  935. * args:
  936. * data - pointer to user data (audio context)
  937. *
  938. * asserts:
  939. * data is not null
  940. *
  941. * returns: pointer to error code
  942. */
  943. void* pulse_read_audio(void* data)
  944. {
  945. AudioMgrImpl* audio_mgr = (AudioMgrImpl*)data;
  946. assert(audio_mgr != NULL);
  947. rvc_audio_context_t* audio_ctx = audio_mgr->audio_get_context();
  948. /*assertions*/
  949. assert(audio_ctx != NULL);
  950. pa_mainloop* pa_ml;
  951. pa_mainloop_api* pa_mlapi;
  952. pa_buffer_attr bufattr;
  953. pa_stream* recordstream = NULL; // pulse audio stream
  954. pa_context* pa_ctx; //pulse context
  955. pa_sample_spec ss;
  956. pa_stream_flags_t flags = PA_STREAM_NOFLAGS;
  957. int32_t pastream_flag = (int32_t)PA_STREAM_NOFLAGS;
  958. int r;
  959. int pa_ready = 0;
  960. /* Create a mainloop API and connection to the default server */
  961. pa_ml = pa_mainloop_new();
  962. pa_mlapi = pa_mainloop_get_api(pa_ml);
  963. pa_ctx = pa_context_new(pa_mlapi, "rvc pulse API");
  964. if (pa_context_connect(pa_ctx, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0)
  965. {
  966. audio_mgr->audiolog("AUDIO: PULSE - unable to connect to server: pa_context_connect failed.");
  967. finish(pa_ctx, pa_ml);
  968. return ((void*)-1);
  969. }
  970. /*
  971. * This function defines a callback so the server will tell us it's state.
  972. * Our callback will wait for the state to be ready. The callback will
  973. * modify the variable to 1 so we know when we have a connection and it's
  974. * ready.
  975. * If there's an error, the callback will set pa_ready to 2
  976. */
  977. pa_context_set_state_callback(pa_ctx, pa_state_cb, &pa_ready);
  978. /*
  979. * This function defines a time event callback (called every TIME_EVENT_USEC)
  980. */
  981. //pa_context_rttime_new(pa_ctx, pa_rtclock_now() + TIME_EVENT_USEC, time_event_callback, NULL);
  982. /*
  983. * We can't do anything until PA is ready, so just iterate the mainloop
  984. * and continue
  985. */
  986. while (pa_ready == 0){
  987. pa_mainloop_iterate(pa_ml, 1, NULL);
  988. }
  989. if (pa_ready == 2){
  990. finish(pa_ctx, pa_ml);
  991. return ((void*)-1);
  992. }
  993. /* set the sample spec (frame rate, channels and format) */
  994. ss.rate = audio_ctx->samprate;
  995. ss.channels = audio_ctx->channels;
  996. ss.format = audio_ctx->eformat;
  997. recordstream = pa_stream_new(pa_ctx, "Record", &ss, NULL);
  998. if (!recordstream)
  999. audio_mgr->audiolog("AUDIO: (pulse audio) pa_stream_new failed (chan:%d rate:%d)",
  1000. ss.channels, ss.rate);
  1001. /* define the callbacks */
  1002. pa_stream_set_read_callback(recordstream, stream_request_cb, (void*)audio_ctx);
  1003. // Set properties of the record buffer
  1004. pa_zero(bufattr);
  1005. /* optimal value for all is (uint32_t)-1 ~= 2 sec */
  1006. bufattr.maxlength = (uint32_t)-1;
  1007. bufattr.prebuf = (uint32_t)-1;
  1008. bufattr.minreq = (uint32_t)-1;
  1009. if (audio_ctx->latency > 0){
  1010. bufattr.fragsize = bufattr.tlength = pa_usec_to_bytes((audio_ctx->latency * 1000) * PA_USEC_PER_MSEC, &ss);
  1011. uint32_t uvsersion = pa_context_get_protocol_version(pa_ctx);
  1012. if (uvsersion >= RVC_PA_ADJUST_LATENCY_PROTOCOL_VERSION) {
  1013. pastream_flag |= PA_STREAM_ADJUST_LATENCY;
  1014. }
  1015. //audio_mgr->audiolog("pa protocol version is %d.", uvsersion);
  1016. }
  1017. else {
  1018. bufattr.fragsize = bufattr.tlength = (uint32_t)-1;
  1019. }
  1020. pastream_flag |= PA_STREAM_INTERPOLATE_TIMING;
  1021. pastream_flag |= PA_STREAM_AUTO_TIMING_UPDATE;
  1022. char* dev = audio_ctx->list_input_devices[audio_ctx->device].name;
  1023. //audio_mgr->audiolog("AUDIO: (pulse audio) connecting to device %s (channels %d rate %d)", dev, ss.channels, ss.rate);
  1024. r = pa_stream_connect_record(recordstream, dev, &bufattr, (pa_stream_flags_t)pastream_flag);
  1025. if (r < 0)
  1026. {
  1027. //audio_mgr->audiolog("AUDIO: (pulse audio) skip latency adjustment");
  1028. /*
  1029. * Old pulse audio servers don't like the ADJUST_LATENCY flag,
  1030. * so retry without that
  1031. */
  1032. r = pa_stream_connect_record(recordstream, dev, &bufattr,
  1033. pa_stream_flags_t((int32_t)PA_STREAM_INTERPOLATE_TIMING |
  1034. (int32_t)PA_STREAM_AUTO_TIMING_UPDATE));
  1035. }
  1036. if (r < 0)
  1037. {
  1038. audio_mgr->audiolog("AUDIO: (pulse audio) pa_stream_connect_record failed");
  1039. finish(pa_ctx, pa_ml);
  1040. return ((void*)-1);
  1041. }
  1042. get_latency(recordstream);
  1043. /*
  1044. * Iterate the main loop while streaming. The second argument is whether
  1045. * or not the iteration should block until something is ready to be
  1046. * done. Set it to zero for non-blocking.
  1047. */
  1048. while (audio_ctx->stream_flag == AUDIO_STRM_ON){
  1049. pa_mainloop_iterate(pa_ml, 1, NULL);
  1050. }
  1051. pa_stream_set_read_callback(recordstream, NULL, NULL);
  1052. //audio_mgr->audiolog("AUDIO: (pulse audio) stream terminated(%i)", audio_ctx->stream_flag);
  1053. pa_stream_disconnect(recordstream);
  1054. pa_stream_unref(recordstream);
  1055. finish(pa_ctx, pa_ml);
  1056. return ((void*)0);
  1057. }
  1058. int AudioMgrImpl::set_audio_capture_params(audiocap_param_t* param)
  1059. {
  1060. assert(param != NULL);
  1061. audio_set_pulseaudio_device(param->ideviceid);
  1062. audio_set_pulsecap_params(param);
  1063. return 0;
  1064. }
  1065. /*
  1066. * set audio device
  1067. * index - device index to set
  1068. *
  1069. * asserts:
  1070. * audio_ctx is not null
  1071. *
  1072. * returns: none
  1073. */
  1074. int AudioMgrImpl::audio_set_pulseaudio_device(int index)
  1075. {
  1076. /*assertions*/
  1077. assert(m_audio_context != NULL);
  1078. if (index >= m_audio_context->num_input_dev) {
  1079. m_audio_context->device = m_audio_context->num_input_dev - 1;
  1080. }
  1081. else if (index >= 0) {
  1082. m_audio_context->device = index;
  1083. }
  1084. return 0;
  1085. }
  1086. int AudioMgrImpl::audio_set_pulsecap_params(audiocap_param_t* param)
  1087. {
  1088. assert(param != NULL);
  1089. m_audio_context->channels = param->ichannels;
  1090. if (m_audio_context->channels > 2) {
  1091. m_audio_context->channels = 2;/*limit it to stereo input*/
  1092. }
  1093. m_audio_context->samprate = param->isamprate;
  1094. m_audio_context->eformat = pa_sample_format_t(param->isampleformat);
  1095. m_audio_context->audio_param.on_audio_callback = param->on_audio_callback;
  1096. m_audio_context->audio_param.user_data = param->user_data;
  1097. m_audio_context->latency = param->flatency;
  1098. }
  1099. /*
  1100. * set the current latency
  1101. *
  1102. * asserts:
  1103. * audio_ctx is not null
  1104. *
  1105. * returns: none
  1106. */
  1107. void AudioMgrImpl::audio_set_latency(double latency)
  1108. {
  1109. /*assertions*/
  1110. assert(m_audio_context != NULL);
  1111. m_audio_context->latency = latency;
  1112. }
  1113. rvc_audio_context_t* AudioMgrImpl::audio_get_context()
  1114. {
  1115. return m_audio_context;
  1116. }
  1117. audiomgr_callback_t* AudioMgrImpl::audio_get_callback()
  1118. {
  1119. return &m_callback;
  1120. }
  1121. int AudioMgrImpl::start_audio_capture()
  1122. {
  1123. /*assertions*/
  1124. assert(m_audio_context != NULL);
  1125. m_audio_context->stream_flag = AUDIO_STRM_ON;
  1126. /* start audio capture thread */
  1127. if (__THREAD_CREATE(&m_readthread, pulse_read_audio, this)){
  1128. audiolog("AUDIO: (pulse audio) read thread creation failed");
  1129. m_audio_context->stream_flag = AUDIO_STRM_OFF;
  1130. return (-1);
  1131. }
  1132. return 0;
  1133. }
  1134. void AudioMgrImpl::audiolog(const char* fmt, ...)
  1135. {
  1136. if (m_callback.debug) {
  1137. va_list arg;
  1138. va_start(arg, fmt);
  1139. if(*m_callback.debug){
  1140. (*m_callback.debug)(m_callback.user_data, fmt, arg);
  1141. }
  1142. va_end(arg);
  1143. }
  1144. }