libaudiomgr_linux.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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_EX-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_EX-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. int AudioMgrImpl::audio_get_device_volume(int* ivolume, const char* pstrname, bool binput)
  568. {
  569. audiopulse_get_device_volume(ivolume, pstrname, binput);
  570. float fvol = (*ivolume) * 1000 / RVC_MAX_VOLUME;
  571. int ivol = fvol;
  572. int ilast = ivol % 10;
  573. int inum = ivol / 10;
  574. if (ilast >= 5) {
  575. inum++;
  576. }
  577. *ivolume = inum;
  578. return 0;
  579. }
  580. int AudioMgrImpl::audiopulse_get_device_volume(int* ivolume, const char* pstrname, bool binput)
  581. {
  582. int iret = -1;
  583. pa_mainloop* pa_ml;
  584. pa_mainloop_api* pa_mlapi;
  585. pa_operation* pa_op = NULL;
  586. pa_context* pa_ctx;
  587. /* We'll need these state variables to keep track of our requests */
  588. int state = 0;
  589. int pa_ready = 0;
  590. rvc_audio_volume_t rvc_volume = { 0 };
  591. strcpy(rvc_volume.strdevicename, pstrname);
  592. /* Create a mainloop API and connection to the default server */
  593. pa_ml = pa_mainloop_new();
  594. pa_mlapi = pa_mainloop_get_api(pa_ml);
  595. pa_ctx = pa_context_new(pa_mlapi, "get audio volume");
  596. /* This function connects to the pulse server */
  597. if (pa_context_connect(pa_ctx, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0)
  598. {
  599. audiolog("AUDIO: PULSE - unable to connect to server: pa_context_connect failed");
  600. finish(pa_ctx, pa_ml);
  601. return -1;
  602. }
  603. /*
  604. * This function defines a callback so the server will tell us
  605. * it's state.
  606. * Our callback will wait for the state to be ready.
  607. * The callback will modify the variable to 1 so we know when we
  608. * have a connection and it's ready.
  609. * If there's an error, the callback will set pa_ready to 2
  610. */
  611. pa_context_set_state_callback(pa_ctx, pa_state_cb, &pa_ready);
  612. /*
  613. * Now we'll enter into an infinite loop until we get the data
  614. * we receive or if there's an error
  615. */
  616. for (;;)
  617. {
  618. /*
  619. * We can't do anything until PA is ready,
  620. * so just iterate the mainloop and continue
  621. */
  622. if (pa_ready == 0)
  623. {
  624. pa_mainloop_iterate(pa_ml, 1, NULL);
  625. continue;
  626. }
  627. ///* We couldn't get a connection to the server, so exit out */
  628. if (pa_ready == 2)
  629. {
  630. finish(pa_ctx, pa_ml);
  631. return -1;
  632. }
  633. /*
  634. * At this point, we're connected to the server and ready
  635. * to make requests
  636. */
  637. if (0 == state){
  638. if (binput) {
  639. pa_op = pa_context_get_source_info_list(
  640. pa_ctx,
  641. pa_sourcevolume_cb,
  642. (void*)& rvc_volume);
  643. }
  644. else
  645. {
  646. pa_op = pa_context_get_sink_info_list(
  647. pa_ctx,
  648. pa_sinkvolume_cb,
  649. (void*)& rvc_volume);
  650. }
  651. state++;
  652. }
  653. if (rvc_volume.volume_flag){
  654. *ivolume = rvc_volume.cvolumes.values[0];
  655. pa_operation_unref(pa_op);
  656. break;
  657. }
  658. if (pa_operation_get_state(pa_op) == PA_OPERATION_DONE)
  659. {
  660. /*
  661. * Now we're done,
  662. * clean up and disconnect and return
  663. */
  664. pa_operation_unref(pa_op);
  665. finish(pa_ctx, pa_ml);
  666. return 0;
  667. }
  668. /*
  669. * Iterate the main loop and go again. The second argument is whether
  670. * or not the iteration should block until something is ready to be
  671. * done. Set it to zero for non-blocking.
  672. */
  673. pa_mainloop_iterate(pa_ml, 1, NULL);
  674. }
  675. finish(pa_ctx, pa_ml);
  676. return iret;
  677. }
  678. int AudioMgrImpl::audio_set_device_volume(int ivolume, const char* pstrname, bool binput)
  679. {
  680. int ivol = ivolume * RVC_MAX_VOLUME / 100;
  681. return audiopulse_set_device_volume(ivol, pstrname, binput);
  682. }
  683. int AudioMgrImpl::audiopulse_set_device_volume(int ivolume, const char* pstrname, bool binput)
  684. {
  685. int iret = -1;
  686. pa_mainloop* pa_ml;
  687. pa_mainloop_api* pa_mlapi;
  688. pa_operation* pa_op = NULL;
  689. pa_context* pa_ctx;
  690. /* We'll need these state variables to keep track of our requests */
  691. int state = 0;
  692. int pa_ready = 0;
  693. rvc_volume_set_t rvc_volume = { 0 };
  694. strcpy(rvc_volume.strdevicename, pstrname);
  695. rvc_volume.ivolume = ivolume;
  696. /* Create a mainloop API and connection to the default server */
  697. pa_ml = pa_mainloop_new();
  698. pa_mlapi = pa_mainloop_get_api(pa_ml);
  699. pa_ctx = pa_context_new(pa_mlapi, "set audio volume");
  700. /* This function connects to the pulse server */
  701. if (pa_context_connect(pa_ctx, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0)
  702. {
  703. audiolog("AUDIO: PULSE - unable to connect to server: pa_context_connect failed");
  704. finish(pa_ctx, pa_ml);
  705. return -1;
  706. }
  707. /*
  708. * This function defines a callback so the server will tell us
  709. * it's state.
  710. * Our callback will wait for the state to be ready.
  711. * The callback will modify the variable to 1 so we know when we
  712. * have a connection and it's ready.
  713. * If there's an error, the callback will set pa_ready to 2
  714. */
  715. pa_context_set_state_callback(pa_ctx, pa_state_cb, &pa_ready);
  716. /*
  717. * Now we'll enter into an infinite loop until we get the data
  718. * we receive or if there's an error
  719. */
  720. for (;;)
  721. {
  722. /*
  723. * We can't do anything until PA is ready,
  724. * so just iterate the mainloop and continue
  725. */
  726. if (pa_ready == 0)
  727. {
  728. pa_mainloop_iterate(pa_ml, 1, NULL);
  729. continue;
  730. }
  731. ///* We couldn't get a connection to the server, so exit out */
  732. if (pa_ready == 2)
  733. {
  734. finish(pa_ctx, pa_ml);
  735. return -1;
  736. }
  737. /*
  738. * At this point, we're connected to the server and ready
  739. * to make requests
  740. */
  741. if (0 == state) {
  742. if (binput) {
  743. pa_op = pa_context_get_source_info_list(
  744. pa_ctx,
  745. pa_setsourcevolume_cb,
  746. (void*)&rvc_volume);
  747. }
  748. else
  749. {
  750. pa_op = pa_context_get_sink_info_list(
  751. pa_ctx,
  752. pa_setsinkvolume_cb,
  753. (void*)&rvc_volume);
  754. }
  755. state++;
  756. }
  757. if (rvc_volume.volume_flag) {
  758. pa_operation_unref(pa_op);
  759. break;
  760. }
  761. if (pa_operation_get_state(pa_op) == PA_OPERATION_DONE)
  762. {
  763. /*
  764. * Now we're done,
  765. * clean up and disconnect and return
  766. */
  767. pa_operation_unref(pa_op);
  768. finish(pa_ctx, pa_ml);
  769. return 0;
  770. }
  771. /*
  772. * Iterate the main loop and go again. The second argument is whether
  773. * or not the iteration should block until something is ready to be
  774. * done. Set it to zero for non-blocking.
  775. */
  776. pa_mainloop_iterate(pa_ml, 1, NULL);
  777. }
  778. finish(pa_ctx, pa_ml);
  779. return iret;
  780. }
  781. int AudioMgrImpl::audio_mgr_initialize()
  782. {
  783. int iret = -1;
  784. m_audio_context = (rvc_audio_context_t*)calloc(1, sizeof(rvc_audio_context_t));
  785. if (NULL == m_audio_context){
  786. audiolog("%s:%d couldn't allocate audio context.", __FUNCTION__, __LINE__);
  787. return iret;
  788. }
  789. iret = audio_init_pulseaudio();
  790. /*force a valid number of channels*/
  791. if (m_audio_context->channels > 2) {
  792. m_audio_context->channels = 2;
  793. }
  794. return iret;
  795. }
  796. /*
  797. * close and clean audio context for pulse audio api
  798. *
  799. * asserts:
  800. * none
  801. *
  802. * returns: none
  803. */
  804. int AudioMgrImpl::audio_close_pulseaudio()
  805. {
  806. int iret = -1;
  807. if (m_audio_context == NULL){
  808. return iret;
  809. }
  810. if (m_audio_context->stream_flag == AUDIO_STRM_ON) {
  811. stop_audio_capture();
  812. }
  813. if (NULL != m_audio_context->list_input_devices) {
  814. free(m_audio_context->list_input_devices);
  815. m_audio_context->list_input_devices = NULL;
  816. }
  817. if (NULL != m_audio_context->list_output_devices){
  818. free(m_audio_context->list_output_devices);
  819. m_audio_context->list_output_devices = NULL;
  820. }
  821. if (NULL != m_audio_context->capture_buff) {
  822. free(m_audio_context->capture_buff);
  823. m_audio_context->capture_buff = NULL;
  824. }
  825. free(m_audio_context);
  826. m_audio_context = NULL;
  827. iret = 0;
  828. return iret;
  829. }
  830. /*
  831. * stop and join the main loop iteration thread
  832. *
  833. * asserts:
  834. * audio_ctx is not null
  835. *
  836. * returns: error code
  837. */
  838. int AudioMgrImpl::stop_audio_capture()
  839. {
  840. /*assertions*/
  841. assert(m_audio_context != NULL);
  842. m_audio_context->stream_flag = AUDIO_STRM_OFF;
  843. if (0 == __THREAD_JOIN(m_readthread)){
  844. audiolog("read thread join success!");
  845. }
  846. else{
  847. audiolog("read thread join failed!");
  848. }
  849. return 0;
  850. }
  851. int AudioMgrImpl::audio_mgr_terminate()
  852. {
  853. int iret = -1;
  854. /*assertions*/
  855. assert(m_audio_context != NULL);
  856. /* thread must be joined before destroying the mutex
  857. * so no need to unlock before destroying it
  858. */
  859. iret = audio_close_pulseaudio();
  860. return iret;
  861. }
  862. /*
  863. * update pulse audio latency
  864. * args:
  865. * s - pointer to pa_stream
  866. *
  867. * asserts:
  868. * none
  869. *
  870. * returns:none
  871. */
  872. static void get_latency(pa_stream* s)
  873. {
  874. pa_usec_t l;
  875. int negative;
  876. pa_stream_get_timing_info(s);
  877. if (pa_stream_get_latency(s, &l, &negative) != 0)
  878. {
  879. return;
  880. }
  881. latency = l; /*can only be negative in monitoring streams*/
  882. }
  883. /*
  884. * audio record callback
  885. * args:
  886. * s - pointer to pa_stream
  887. * length - buffer length
  888. * data - pointer to user data
  889. *
  890. * asserts:
  891. * none
  892. *
  893. * returns: none
  894. */
  895. static void stream_request_cb(pa_stream* s, size_t length, void* data)
  896. {
  897. rvc_audio_context_t* audio_ctx = (rvc_audio_context_t*)data;
  898. if (audio_ctx->channels == 0){
  899. return;
  900. }
  901. if (audio_ctx->samprate == 0){
  902. return;
  903. }
  904. int64_t ts = 0;
  905. while (pa_stream_readable_size(s) > 0)
  906. {
  907. const void* inputBuffer;
  908. size_t length;
  909. /*read from stream*/
  910. if (pa_stream_peek(s, &inputBuffer, &length) < 0){
  911. return;
  912. }
  913. if (length == 0){
  914. return; /*buffer is empty*/
  915. }
  916. get_latency(s);
  917. ts = ns_time_monotonic() - (latency * 1000);
  918. if (audio_ctx->last_ts <= 0)
  919. audio_ctx->last_ts = ts;
  920. audio_ctx->audio_param.on_audio_callback(inputBuffer, length, audio_ctx->audio_param.user_data);
  921. pa_stream_drop(s); /*clean the samples*/
  922. }
  923. }
  924. /*
  925. * Iterate the main loop while recording is on.
  926. * This function runs under it's own thread called by audio_pulse_start
  927. * args:
  928. * data - pointer to user data (audio context)
  929. *
  930. * asserts:
  931. * data is not null
  932. *
  933. * returns: pointer to error code
  934. */
  935. void* pulse_read_audio(void* data)
  936. {
  937. AudioMgrImpl* audio_mgr = (AudioMgrImpl*)data;
  938. assert(audio_mgr != NULL);
  939. rvc_audio_context_t* audio_ctx = audio_mgr->audio_get_context();
  940. /*assertions*/
  941. assert(audio_ctx != NULL);
  942. pa_mainloop* pa_ml;
  943. pa_mainloop_api* pa_mlapi;
  944. pa_buffer_attr bufattr;
  945. pa_stream* recordstream = NULL; // pulse audio stream
  946. pa_context* pa_ctx; //pulse context
  947. pa_sample_spec ss;
  948. pa_stream_flags_t flags = PA_STREAM_NOFLAGS;
  949. int32_t pastream_flag = (int32_t)PA_STREAM_NOFLAGS;
  950. int r;
  951. int pa_ready = 0;
  952. /* Create a mainloop API and connection to the default server */
  953. pa_ml = pa_mainloop_new();
  954. pa_mlapi = pa_mainloop_get_api(pa_ml);
  955. pa_ctx = pa_context_new(pa_mlapi, "rvc pulse API");
  956. if (pa_context_connect(pa_ctx, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0)
  957. {
  958. audio_mgr->audiolog("AUDIO: PULSE - unable to connect to server: pa_context_connect failed.");
  959. finish(pa_ctx, pa_ml);
  960. return ((void*)-1);
  961. }
  962. /*
  963. * This function defines a callback so the server will tell us it's state.
  964. * Our callback will wait for the state to be ready. The callback will
  965. * modify the variable to 1 so we know when we have a connection and it's
  966. * ready.
  967. * If there's an error, the callback will set pa_ready to 2
  968. */
  969. pa_context_set_state_callback(pa_ctx, pa_state_cb, &pa_ready);
  970. /*
  971. * This function defines a time event callback (called every TIME_EVENT_USEC)
  972. */
  973. //pa_context_rttime_new(pa_ctx, pa_rtclock_now() + TIME_EVENT_USEC, time_event_callback, NULL);
  974. /*
  975. * We can't do anything until PA is ready, so just iterate the mainloop
  976. * and continue
  977. */
  978. while (pa_ready == 0){
  979. pa_mainloop_iterate(pa_ml, 1, NULL);
  980. }
  981. if (pa_ready == 2){
  982. finish(pa_ctx, pa_ml);
  983. return ((void*)-1);
  984. }
  985. /* set the sample spec (frame rate, channels and format) */
  986. ss.rate = audio_ctx->samprate;
  987. ss.channels = audio_ctx->channels;
  988. ss.format = audio_ctx->eformat;
  989. recordstream = pa_stream_new(pa_ctx, "Record", &ss, NULL);
  990. if (!recordstream)
  991. audio_mgr->audiolog("AUDIO: (pulse audio) pa_stream_new failed (chan:%d rate:%d)",
  992. ss.channels, ss.rate);
  993. /* define the callbacks */
  994. pa_stream_set_read_callback(recordstream, stream_request_cb, (void*)audio_ctx);
  995. // Set properties of the record buffer
  996. pa_zero(bufattr);
  997. /* optimal value for all is (uint32_t)-1 ~= 2 sec */
  998. bufattr.maxlength = (uint32_t)-1;
  999. bufattr.prebuf = (uint32_t)-1;
  1000. bufattr.minreq = (uint32_t)-1;
  1001. if (audio_ctx->latency > 0){
  1002. bufattr.fragsize = bufattr.tlength = pa_usec_to_bytes((audio_ctx->latency * 1000) * PA_USEC_PER_MSEC, &ss);
  1003. uint32_t uvsersion = pa_context_get_protocol_version(pa_ctx);
  1004. if (uvsersion >= RVC_PA_ADJUST_LATENCY_PROTOCOL_VERSION) {
  1005. pastream_flag |= PA_STREAM_ADJUST_LATENCY;
  1006. }
  1007. audio_mgr->audiolog("pa protocol version is %d.", uvsersion);
  1008. }
  1009. else {
  1010. bufattr.fragsize = bufattr.tlength = (uint32_t)-1;
  1011. }
  1012. pastream_flag |= PA_STREAM_INTERPOLATE_TIMING;
  1013. pastream_flag |= PA_STREAM_AUTO_TIMING_UPDATE;
  1014. char* dev = audio_ctx->list_input_devices[audio_ctx->device].name;
  1015. audio_mgr->audiolog("AUDIO: (pulse audio) connecting to device %s (channels %d rate %d)", dev, ss.channels, ss.rate);
  1016. r = pa_stream_connect_record(recordstream, dev, &bufattr, (pa_stream_flags_t)pastream_flag);
  1017. if (r < 0)
  1018. {
  1019. audio_mgr->audiolog("AUDIO: (pulse audio) skip latency adjustment");
  1020. /*
  1021. * Old pulse audio servers don't like the ADJUST_LATENCY flag,
  1022. * so retry without that
  1023. */
  1024. r = pa_stream_connect_record(recordstream, dev, &bufattr,
  1025. pa_stream_flags_t((int32_t)PA_STREAM_INTERPOLATE_TIMING |
  1026. (int32_t)PA_STREAM_AUTO_TIMING_UPDATE));
  1027. }
  1028. else {
  1029. audio_mgr->audiolog("pa_stream_connect_record success!");
  1030. }
  1031. if (r < 0)
  1032. {
  1033. audio_mgr->audiolog("AUDIO: (pulse audio) pa_stream_connect_record failed");
  1034. finish(pa_ctx, pa_ml);
  1035. return ((void*)-1);
  1036. }
  1037. get_latency(recordstream);
  1038. /*
  1039. * Iterate the main loop while streaming. The second argument is whether
  1040. * or not the iteration should block until something is ready to be
  1041. * done. Set it to zero for non-blocking.
  1042. */
  1043. while (audio_ctx->stream_flag == AUDIO_STRM_ON){
  1044. pa_mainloop_iterate(pa_ml, 1, NULL);
  1045. }
  1046. pa_stream_set_read_callback(recordstream, NULL, NULL);
  1047. audio_mgr->audiolog("AUDIO: (pulse audio) stream terminated(%i)", audio_ctx->stream_flag);
  1048. pa_stream_disconnect(recordstream);
  1049. pa_stream_unref(recordstream);
  1050. finish(pa_ctx, pa_ml);
  1051. return ((void*)0);
  1052. }
  1053. int AudioMgrImpl::set_audio_capture_params(audiocap_param_t* param)
  1054. {
  1055. assert(param != NULL);
  1056. audio_set_pulseaudio_device(param->ideviceid);
  1057. audio_set_pulsecap_params(param);
  1058. return 0;
  1059. }
  1060. /*
  1061. * set audio device
  1062. * index - device index to set
  1063. *
  1064. * asserts:
  1065. * audio_ctx is not null
  1066. *
  1067. * returns: none
  1068. */
  1069. int AudioMgrImpl::audio_set_pulseaudio_device(int index)
  1070. {
  1071. /*assertions*/
  1072. assert(m_audio_context != NULL);
  1073. if (index >= m_audio_context->num_input_dev) {
  1074. m_audio_context->device = m_audio_context->num_input_dev - 1;
  1075. }
  1076. else if (index >= 0) {
  1077. m_audio_context->device = index;
  1078. }
  1079. int verbosity = 1;
  1080. if (verbosity > 0) {
  1081. printf("AUDIO: Pulseaudio device changed to %i\n", m_audio_context->device);
  1082. printf("AUDIO: Pulseaudio device description is %s\n", m_audio_context->list_input_devices[m_audio_context->device].description);
  1083. }
  1084. m_audio_context->latency = m_audio_context->list_input_devices[m_audio_context->device].high_latency;
  1085. m_audio_context->channels = m_audio_context->list_input_devices[m_audio_context->device].channels;
  1086. if (m_audio_context->channels > 2){
  1087. m_audio_context->channels = 2;/*limit it to stereo input*/
  1088. }
  1089. m_audio_context->samprate = m_audio_context->list_input_devices[m_audio_context->device].samprate;
  1090. return 0;
  1091. }
  1092. int AudioMgrImpl::audio_set_pulsecap_params(audiocap_param_t* param)
  1093. {
  1094. assert(param != NULL);
  1095. m_audio_context->channels = param->ichannels;
  1096. if (m_audio_context->channels > 2) {
  1097. m_audio_context->channels = 2;/*limit it to stereo input*/
  1098. }
  1099. m_audio_context->samprate = param->isamprate;
  1100. m_audio_context->eformat = pa_sample_format_t(param->isampleformat);
  1101. m_audio_context->audio_param.on_audio_callback = param->on_audio_callback;
  1102. m_audio_context->audio_param.user_data = param->user_data;
  1103. m_audio_context->latency = param->flatency;
  1104. //m_audio_context->samprate = m_audio_context->list_input_devices[m_audio_context->device].samprate;
  1105. }
  1106. /*
  1107. * set the current latency
  1108. *
  1109. * asserts:
  1110. * audio_ctx is not null
  1111. *
  1112. * returns: none
  1113. */
  1114. void AudioMgrImpl::audio_set_latency(double latency)
  1115. {
  1116. /*assertions*/
  1117. assert(m_audio_context != NULL);
  1118. m_audio_context->latency = latency;
  1119. }
  1120. rvc_audio_context_t* AudioMgrImpl::audio_get_context()
  1121. {
  1122. return m_audio_context;
  1123. }
  1124. audiomgr_callback_t* AudioMgrImpl::audio_get_callback()
  1125. {
  1126. return &m_callback;
  1127. }
  1128. int AudioMgrImpl::start_audio_capture()
  1129. {
  1130. /*assertions*/
  1131. assert(m_audio_context != NULL);
  1132. m_audio_context->stream_flag = AUDIO_STRM_ON;
  1133. /* start audio capture thread */
  1134. if (__THREAD_CREATE(&m_readthread, pulse_read_audio, this)){
  1135. audiolog("AUDIO: (pulse audio) read thread creation failed");
  1136. m_audio_context->stream_flag = AUDIO_STRM_OFF;
  1137. return (-1);
  1138. }
  1139. else {
  1140. audiolog("AUDIO: (pulse audio) read thread creation success, and thread id is %u.", m_readthread);
  1141. }
  1142. return 0;
  1143. }
  1144. void AudioMgrImpl::audiolog(const char* fmt, ...)
  1145. {
  1146. if (m_callback.debug) {
  1147. va_list arg;
  1148. va_start(arg, fmt);
  1149. if(*m_callback.debug){
  1150. (*m_callback.debug)(m_callback.user_data, fmt, arg);
  1151. }
  1152. va_end(arg);
  1153. }
  1154. }