audio_system.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. * Copyright (c) 2016 Actions Semi Co., Inc.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief audio system.
  9. */
  10. #include <os_common_api.h>
  11. #include <mem_manager.h>
  12. #include <msg_manager.h>
  13. #include <audio_system.h>
  14. #include <audio_record.h>
  15. #include <audio_track.h>
  16. #include <audio_device.h>
  17. #include <audio_hal.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <property_manager.h>
  22. #define SYS_LOG_NO_NEWLINE
  23. #ifdef SYS_LOG_DOMAIN
  24. #undef SYS_LOG_DOMAIN
  25. #endif
  26. #define SYS_LOG_DOMAIN "audio system"
  27. struct audio_system_t *audio_system;
  28. int audio_system_set_output_sample_rate(int value)
  29. {
  30. if (!audio_system)
  31. return -ESRCH;
  32. audio_system->output_sample_rate = value;
  33. return 0;
  34. }
  35. int audio_system_get_output_sample_rate(void)
  36. {
  37. if (!audio_system)
  38. return -ESRCH;
  39. return audio_system->output_sample_rate;
  40. }
  41. int audio_system_get_output_framecount(void)
  42. {
  43. if (!audio_system)
  44. return -ESRCH;
  45. return 0;
  46. }
  47. int audio_system_set_master_volume(int value)
  48. {
  49. if (!audio_system)
  50. return -ESRCH;
  51. audio_system->master_volume = value;
  52. return audio_system_set_stream_volume(AUDIO_STREAM_DEFAULT, value);
  53. }
  54. int audio_system_get_master_volume(void)
  55. {
  56. if (!audio_system)
  57. return -ESRCH;
  58. return audio_system->master_volume;
  59. }
  60. int audio_system_set_master_mute(int value)
  61. {
  62. if (!audio_system)
  63. return -ESRCH;
  64. audio_system->master_muted = value;
  65. /**TODO set master mute */
  66. return 0;
  67. }
  68. int audio_system_get_master_mute(void)
  69. {
  70. if (!audio_system)
  71. return -ESRCH;
  72. return audio_system->master_muted;
  73. }
  74. int audio_system_set_stream_mute(int stream_type, int mute)
  75. {
  76. struct audio_track_t *audio_track = NULL;
  77. int ret = -ESRCH;
  78. if (!audio_system)
  79. return -ESRCH;
  80. for (int i = 0; i < MAX_AUDIO_TRACK_NUM; i++) {
  81. audio_track = audio_system->audio_track_pool[i];
  82. if (audio_track && audio_track->stream_type == stream_type) {
  83. ret = audio_track_set_mute(audio_track, mute);
  84. }
  85. }
  86. return ret;
  87. }
  88. int audio_system_get_stream_mute(int stream_type)
  89. {
  90. struct audio_track_t *audio_track = NULL;
  91. int ret = -ESRCH;
  92. if (!audio_system)
  93. return -ESRCH;
  94. for (int i = 0; i < MAX_AUDIO_TRACK_NUM; i++) {
  95. audio_track = audio_system->audio_track_pool[i];
  96. if (audio_track && audio_track->stream_type == stream_type) {
  97. ret = audio_track_get_volume(audio_track);
  98. break;
  99. }
  100. }
  101. return (ret == 0) ? 1 : 0;
  102. }
  103. int audio_system_mute_microphone(int value)
  104. {
  105. struct audio_record_t *audio_record = NULL;
  106. if (!audio_system)
  107. return -ESRCH;
  108. audio_system->microphone_muted = value;
  109. for (int i = 0; i < MAX_AUDIO_RECORD_NUM; i++) {
  110. audio_record = audio_system->audio_record_pool[i];
  111. if (audio_record) {
  112. audio_record->muted = audio_system->microphone_muted;
  113. }
  114. }
  115. return 0;
  116. }
  117. int audio_system_get_microphone_muted(void)
  118. {
  119. if (!audio_system)
  120. return -ESRCH;
  121. return audio_system->microphone_muted ? 1 : 0;
  122. }
  123. int audio_system_register_track(struct audio_track_t *audio_track)
  124. {
  125. if (!audio_system)
  126. return -ESRCH;
  127. for (int i = 0; i < MAX_AUDIO_TRACK_NUM; i++) {
  128. if (!audio_system->audio_track_pool[i]) {
  129. audio_system->audio_track_pool[i] = audio_track;
  130. audio_system->audio_track_num++;
  131. SYS_LOG_INF(" %p index %d\n", audio_track, i);
  132. break;
  133. }
  134. }
  135. return 0;
  136. }
  137. int audio_system_mutex_lock(void)
  138. {
  139. if (!audio_system)
  140. return -ESRCH;
  141. return os_mutex_lock(&audio_system->audio_system_mutex, OS_FOREVER);
  142. }
  143. int audio_system_mutex_unlock(void)
  144. {
  145. if (!audio_system)
  146. return -ESRCH;
  147. os_mutex_unlock(&audio_system->audio_system_mutex);
  148. return 0;
  149. }
  150. struct audio_track_t *audio_system_get_track(void)
  151. {
  152. if (!audio_system)
  153. return NULL;
  154. for (int i = 0; i < MAX_AUDIO_TRACK_NUM; i++) {
  155. if (audio_system->audio_track_pool[i]) {
  156. return audio_system->audio_track_pool[i];
  157. }
  158. }
  159. return NULL;
  160. }
  161. int audio_system_unregister_track(struct audio_track_t *audio_track)
  162. {
  163. int ret = -ENXIO;
  164. if (!audio_system)
  165. return -ESRCH;
  166. for (int i = 0; i < MAX_AUDIO_TRACK_NUM; i++) {
  167. if (audio_system->audio_track_pool[i] == audio_track) {
  168. audio_system->audio_track_pool[i] = NULL;
  169. audio_system->audio_track_num--;
  170. SYS_LOG_INF(" %p index %d\n", audio_track, i);
  171. ret = 0;
  172. break;
  173. }
  174. }
  175. for (int i = 0; i < MAX_AUDIO_TRACK_NUM; i++) {
  176. struct audio_track_t *tmp_track = NULL;
  177. tmp_track = audio_system->audio_track_pool[i];
  178. if (tmp_track)
  179. audio_system_set_stream_volume(tmp_track->stream_type, tmp_track->volume);
  180. }
  181. return 0;
  182. }
  183. int audio_system_register_record(struct audio_record_t *audio_record)
  184. {
  185. int ret = -EAGAIN;
  186. if (!audio_system)
  187. return -ESRCH;
  188. for (int i = 0; i < MAX_AUDIO_RECORD_NUM; i++) {
  189. if (!audio_system->audio_record_pool[i]) {
  190. audio_system->audio_record_pool[i] = audio_record;
  191. audio_system->audio_record_num++;
  192. SYS_LOG_INF(" %p index %d\n", audio_record, i);
  193. ret = 0;
  194. break;
  195. }
  196. }
  197. return ret;
  198. }
  199. int audio_system_unregister_record(struct audio_record_t *audio_record)
  200. {
  201. int ret = -ENXIO;
  202. if (!audio_system)
  203. return -ESRCH;
  204. for (int i = 0; i < MAX_AUDIO_RECORD_NUM; i++) {
  205. if (audio_system->audio_record_pool[i] == audio_record) {
  206. audio_system->audio_record_pool[i] = NULL;
  207. SYS_LOG_INF(" %p index %d\n", audio_record, i);
  208. audio_system->audio_record_num--;
  209. ret = 0;
  210. break;
  211. }
  212. }
  213. return ret;
  214. }
  215. static void _audio_system_get_volume_config(const char *name, uint8_t *volume)
  216. {
  217. #ifdef CONFIG_PROPERTY
  218. char temp[8];
  219. memset(temp, 0, sizeof(temp));
  220. int ret = property_get(name, temp, sizeof(temp));
  221. if (ret >= 0) {
  222. *volume = atoi(temp);
  223. } else {
  224. *volume = DEFAULT_VOLUME;
  225. }
  226. SYS_LOG_INF(" %s %d\n", name, *volume);
  227. #endif
  228. }
  229. static int _audio_system_save_volume(const char *name, uint8_t volume)
  230. {
  231. char temp[5];
  232. if (!audio_policy_check_save_volume_to_nvram()) {
  233. return 0;
  234. }
  235. snprintf(temp, sizeof(temp), "%d", volume);
  236. #ifdef CONFIG_PROPERTY
  237. return property_set(name, temp, strlen(temp) + 1);
  238. #else
  239. return 0;
  240. #endif
  241. }
  242. int audio_system_set_stream_volume(int stream_type, int volume)
  243. {
  244. struct audio_track_t *audio_track = NULL;
  245. int ret = 0;
  246. if (!audio_system)
  247. return -ESRCH;
  248. if (volume >= audio_policy_get_volume_level()) {
  249. volume = audio_policy_get_volume_level();
  250. ret = MAX_VOLUME_VALUE;
  251. }
  252. /**usound default used 16 level */
  253. if (stream_type == AUDIO_STREAM_USOUND) {
  254. if (volume >= 16) {
  255. volume = 16;
  256. ret = MAX_VOLUME_VALUE;
  257. }
  258. }
  259. if (volume <= 0) {
  260. ret = MIN_VOLUME_VALUE;
  261. volume = 0;
  262. }
  263. os_mutex_lock(&audio_system->audio_system_mutex, OS_FOREVER);
  264. for (int i = 0; i < MAX_AUDIO_TRACK_NUM; i++) {
  265. audio_track = audio_system->audio_track_pool[i];
  266. if (audio_track &&
  267. (audio_track->stream_type == stream_type
  268. || stream_type == AUDIO_STREAM_DEFAULT)) {
  269. audio_track_set_volume(audio_track, volume);
  270. }
  271. }
  272. os_mutex_unlock(&audio_system->audio_system_mutex);
  273. if (stream_type == AUDIO_STREAM_VOICE) {
  274. audio_system->voice_volume = volume;
  275. _audio_system_save_volume(CFG_BTCALL_VOLUME, volume);
  276. }
  277. if (stream_type == AUDIO_STREAM_MUSIC || stream_type == AUDIO_STREAM_DEFAULT) {
  278. audio_system->music_volume = volume;
  279. _audio_system_save_volume(CFG_BTPLAY_VOLUME, volume);
  280. }
  281. if (stream_type == AUDIO_STREAM_LINEIN || stream_type == AUDIO_STREAM_DEFAULT) {
  282. audio_system->linein_volume = volume;
  283. _audio_system_save_volume(CFG_LINEIN_VOLUME, volume);
  284. }
  285. if (stream_type == AUDIO_STREAM_USOUND || stream_type == AUDIO_STREAM_DEFAULT) {
  286. audio_system->usound_volume = volume;
  287. _audio_system_save_volume(CFG_USOUND_VOLUME, volume);
  288. }
  289. if (stream_type == AUDIO_STREAM_LOCAL_MUSIC || stream_type == AUDIO_STREAM_DEFAULT) {
  290. audio_system->lcmusic_volume = volume;
  291. _audio_system_save_volume(CFG_LOCALPLAY_VOLUME, volume);
  292. }
  293. if (stream_type == AUDIO_STREAM_FM || stream_type == AUDIO_STREAM_DEFAULT) {
  294. audio_system->fm_volume = volume;
  295. _audio_system_save_volume(CFG_FM_VOLUME, volume);
  296. }
  297. if (stream_type == AUDIO_STREAM_I2SRX_IN || stream_type == AUDIO_STREAM_DEFAULT) {
  298. audio_system->i2srx_in_volume = volume;
  299. _audio_system_save_volume(CFG_I2SRX_IN_VOLUME, volume);
  300. }
  301. if (stream_type == AUDIO_STREAM_MIC_IN || stream_type == AUDIO_STREAM_DEFAULT) {
  302. audio_system->mic_in_volume = volume;
  303. _audio_system_save_volume(CFG_MIC_IN_VOLUME, volume);
  304. }
  305. if (stream_type == AUDIO_STREAM_SPDIF_IN || stream_type == AUDIO_STREAM_DEFAULT) {
  306. audio_system->spidf_in_volume = volume;
  307. _audio_system_save_volume(CFG_SPDIF_IN_VOLUME, volume);
  308. }
  309. if (stream_type != AUDIO_STREAM_VOICE) {
  310. audio_system->master_volume = volume;
  311. if (volume < audio_system->min_volume)
  312. volume = audio_system->min_volume;
  313. if(volume > audio_system->max_volume)
  314. volume = audio_system->max_volume;
  315. if (!audio_policy_check_tts_fixed_volume()) {
  316. audio_system->tts_volume = volume;
  317. _audio_system_save_volume(CFG_TONE_VOLUME, volume);
  318. }
  319. }
  320. SYS_LOG_INF("volume: %d %d ret %d ok\n", volume, stream_type, ret);
  321. return ret;
  322. }
  323. int audio_system_get_current_volume(int stream_type)
  324. {
  325. int volume = 0;
  326. struct audio_track_t *audio_track = NULL;
  327. if (!audio_system)
  328. return -ESRCH;
  329. volume = audio_system_get_stream_volume(stream_type);
  330. /**make sure tts have volume*/
  331. for (int i = 0; i < MAX_AUDIO_TRACK_NUM; i++) {
  332. audio_track = audio_system->audio_track_pool[i];
  333. if ((audio_track->stream_type == AUDIO_STREAM_TTS)
  334. || (audio_track->stream_type == AUDIO_STREAM_TIP)) {
  335. if (volume != audio_system->tts_volume) {
  336. volume = audio_system->tts_volume;
  337. }
  338. }
  339. }
  340. return volume;
  341. }
  342. int audio_system_get_current_pa_volume(int stream_type)
  343. {
  344. int volume = 0;
  345. int pa_volume = 0;
  346. struct audio_track_t *audio_track = NULL;
  347. if (!audio_system)
  348. return -ESRCH;
  349. volume = audio_system_get_current_volume(stream_type);
  350. pa_volume = audio_policy_get_pa_volume(stream_type, volume);
  351. /**make sure tts have volume*/
  352. for (int i = 0; i < MAX_AUDIO_TRACK_NUM; i++) {
  353. audio_track = audio_system->audio_track_pool[i];
  354. if ((audio_track->stream_type == AUDIO_STREAM_TTS)
  355. || (audio_track->stream_type == AUDIO_STREAM_TIP)) {
  356. pa_volume = audio_policy_get_pa_volume(AUDIO_STREAM_TTS, volume);
  357. }
  358. }
  359. return pa_volume;
  360. }
  361. int audio_system_get_stream_volume(int stream_type)
  362. {
  363. int volume = 0;
  364. if (!audio_system)
  365. return -ESRCH;
  366. switch (stream_type) {
  367. case AUDIO_STREAM_MUSIC:
  368. volume = audio_system->music_volume;
  369. break;
  370. case AUDIO_STREAM_TTS:
  371. case AUDIO_STREAM_TIP:
  372. volume = audio_system->tts_volume;
  373. break;
  374. case AUDIO_STREAM_VOICE:
  375. volume = audio_system->voice_volume;
  376. break;
  377. case AUDIO_STREAM_LINEIN:
  378. volume = audio_system->linein_volume;
  379. break;
  380. case AUDIO_STREAM_USOUND:
  381. volume = audio_system->usound_volume;
  382. break;
  383. case AUDIO_STREAM_LOCAL_MUSIC:
  384. volume = audio_system->lcmusic_volume;
  385. break;
  386. case AUDIO_STREAM_FM:
  387. volume = audio_system->fm_volume;
  388. break;
  389. case AUDIO_STREAM_I2SRX_IN:
  390. volume = audio_system->i2srx_in_volume;
  391. break;
  392. case AUDIO_STREAM_SPDIF_IN:
  393. volume = audio_system->spidf_in_volume;
  394. break;
  395. default:
  396. volume = audio_system->master_volume;
  397. break;
  398. }
  399. return volume;
  400. }
  401. int audio_system_set_stream_pa_volume(int stream_type, int volume)
  402. {
  403. struct audio_track_t *audio_track = NULL;
  404. if (!audio_system)
  405. return -ESRCH;
  406. os_mutex_lock(&audio_system->audio_system_mutex, OS_FOREVER);
  407. for (int i = 0; i < MAX_AUDIO_TRACK_NUM; i++) {
  408. audio_track = audio_system->audio_track_pool[i];
  409. if (audio_track &&
  410. (audio_track->stream_type == stream_type
  411. || stream_type == AUDIO_STREAM_DEFAULT)) {
  412. audio_track_set_pa_volume(audio_track, volume);
  413. }
  414. }
  415. os_mutex_unlock(&audio_system->audio_system_mutex);
  416. return 0;
  417. }
  418. int audio_system_set_microphone_volume(int stream_type, int volume)
  419. {
  420. struct audio_record_t *audio_record = NULL;
  421. int ret = 0;
  422. if (!audio_system)
  423. return -ESRCH;
  424. for (int i = 0; i < MAX_AUDIO_RECORD_NUM; i++) {
  425. audio_record = audio_system->audio_record_pool[i];
  426. if (audio_record) {
  427. audio_record_set_volume(audio_record, volume);
  428. }
  429. }
  430. SYS_LOG_INF("volume: %d ret %d ok\n", volume, ret);
  431. return ret;
  432. }
  433. int audio_system_get_max_volume(void)
  434. {
  435. return audio_system->max_volume;
  436. }
  437. int audio_system_get_min_volume(void)
  438. {
  439. return audio_system->min_volume;
  440. }
  441. static struct audio_system_t global_audio_system;
  442. int aduio_system_init(void)
  443. {
  444. audio_system = &global_audio_system;
  445. memset(audio_system, 0, sizeof(struct audio_system_t));
  446. os_mutex_init(&audio_system->audio_system_mutex);
  447. _audio_system_get_volume_config(CFG_BTPLAY_VOLUME, &audio_system->music_volume);
  448. audio_system->master_volume = audio_system->music_volume;
  449. _audio_system_get_volume_config(CFG_TONE_VOLUME, &audio_system->tts_volume);
  450. _audio_system_get_volume_config(CFG_BTCALL_VOLUME, &audio_system->voice_volume);
  451. _audio_system_get_volume_config(CFG_LINEIN_VOLUME, &audio_system->linein_volume);
  452. _audio_system_get_volume_config(CFG_USOUND_VOLUME, &audio_system->usound_volume);
  453. _audio_system_get_volume_config(CFG_LOCALPLAY_VOLUME, &audio_system->lcmusic_volume);
  454. _audio_system_get_volume_config(CFG_FM_VOLUME, &audio_system->fm_volume);
  455. _audio_system_get_volume_config(CFG_I2SRX_IN_VOLUME, &audio_system->i2srx_in_volume);
  456. _audio_system_get_volume_config(CFG_SPDIF_IN_VOLUME, &audio_system->spidf_in_volume);
  457. _audio_system_get_volume_config(CFG_TTS_MIN_VOL, &audio_system->min_volume);
  458. _audio_system_get_volume_config(CFG_TTS_MAX_VOL, &audio_system->max_volume);
  459. /* audio_system_set_output_sample_rate(48); */
  460. #if CONFIG_AUDIO_ACTS
  461. /* init audio hal */
  462. hal_audio_out_init();
  463. hal_audio_in_init();
  464. #endif
  465. /* audio_flinger_init(); */
  466. return 0;
  467. }