PageRenderTime 117ms CodeModel.GetById 66ms RepoModel.GetById 6ms app.codeStats 1ms

/audio/audio_hw.c

https://gitlab.com/LiquidSmooth-Devices/android_device_samsung_n7100
C | 3078 lines | 2405 code | 461 blank | 212 comment | 472 complexity | 70f0ce432fdbda3eb3c8317ab9c347f5 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * Copyright (C) 2011 The Android Open Source Project
  3. * Copyright (C) 2012 Wolfson Microelectronics plc
  4. * Copyright (C) 2012 The CyanogenMod Project
  5. * Daniel Hillenbrand <codeworkx@cyanogenmod.com>
  6. * Guillaume "XpLoDWilD" Lesniak <xplodgui@gmail.com>
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #define LOG_TAG "audio_hw_primary"
  21. #define LOG_NDEBUG 0
  22. #include <errno.h>
  23. #include <pthread.h>
  24. #include <stdint.h>
  25. #include <sys/time.h>
  26. #include <stdlib.h>
  27. #include <expat.h>
  28. #include <cutils/log.h>
  29. #include <cutils/str_parms.h>
  30. #include <cutils/properties.h>
  31. #include <hardware/hardware.h>
  32. #include <system/audio.h>
  33. #include <hardware/audio.h>
  34. #include <tinyalsa/asoundlib.h>
  35. #include <audio_utils/resampler.h>
  36. #include <audio_utils/echo_reference.h>
  37. #include <hardware/audio_effect.h>
  38. #include <audio_effects/effect_aec.h>
  39. #include "audio_hw.h"
  40. #include "ril_interface.h"
  41. struct pcm_config pcm_config_mm = {
  42. .channels = 2,
  43. .rate = MM_FULL_POWER_SAMPLING_RATE,
  44. .period_size = DEEP_BUFFER_LONG_PERIOD_SIZE,
  45. .period_count = PLAYBACK_DEEP_BUFFER_LONG_PERIOD_COUNT,
  46. .format = PCM_FORMAT_S16_LE,
  47. };
  48. struct pcm_config pcm_config_tones = {
  49. .channels = 2,
  50. .rate = MM_FULL_POWER_SAMPLING_RATE,
  51. .period_size = SHORT_PERIOD_SIZE,
  52. .period_count = PLAYBACK_SHORT_PERIOD_COUNT,
  53. .format = PCM_FORMAT_S16_LE,
  54. .start_threshold = 0,
  55. .avail_min = 0,
  56. };
  57. struct pcm_config pcm_config_capture = {
  58. .channels = 2,
  59. .rate = DEFAULT_IN_SAMPLING_RATE,
  60. .period_size = CAPTURE_PERIOD_SIZE,
  61. .period_count = CAPTURE_PERIOD_COUNT,
  62. .format = PCM_FORMAT_S16_LE,
  63. };
  64. struct pcm_config pcm_config_vx = {
  65. .channels = 2,
  66. .rate = VX_WB_SAMPLING_RATE,
  67. .period_size = 160,
  68. .period_count = 2,
  69. .format = PCM_FORMAT_S16_LE,
  70. };
  71. #define MIN(x, y) ((x) > (y) ? (y) : (x))
  72. struct m0_audio_device {
  73. struct audio_hw_device hw_device;
  74. pthread_mutex_t lock; /* see note below on mutex acquisition order */
  75. struct m0_dev_cfg *dev_cfgs;
  76. int num_dev_cfgs;
  77. struct mixer *mixer;
  78. audio_mode_t mode;
  79. int active_out_device;
  80. int out_device;
  81. int active_in_device;
  82. int in_device;
  83. struct pcm *pcm_modem_dl;
  84. struct pcm *pcm_modem_ul;
  85. struct pcm *pcm_bt_dl;
  86. struct pcm *pcm_bt_ul;
  87. int in_call;
  88. float voice_volume;
  89. struct m0_stream_in *active_input;
  90. struct m0_stream_out *outputs[OUTPUT_TOTAL];
  91. bool mic_mute;
  92. struct echo_reference_itfe *echo_reference;
  93. bool bluetooth_nrec;
  94. int wb_amr;
  95. bool screen_off;
  96. /* RIL */
  97. struct ril_handle ril;
  98. };
  99. struct m0_stream_out {
  100. struct audio_stream_out stream;
  101. pthread_mutex_t lock; /* see note below on mutex acquisition order */
  102. struct pcm_config config[PCM_TOTAL];
  103. struct pcm *pcm[PCM_TOTAL];
  104. struct resampler_itfe *resampler;
  105. char *buffer;
  106. size_t buffer_frames;
  107. int standby;
  108. struct echo_reference_itfe *echo_reference;
  109. int write_threshold;
  110. bool use_long_periods;
  111. audio_channel_mask_t channel_mask;
  112. audio_channel_mask_t sup_channel_masks[3];
  113. struct m0_audio_device *dev;
  114. };
  115. #define MAX_PREPROCESSORS 3 /* maximum one AGC + one NS + one AEC per input stream */
  116. struct effect_info_s {
  117. effect_handle_t effect_itfe;
  118. size_t num_channel_configs;
  119. channel_config_t* channel_configs;
  120. };
  121. #define NUM_IN_AUX_CNL_CONFIGS 2
  122. channel_config_t in_aux_cnl_configs[NUM_IN_AUX_CNL_CONFIGS] = {
  123. { AUDIO_CHANNEL_IN_FRONT , AUDIO_CHANNEL_IN_BACK},
  124. { AUDIO_CHANNEL_IN_STEREO , AUDIO_CHANNEL_IN_RIGHT}
  125. };
  126. struct m0_stream_in {
  127. struct audio_stream_in stream;
  128. pthread_mutex_t lock; /* see note below on mutex acquisition order */
  129. struct pcm_config config;
  130. struct pcm *pcm;
  131. int device;
  132. struct resampler_itfe *resampler;
  133. struct resampler_buffer_provider buf_provider;
  134. unsigned int requested_rate;
  135. int standby;
  136. int source;
  137. struct echo_reference_itfe *echo_reference;
  138. bool need_echo_reference;
  139. int16_t *read_buf;
  140. size_t read_buf_size;
  141. size_t read_buf_frames;
  142. int16_t *proc_buf_in;
  143. int16_t *proc_buf_out;
  144. size_t proc_buf_size;
  145. size_t proc_buf_frames;
  146. int16_t *ref_buf;
  147. size_t ref_buf_size;
  148. size_t ref_buf_frames;
  149. int read_status;
  150. int num_preprocessors;
  151. struct effect_info_s preprocessors[MAX_PREPROCESSORS];
  152. bool aux_channels_changed;
  153. uint32_t main_channels;
  154. uint32_t aux_channels;
  155. struct m0_audio_device *dev;
  156. };
  157. struct m0_dev_cfg {
  158. int mask;
  159. struct route_setting *on;
  160. unsigned int on_len;
  161. struct route_setting *off;
  162. unsigned int off_len;
  163. };
  164. /**
  165. * NOTE: when multiple mutexes have to be acquired, always respect the following order:
  166. * hw device > in stream > out stream
  167. */
  168. static void select_output_device(struct m0_audio_device *adev);
  169. static void select_input_device(struct m0_audio_device *adev);
  170. static int adev_set_voice_volume(struct audio_hw_device *dev, float volume);
  171. static int do_input_standby(struct m0_stream_in *in);
  172. static int do_output_standby(struct m0_stream_out *out);
  173. static void in_update_aux_channels(struct m0_stream_in *in, effect_handle_t effect);
  174. /* The enable flag when 0 makes the assumption that enums are disabled by
  175. * "Off" and integers/booleans by 0 */
  176. static int set_bigroute_by_array(struct mixer *mixer, struct route_setting *route,
  177. int enable)
  178. {
  179. struct mixer_ctl *ctl;
  180. unsigned int i, j, ret;
  181. /* Go through the route array and set each value */
  182. i = 0;
  183. while (route[i].ctl_name) {
  184. ctl = mixer_get_ctl_by_name(mixer, route[i].ctl_name);
  185. if (!ctl) {
  186. ALOGE("Unknown control '%s'\n", route[i].ctl_name);
  187. return -EINVAL;
  188. }
  189. if (route[i].strval) {
  190. if (enable) {
  191. ret = mixer_ctl_set_enum_by_string(ctl, route[i].strval);
  192. if (ret != 0) {
  193. ALOGE("Failed to set '%s' to '%s'\n", route[i].ctl_name, route[i].strval);
  194. } else {
  195. ALOGV("Set '%s' to '%s'\n", route[i].ctl_name, route[i].strval);
  196. }
  197. } else {
  198. ret = mixer_ctl_set_enum_by_string(ctl, "Off");
  199. if (ret != 0) {
  200. ALOGE("Failed to set '%s' to '%s'\n", route[i].ctl_name, route[i].strval);
  201. } else {
  202. ALOGV("Set '%s' to '%s'\n", route[i].ctl_name, "Off");
  203. }
  204. }
  205. } else {
  206. /* This ensures multiple (i.e. stereo) values are set jointly */
  207. for (j = 0; j < mixer_ctl_get_num_values(ctl); j++) {
  208. if (enable) {
  209. ret = mixer_ctl_set_value(ctl, j, route[i].intval);
  210. if (ret != 0) {
  211. ALOGE("Failed to set '%s' to '%d'\n", route[i].ctl_name, route[i].intval);
  212. } else {
  213. ALOGV("Set '%s' to '%d'\n", route[i].ctl_name, route[i].intval);
  214. }
  215. } else {
  216. ret = mixer_ctl_set_value(ctl, j, 0);
  217. if (ret != 0) {
  218. ALOGE("Failed to set '%s' to '%d'\n", route[i].ctl_name, route[i].intval);
  219. } else {
  220. ALOGV("Set '%s' to '%d'\n", route[i].ctl_name, 0);
  221. }
  222. }
  223. }
  224. }
  225. i++;
  226. }
  227. return 0;
  228. }
  229. /* The enable flag when 0 makes the assumption that enums are disabled by
  230. * "Off" and integers/booleans by 0 */
  231. static int set_route_by_array(struct mixer *mixer, struct route_setting *route,
  232. unsigned int len)
  233. {
  234. struct mixer_ctl *ctl;
  235. unsigned int i, j, ret;
  236. /* Go through the route array and set each value */
  237. for (i = 0; i < len; i++) {
  238. ctl = mixer_get_ctl_by_name(mixer, route[i].ctl_name);
  239. if (!ctl) {
  240. ALOGE("Unknown control '%s'\n", route[i].ctl_name);
  241. return -EINVAL;
  242. }
  243. if (route[i].strval) {
  244. ret = mixer_ctl_set_enum_by_string(ctl, route[i].strval);
  245. if (ret != 0) {
  246. ALOGE("Failed to set '%s' to '%s'\n",
  247. route[i].ctl_name, route[i].strval);
  248. } else {
  249. ALOGV("Set '%s' to '%s'\n",
  250. route[i].ctl_name, route[i].strval);
  251. }
  252. } else {
  253. /* This ensures multiple (i.e. stereo) values are set jointly */
  254. for (j = 0; j < mixer_ctl_get_num_values(ctl); j++) {
  255. ret = mixer_ctl_set_value(ctl, j, route[i].intval);
  256. if (ret != 0) {
  257. ALOGE("Failed to set '%s'.%d to %d\n",
  258. route[i].ctl_name, j, route[i].intval);
  259. } else {
  260. ALOGV("Set '%s'.%d to %d\n",
  261. route[i].ctl_name, j, route[i].intval);
  262. }
  263. }
  264. }
  265. }
  266. return 0;
  267. }
  268. /* Must be called with lock */
  269. void select_devices(struct m0_audio_device *adev)
  270. {
  271. int i;
  272. if (adev->active_out_device == adev->out_device && adev->active_in_device == adev->in_device)
  273. return;
  274. ALOGV("Changing output device %x => %x\n", adev->active_out_device, adev->out_device);
  275. ALOGV("Changing input device %x => %x\n", adev->active_in_device, adev->in_device);
  276. /* Turn on new devices first so we don't glitch due to powerdown... */
  277. for (i = 0; i < adev->num_dev_cfgs; i++)
  278. if ((adev->out_device & adev->dev_cfgs[i].mask) &&
  279. !(adev->active_out_device & adev->dev_cfgs[i].mask) &&
  280. !(adev->dev_cfgs[i].mask & AUDIO_DEVICE_BIT_IN))
  281. set_route_by_array(adev->mixer, adev->dev_cfgs[i].on,
  282. adev->dev_cfgs[i].on_len);
  283. for (i = 0; i < adev->num_dev_cfgs; i++)
  284. if ((adev->in_device & adev->dev_cfgs[i].mask) &&
  285. !(adev->active_in_device & adev->dev_cfgs[i].mask) &&
  286. (adev->dev_cfgs[i].mask & AUDIO_DEVICE_BIT_IN))
  287. set_route_by_array(adev->mixer, adev->dev_cfgs[i].on,
  288. adev->dev_cfgs[i].on_len);
  289. /* ...then disable old ones. */
  290. for (i = 0; i < adev->num_dev_cfgs; i++)
  291. if (!(adev->out_device & adev->dev_cfgs[i].mask) &&
  292. (adev->active_out_device & adev->dev_cfgs[i].mask) &&
  293. !(adev->dev_cfgs[i].mask & AUDIO_DEVICE_BIT_IN))
  294. set_route_by_array(adev->mixer, adev->dev_cfgs[i].off,
  295. adev->dev_cfgs[i].off_len);
  296. for (i = 0; i < adev->num_dev_cfgs; i++)
  297. if (!(adev->in_device & adev->dev_cfgs[i].mask) &&
  298. (adev->active_in_device & adev->dev_cfgs[i].mask) &&
  299. (adev->dev_cfgs[i].mask & AUDIO_DEVICE_BIT_IN))
  300. set_route_by_array(adev->mixer, adev->dev_cfgs[i].off,
  301. adev->dev_cfgs[i].off_len);
  302. adev->active_out_device = adev->out_device;
  303. adev->active_in_device = adev->in_device;
  304. }
  305. static int start_call(struct m0_audio_device *adev)
  306. {
  307. ALOGV("Opening modem PCMs");
  308. int bt_on;
  309. bt_on = adev->out_device & AUDIO_DEVICE_OUT_ALL_SCO;
  310. if (bt_on) {
  311. /* use amr-nb for bluetooth */
  312. pcm_config_vx.rate = VX_NB_SAMPLING_RATE;
  313. } else {
  314. pcm_config_vx.rate = adev->wb_amr ? VX_WB_SAMPLING_RATE : VX_NB_SAMPLING_RATE;
  315. }
  316. /* Open modem PCM channels */
  317. if (adev->pcm_modem_dl == NULL) {
  318. ALOGD("Opening PCM modem DL stream");
  319. adev->pcm_modem_dl = pcm_open(CARD_DEFAULT, PORT_MODEM, PCM_OUT, &pcm_config_vx);
  320. if (!pcm_is_ready(adev->pcm_modem_dl)) {
  321. ALOGE("cannot open PCM modem DL stream: %s", pcm_get_error(adev->pcm_modem_dl));
  322. goto err_open_dl;
  323. }
  324. }
  325. if (adev->pcm_modem_ul == NULL) {
  326. ALOGD("Opening PCM modem UL stream");
  327. adev->pcm_modem_ul = pcm_open(CARD_DEFAULT, PORT_MODEM, PCM_IN, &pcm_config_vx);
  328. if (!pcm_is_ready(adev->pcm_modem_ul)) {
  329. ALOGE("cannot open PCM modem UL stream: %s", pcm_get_error(adev->pcm_modem_ul));
  330. goto err_open_ul;
  331. }
  332. }
  333. ALOGD("Starting PCM modem streams");
  334. pcm_start(adev->pcm_modem_dl);
  335. pcm_start(adev->pcm_modem_ul);
  336. /* Open bluetooth PCM channels */
  337. if (bt_on) {
  338. ALOGV("Opening bluetooth PCMs");
  339. if (adev->pcm_bt_dl == NULL) {
  340. ALOGD("Opening PCM bluetooth DL stream");
  341. adev->pcm_bt_dl = pcm_open(CARD_DEFAULT, PORT_BT, PCM_OUT, &pcm_config_vx);
  342. if (!pcm_is_ready(adev->pcm_bt_dl)) {
  343. ALOGE("cannot open PCM bluetooth DL stream: %s", pcm_get_error(adev->pcm_bt_dl));
  344. goto err_open_dl;
  345. }
  346. }
  347. if (adev->pcm_bt_ul == NULL) {
  348. ALOGD("Opening PCM bluetooth UL stream");
  349. adev->pcm_bt_ul = pcm_open(CARD_DEFAULT, PORT_BT, PCM_IN, &pcm_config_vx);
  350. if (!pcm_is_ready(adev->pcm_bt_ul)) {
  351. ALOGE("cannot open PCM bluetooth UL stream: %s", pcm_get_error(adev->pcm_bt_ul));
  352. goto err_open_ul;
  353. }
  354. }
  355. ALOGD("Starting PCM bluetooth streams");
  356. pcm_start(adev->pcm_bt_dl);
  357. pcm_start(adev->pcm_bt_ul);
  358. }
  359. return 0;
  360. err_open_ul:
  361. pcm_close(adev->pcm_modem_ul);
  362. adev->pcm_modem_ul = NULL;
  363. pcm_close(adev->pcm_bt_ul);
  364. adev->pcm_bt_ul = NULL;
  365. err_open_dl:
  366. pcm_close(adev->pcm_modem_dl);
  367. adev->pcm_modem_dl = NULL;
  368. pcm_close(adev->pcm_bt_dl);
  369. adev->pcm_bt_dl = NULL;
  370. return -ENOMEM;
  371. }
  372. static void end_call(struct m0_audio_device *adev)
  373. {
  374. int bt_on;
  375. bt_on = adev->out_device & AUDIO_DEVICE_OUT_ALL_SCO;
  376. if (adev->pcm_modem_dl != NULL) {
  377. ALOGD("Stopping modem DL PCM");
  378. pcm_stop(adev->pcm_modem_dl);
  379. ALOGV("Closing modem DL PCM");
  380. pcm_close(adev->pcm_modem_dl);
  381. }
  382. if (adev->pcm_modem_ul != NULL) {
  383. ALOGD("Stopping modem UL PCM");
  384. pcm_stop(adev->pcm_modem_ul);
  385. ALOGV("Closing modem UL PCM");
  386. pcm_close(adev->pcm_modem_ul);
  387. }
  388. adev->pcm_modem_dl = NULL;
  389. adev->pcm_modem_ul = NULL;
  390. if (bt_on) {
  391. if (adev->pcm_bt_dl != NULL) {
  392. ALOGD("Stopping bluetooth DL PCM");
  393. pcm_stop(adev->pcm_bt_dl);
  394. ALOGV("Closing bluetooth DL PCM");
  395. pcm_close(adev->pcm_bt_dl);
  396. }
  397. if (adev->pcm_bt_ul != NULL) {
  398. ALOGD("Stopping bluetooth UL PCM");
  399. pcm_stop(adev->pcm_bt_ul);
  400. ALOGV("Closing bluetooth UL PCM");
  401. pcm_close(adev->pcm_bt_ul);
  402. }
  403. }
  404. adev->pcm_bt_dl = NULL;
  405. adev->pcm_bt_ul = NULL;
  406. }
  407. static void set_eq_filter(struct m0_audio_device *adev)
  408. {
  409. }
  410. void audio_set_wb_amr_callback(void *data, int enable)
  411. {
  412. struct m0_audio_device *adev = (struct m0_audio_device *)data;
  413. pthread_mutex_lock(&adev->lock);
  414. if (adev->wb_amr != enable) {
  415. adev->wb_amr = enable;
  416. /* reopen the modem PCMs at the new rate */
  417. if (adev->in_call) {
  418. end_call(adev);
  419. select_output_device(adev);
  420. start_call(adev);
  421. }
  422. }
  423. pthread_mutex_unlock(&adev->lock);
  424. }
  425. static void set_incall_device(struct m0_audio_device *adev)
  426. {
  427. int device_type;
  428. switch(adev->out_device) {
  429. case AUDIO_DEVICE_OUT_EARPIECE:
  430. device_type = SOUND_AUDIO_PATH_HANDSET;
  431. break;
  432. case AUDIO_DEVICE_OUT_SPEAKER:
  433. case AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET:
  434. case AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET:
  435. case AUDIO_DEVICE_OUT_AUX_DIGITAL:
  436. device_type = SOUND_AUDIO_PATH_SPEAKER;
  437. break;
  438. case AUDIO_DEVICE_OUT_WIRED_HEADSET:
  439. device_type = SOUND_AUDIO_PATH_HEADSET;
  440. break;
  441. case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
  442. device_type = SOUND_AUDIO_PATH_HEADPHONE;
  443. break;
  444. case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
  445. case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
  446. case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
  447. if (adev->bluetooth_nrec) {
  448. device_type = SOUND_AUDIO_PATH_BLUETOOTH;
  449. } else {
  450. device_type = SOUND_AUDIO_PATH_BLUETOOTH_NO_NR;
  451. }
  452. break;
  453. default:
  454. device_type = SOUND_AUDIO_PATH_HANDSET;
  455. break;
  456. }
  457. /* if output device isn't supported, open modem side to handset by default */
  458. ALOGE("%s: ril_set_call_audio_path(%d)", __func__, device_type);
  459. ril_set_call_audio_path(&adev->ril, device_type);
  460. }
  461. static void force_all_standby(struct m0_audio_device *adev)
  462. {
  463. struct m0_stream_in *in;
  464. struct m0_stream_out *out;
  465. /* only needed for low latency output streams as other streams are not used
  466. * for voice use cases */
  467. if (adev->outputs[OUTPUT_LOW_LATENCY] != NULL &&
  468. !adev->outputs[OUTPUT_LOW_LATENCY]->standby) {
  469. out = adev->outputs[OUTPUT_LOW_LATENCY];
  470. pthread_mutex_lock(&out->lock);
  471. do_output_standby(out);
  472. pthread_mutex_unlock(&out->lock);
  473. }
  474. if (adev->active_input) {
  475. in = adev->active_input;
  476. pthread_mutex_lock(&in->lock);
  477. do_input_standby(in);
  478. pthread_mutex_unlock(&in->lock);
  479. }
  480. }
  481. static void select_mode(struct m0_audio_device *adev)
  482. {
  483. if (adev->mode == AUDIO_MODE_IN_CALL) {
  484. ALOGE("Entering IN_CALL state, in_call=%d", adev->in_call);
  485. if (!adev->in_call) {
  486. force_all_standby(adev);
  487. /* force earpiece route for in call state if speaker is the
  488. only currently selected route. This prevents having to tear
  489. down the modem PCMs to change route from speaker to earpiece
  490. after the ringtone is played, but doesn't cause a route
  491. change if a headset or bt device is already connected. If
  492. speaker is not the only thing active, just remove it from
  493. the route. We'll assume it'll never be used initally during
  494. a call. This works because we're sure that the audio policy
  495. manager will update the output device after the audio mode
  496. change, even if the device selection did not change. */
  497. if (adev->out_device == AUDIO_DEVICE_OUT_SPEAKER) {
  498. adev->out_device = AUDIO_DEVICE_OUT_EARPIECE;
  499. adev->in_device = AUDIO_DEVICE_IN_BUILTIN_MIC & ~AUDIO_DEVICE_BIT_IN;
  500. } else
  501. adev->out_device &= ~AUDIO_DEVICE_OUT_SPEAKER;
  502. select_output_device(adev);
  503. start_call(adev);
  504. ril_set_call_clock_sync(&adev->ril, SOUND_CLOCK_START);
  505. adev_set_voice_volume(&adev->hw_device, adev->voice_volume);
  506. adev->in_call = 1;
  507. }
  508. } else {
  509. ALOGE("Leaving IN_CALL state, in_call=%d, mode=%d",
  510. adev->in_call, adev->mode);
  511. if (adev->in_call) {
  512. adev->in_call = 0;
  513. ril_set_call_clock_sync(&adev->ril, SOUND_CLOCK_STOP);
  514. end_call(adev);
  515. force_all_standby(adev);
  516. ALOGD("%s: set voicecall route: voicecall_default_disable", __func__);
  517. set_bigroute_by_array(adev->mixer, voicecall_default_disable, 1);
  518. ALOGD("%s: set voicecall route: default_input_disable", __func__);
  519. set_bigroute_by_array(adev->mixer, default_input_disable, 1);
  520. ALOGD("%s: set voicecall route: headset_input_disable", __func__);
  521. set_bigroute_by_array(adev->mixer, headset_input_disable, 1);
  522. ALOGD("%s: set voicecall route: bt_disable", __func__);
  523. set_bigroute_by_array(adev->mixer, bt_disable, 1);
  524. select_output_device(adev);
  525. //Force Input Standby
  526. adev->in_device = AUDIO_DEVICE_NONE;
  527. select_input_device(adev);
  528. }
  529. }
  530. }
  531. static void select_output_device(struct m0_audio_device *adev)
  532. {
  533. int headset_on;
  534. int headphone_on;
  535. int speaker_on;
  536. int earpiece_on;
  537. int bt_on;
  538. unsigned int channel;
  539. headset_on = adev->out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET;
  540. headphone_on = adev->out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
  541. speaker_on = adev->out_device & AUDIO_DEVICE_OUT_SPEAKER;
  542. earpiece_on = adev->out_device & AUDIO_DEVICE_OUT_EARPIECE;
  543. bt_on = adev->out_device & AUDIO_DEVICE_OUT_ALL_SCO;
  544. switch(adev->out_device) {
  545. case AUDIO_DEVICE_OUT_SPEAKER:
  546. ALOGD("%s: AUDIO_DEVICE_OUT_SPEAKER", __func__);
  547. break;
  548. case AUDIO_DEVICE_OUT_WIRED_HEADSET:
  549. ALOGD("%s: AUDIO_DEVICE_OUT_WIRED_HEADSET", __func__);
  550. break;
  551. case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
  552. ALOGD("%s: AUDIO_DEVICE_OUT_WIRED_HEADPHONE", __func__);
  553. break;
  554. case AUDIO_DEVICE_OUT_EARPIECE:
  555. ALOGD("%s: AUDIO_DEVICE_OUT_EARPIECE", __func__);
  556. break;
  557. case AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET:
  558. ALOGD("%s: AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET", __func__);
  559. break;
  560. case AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET:
  561. ALOGD("%s: AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET", __func__);
  562. break;
  563. case AUDIO_DEVICE_OUT_ALL_SCO:
  564. ALOGD("%s: AUDIO_DEVICE_OUT_ALL_SCO", __func__);
  565. break;
  566. case AUDIO_DEVICE_OUT_USB_ACCESSORY:
  567. ALOGD("%s: AUDIO_DEVICE_OUT_USB_ACCESSORY", __func__);
  568. break;
  569. case AUDIO_DEVICE_OUT_USB_DEVICE:
  570. ALOGD("%s: AUDIO_DEVICE_OUT_USB_DEVICE", __func__);
  571. break;
  572. default:
  573. ALOGD("%s: AUDIO_DEVICE_OUT_ALL", __func__);
  574. break;
  575. }
  576. select_devices(adev);
  577. set_eq_filter(adev);
  578. if (adev->mode == AUDIO_MODE_IN_CALL) {
  579. if (headset_on || headphone_on || speaker_on || earpiece_on) {
  580. ALOGD("%s: set voicecall route: voicecall_default", __func__);
  581. set_bigroute_by_array(adev->mixer, voicecall_default, 1);
  582. } else {
  583. ALOGD("%s: set voicecall route: voicecall_default_disable", __func__);
  584. set_bigroute_by_array(adev->mixer, voicecall_default_disable, 1);
  585. }
  586. if (speaker_on || earpiece_on || headphone_on) {
  587. ALOGD("%s: set voicecall route: default_input", __func__);
  588. set_bigroute_by_array(adev->mixer, default_input, 1);
  589. } else {
  590. ALOGD("%s: set voicecall route: default_input_disable", __func__);
  591. set_bigroute_by_array(adev->mixer, default_input_disable, 1);
  592. }
  593. if (headset_on) {
  594. ALOGD("%s: set voicecall route: headset_input", __func__);
  595. set_bigroute_by_array(adev->mixer, headset_input, 1);
  596. } else {
  597. ALOGD("%s: set voicecall route: headset_input_disable", __func__);
  598. set_bigroute_by_array(adev->mixer, headset_input_disable, 1);
  599. }
  600. if (bt_on) {
  601. ALOGD("%s: set voicecall route: bt_input", __func__);
  602. set_bigroute_by_array(adev->mixer, bt_input, 1);
  603. ALOGD("%s: set voicecall route: bt_output", __func__);
  604. set_bigroute_by_array(adev->mixer, bt_output, 1);
  605. } else {
  606. ALOGD("%s: set voicecall route: bt_disable", __func__);
  607. set_bigroute_by_array(adev->mixer, bt_disable, 1);
  608. }
  609. set_incall_device(adev);
  610. }
  611. }
  612. static void select_input_device(struct m0_audio_device *adev)
  613. {
  614. int input_device = AUDIO_DEVICE_BIT_IN | adev->in_device;
  615. switch(input_device) {
  616. case AUDIO_DEVICE_IN_BUILTIN_MIC:
  617. ALOGD("%s: AUDIO_DEVICE_IN_BUILTIN_MIC", __func__);
  618. break;
  619. case AUDIO_DEVICE_IN_BACK_MIC:
  620. // Force use both mics for video recording
  621. adev->in_device = (AUDIO_DEVICE_IN_BACK_MIC | AUDIO_DEVICE_IN_BUILTIN_MIC) & ~AUDIO_DEVICE_BIT_IN;
  622. ALOGD("%s: AUDIO_DEVICE_IN_BACK_MIC and AUDIO_DEVICE_IN_BUILTIN_MIC", __func__);
  623. break;
  624. case AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET:
  625. ALOGD("%s: AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET", __func__);
  626. break;
  627. case AUDIO_DEVICE_IN_WIRED_HEADSET:
  628. ALOGD("%s: AUDIO_DEVICE_IN_WIRED_HEADSET", __func__);
  629. break;
  630. default:
  631. break;
  632. }
  633. select_devices(adev);
  634. }
  635. /* must be called with hw device and output stream mutexes locked */
  636. static int start_output_stream_low_latency(struct m0_stream_out *out)
  637. {
  638. struct m0_audio_device *adev = out->dev;
  639. unsigned int flags = PCM_OUT;
  640. int i;
  641. bool success = true;
  642. if (adev->mode != AUDIO_MODE_IN_CALL) {
  643. select_output_device(adev);
  644. }
  645. /* default to low power: will be corrected in out_write if necessary before first write to
  646. * tinyalsa.
  647. */
  648. if (adev->out_device & ~(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET | AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
  649. /* Something not a dock in use */
  650. out->config[PCM_NORMAL] = pcm_config_tones;
  651. out->config[PCM_NORMAL].rate = MM_FULL_POWER_SAMPLING_RATE;
  652. out->pcm[PCM_NORMAL] = pcm_open(CARD_DEFAULT, PORT_PLAYBACK,
  653. flags, &out->config[PCM_NORMAL]);
  654. }
  655. if (adev->out_device & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) {
  656. /* SPDIF output in use */
  657. out->config[PCM_SPDIF] = pcm_config_tones;
  658. out->config[PCM_SPDIF].rate = MM_FULL_POWER_SAMPLING_RATE;
  659. out->pcm[PCM_SPDIF] = pcm_open(CARD_DEFAULT, PORT_PLAYBACK,
  660. flags, &out->config[PCM_SPDIF]);
  661. }
  662. /* Close any PCMs that could not be opened properly and return an error */
  663. for (i = 0; i < PCM_TOTAL; i++) {
  664. if (out->pcm[i] && !pcm_is_ready(out->pcm[i])) {
  665. ALOGE("%s: cannot open pcm_out driver %d: %s", __func__ , i, pcm_get_error(out->pcm[i]));
  666. pcm_close(out->pcm[i]);
  667. out->pcm[i] = NULL;
  668. success = false;
  669. }
  670. }
  671. if (success) {
  672. out->buffer_frames = pcm_config_tones.period_size * 2;
  673. if (out->buffer == NULL)
  674. out->buffer = malloc(out->buffer_frames * audio_stream_frame_size(&out->stream.common));
  675. if (adev->echo_reference != NULL)
  676. out->echo_reference = adev->echo_reference;
  677. out->resampler->reset(out->resampler);
  678. return 0;
  679. }
  680. return -ENOMEM;
  681. }
  682. /* must be called with hw device and output stream mutexes locked */
  683. static int start_output_stream_deep_buffer(struct m0_stream_out *out)
  684. {
  685. struct m0_audio_device *adev = out->dev;
  686. if (adev->mode != AUDIO_MODE_IN_CALL) {
  687. select_output_device(adev);
  688. }
  689. out->write_threshold = PLAYBACK_DEEP_BUFFER_LONG_PERIOD_COUNT * DEEP_BUFFER_LONG_PERIOD_SIZE;
  690. out->use_long_periods = true;
  691. out->config[PCM_NORMAL] = pcm_config_mm;
  692. out->config[PCM_NORMAL].rate = MM_FULL_POWER_SAMPLING_RATE;
  693. out->pcm[PCM_NORMAL] = pcm_open(CARD_DEFAULT, PORT_PLAYBACK,
  694. PCM_OUT | PCM_MMAP | PCM_NOIRQ, &out->config[PCM_NORMAL]);
  695. if (out->pcm[PCM_NORMAL] && !pcm_is_ready(out->pcm[PCM_NORMAL])) {
  696. ALOGE("%s: cannot open pcm_out driver: %s", __func__, pcm_get_error(out->pcm[PCM_NORMAL]));
  697. pcm_close(out->pcm[PCM_NORMAL]);
  698. out->pcm[PCM_NORMAL] = NULL;
  699. return -ENOMEM;
  700. }
  701. out->buffer_frames = DEEP_BUFFER_SHORT_PERIOD_SIZE * 2;
  702. if (out->buffer == NULL)
  703. out->buffer = malloc(PLAYBACK_DEEP_BUFFER_LONG_PERIOD_COUNT * DEEP_BUFFER_LONG_PERIOD_SIZE);
  704. return 0;
  705. }
  706. static int check_input_parameters(uint32_t sample_rate, audio_format_t format, int channel_count)
  707. {
  708. if (format != AUDIO_FORMAT_PCM_16_BIT)
  709. return -EINVAL;
  710. if ((channel_count < 1) || (channel_count > 2))
  711. return -EINVAL;
  712. switch(sample_rate) {
  713. case 8000:
  714. case 11025:
  715. case 16000:
  716. case 22050:
  717. case 24000:
  718. case 32000:
  719. case 44100:
  720. case 48000:
  721. break;
  722. default:
  723. return -EINVAL;
  724. }
  725. return 0;
  726. }
  727. static size_t get_input_buffer_size(uint32_t sample_rate, audio_format_t format, int channel_count)
  728. {
  729. size_t size;
  730. size_t device_rate;
  731. if (check_input_parameters(sample_rate, format, channel_count) != 0)
  732. return 0;
  733. /* take resampling into account and return the closest majoring
  734. multiple of 16 frames, as audioflinger expects audio buffers to
  735. be a multiple of 16 frames */
  736. size = (pcm_config_capture.period_size * sample_rate) / pcm_config_capture.rate;
  737. size = ((size + 15) / 16) * 16;
  738. return size * channel_count * sizeof(short);
  739. }
  740. static void add_echo_reference(struct m0_stream_out *out,
  741. struct echo_reference_itfe *reference)
  742. {
  743. pthread_mutex_lock(&out->lock);
  744. out->echo_reference = reference;
  745. pthread_mutex_unlock(&out->lock);
  746. }
  747. static void remove_echo_reference(struct m0_stream_out *out,
  748. struct echo_reference_itfe *reference)
  749. {
  750. pthread_mutex_lock(&out->lock);
  751. if (out->echo_reference == reference) {
  752. /* stop writing to echo reference */
  753. reference->write(reference, NULL);
  754. out->echo_reference = NULL;
  755. }
  756. pthread_mutex_unlock(&out->lock);
  757. }
  758. static void put_echo_reference(struct m0_audio_device *adev,
  759. struct echo_reference_itfe *reference)
  760. {
  761. if (adev->echo_reference != NULL &&
  762. reference == adev->echo_reference) {
  763. /* echo reference is taken from the low latency output stream used
  764. * for voice use cases */
  765. if (adev->outputs[OUTPUT_LOW_LATENCY] != NULL &&
  766. !adev->outputs[OUTPUT_LOW_LATENCY]->standby)
  767. remove_echo_reference(adev->outputs[OUTPUT_LOW_LATENCY], reference);
  768. release_echo_reference(reference);
  769. adev->echo_reference = NULL;
  770. }
  771. }
  772. static struct echo_reference_itfe *get_echo_reference(struct m0_audio_device *adev,
  773. audio_format_t format,
  774. uint32_t channel_count,
  775. uint32_t sampling_rate)
  776. {
  777. put_echo_reference(adev, adev->echo_reference);
  778. /* echo reference is taken from the low latency output stream used
  779. * for voice use cases */
  780. if (adev->outputs[OUTPUT_LOW_LATENCY] != NULL &&
  781. !adev->outputs[OUTPUT_LOW_LATENCY]->standby) {
  782. struct audio_stream *stream =
  783. &adev->outputs[OUTPUT_LOW_LATENCY]->stream.common;
  784. uint32_t wr_channel_count = popcount(stream->get_channels(stream));
  785. uint32_t wr_sampling_rate = stream->get_sample_rate(stream);
  786. int status = create_echo_reference(AUDIO_FORMAT_PCM_16_BIT,
  787. channel_count,
  788. sampling_rate,
  789. AUDIO_FORMAT_PCM_16_BIT,
  790. wr_channel_count,
  791. wr_sampling_rate,
  792. &adev->echo_reference);
  793. if (status == 0)
  794. add_echo_reference(adev->outputs[OUTPUT_LOW_LATENCY],
  795. adev->echo_reference);
  796. }
  797. return adev->echo_reference;
  798. }
  799. static int get_playback_delay(struct m0_stream_out *out,
  800. size_t frames,
  801. struct echo_reference_buffer *buffer)
  802. {
  803. size_t kernel_frames;
  804. int status;
  805. int primary_pcm = 0;
  806. /* Find the first active PCM to act as primary */
  807. while ((primary_pcm < PCM_TOTAL) && !out->pcm[primary_pcm])
  808. primary_pcm++;
  809. status = pcm_get_htimestamp(out->pcm[primary_pcm], &kernel_frames, &buffer->time_stamp);
  810. if (status < 0) {
  811. buffer->time_stamp.tv_sec = 0;
  812. buffer->time_stamp.tv_nsec = 0;
  813. buffer->delay_ns = 0;
  814. ALOGV("%s: pcm_get_htimestamp error,"
  815. "setting playbackTimestamp to 0", __func__);
  816. return status;
  817. }
  818. kernel_frames = pcm_get_buffer_size(out->pcm[primary_pcm]) - kernel_frames;
  819. /* adjust render time stamp with delay added by current driver buffer.
  820. * Add the duration of current frame as we want the render time of the last
  821. * sample being written. */
  822. buffer->delay_ns = (long)(((int64_t)(kernel_frames + frames)* 1000000000)/
  823. MM_FULL_POWER_SAMPLING_RATE);
  824. return 0;
  825. }
  826. static uint32_t out_get_sample_rate(const struct audio_stream *stream)
  827. {
  828. return DEFAULT_OUT_SAMPLING_RATE;
  829. }
  830. static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
  831. {
  832. return 0;
  833. }
  834. static size_t out_get_buffer_size_low_latency(const struct audio_stream *stream)
  835. {
  836. struct m0_stream_out *out = (struct m0_stream_out *)stream;
  837. /* take resampling into account and return the closest majoring
  838. multiple of 16 frames, as audioflinger expects audio buffers to
  839. be a multiple of 16 frames. Note: we use the default rate here
  840. from pcm_config_tones.rate. */
  841. size_t size = (SHORT_PERIOD_SIZE * DEFAULT_OUT_SAMPLING_RATE) / pcm_config_tones.rate;
  842. size = ((size + 15) / 16) * 16;
  843. return size * audio_stream_frame_size((struct audio_stream *)stream);
  844. }
  845. static size_t out_get_buffer_size_deep_buffer(const struct audio_stream *stream)
  846. {
  847. struct m0_stream_out *out = (struct m0_stream_out *)stream;
  848. /* take resampling into account and return the closest majoring
  849. multiple of 16 frames, as audioflinger expects audio buffers to
  850. be a multiple of 16 frames. Note: we use the default rate here
  851. from pcm_config_mm.rate. */
  852. size_t size = (DEEP_BUFFER_SHORT_PERIOD_SIZE * DEFAULT_OUT_SAMPLING_RATE) /
  853. pcm_config_mm.rate;
  854. size = ((size + 15) / 16) * 16;
  855. return size * audio_stream_frame_size((struct audio_stream *)stream);
  856. }
  857. static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
  858. {
  859. struct m0_stream_out *out = (struct m0_stream_out *)stream;
  860. return out->channel_mask;
  861. }
  862. static audio_format_t out_get_format(const struct audio_stream *stream)
  863. {
  864. return AUDIO_FORMAT_PCM_16_BIT;
  865. }
  866. static int out_set_format(struct audio_stream *stream, audio_format_t format)
  867. {
  868. return 0;
  869. }
  870. /* must be called with hw device and output stream mutexes locked */
  871. static int do_output_standby(struct m0_stream_out *out)
  872. {
  873. struct m0_audio_device *adev = out->dev;
  874. int i;
  875. bool all_outputs_in_standby = true;
  876. if (!out->standby) {
  877. out->standby = 1;
  878. for (i = 0; i < PCM_TOTAL; i++) {
  879. if (out->pcm[i]) {
  880. pcm_close(out->pcm[i]);
  881. out->pcm[i] = NULL;
  882. }
  883. }
  884. for (i = 0; i < OUTPUT_TOTAL; i++) {
  885. if (adev->outputs[i] != NULL && !adev->outputs[i]->standby) {
  886. all_outputs_in_standby = false;
  887. break;
  888. }
  889. }
  890. /* force standby on low latency output stream so that it can reuse HDMI driver if
  891. * necessary when restarted */
  892. if (out == adev->outputs[OUTPUT_HDMI]) {
  893. if (adev->outputs[OUTPUT_LOW_LATENCY] != NULL &&
  894. !adev->outputs[OUTPUT_LOW_LATENCY]->standby) {
  895. struct m0_stream_out *ll_out = adev->outputs[OUTPUT_LOW_LATENCY];
  896. pthread_mutex_lock(&ll_out->lock);
  897. do_output_standby(ll_out);
  898. pthread_mutex_unlock(&ll_out->lock);
  899. }
  900. }
  901. /* stop writing to echo reference */
  902. if (out->echo_reference != NULL) {
  903. out->echo_reference->write(out->echo_reference, NULL);
  904. out->echo_reference = NULL;
  905. }
  906. }
  907. return 0;
  908. }
  909. static int out_standby(struct audio_stream *stream)
  910. {
  911. struct m0_stream_out *out = (struct m0_stream_out *)stream;
  912. int status;
  913. pthread_mutex_lock(&out->dev->lock);
  914. pthread_mutex_lock(&out->lock);
  915. status = do_output_standby(out);
  916. pthread_mutex_unlock(&out->lock);
  917. pthread_mutex_unlock(&out->dev->lock);
  918. return status;
  919. }
  920. static int out_dump(const struct audio_stream *stream, int fd)
  921. {
  922. return 0;
  923. }
  924. static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
  925. {
  926. struct m0_stream_out *out = (struct m0_stream_out *)stream;
  927. struct m0_audio_device *adev = out->dev;
  928. struct m0_stream_in *in;
  929. struct str_parms *parms;
  930. char *str;
  931. char value[32];
  932. int ret, val = 0;
  933. bool force_input_standby = false;
  934. parms = str_parms_create_str(kvpairs);
  935. ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
  936. if (ret >= 0) {
  937. val = atoi(value);
  938. pthread_mutex_lock(&adev->lock);
  939. pthread_mutex_lock(&out->lock);
  940. if (((adev->out_device) != val) && (val != 0)) {
  941. /* this is needed only when changing device on low latency output
  942. * as other output streams are not used for voice use cases nor
  943. * handle duplication to HDMI or SPDIF */
  944. if (out == adev->outputs[OUTPUT_LOW_LATENCY] && !out->standby) {
  945. /* a change in output device may change the microphone selection */
  946. if (adev->active_input &&
  947. adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
  948. force_input_standby = true;
  949. }
  950. /* force standby if moving to/from HDMI/SPDIF or if the output
  951. * device changes when in HDMI/SPDIF mode */
  952. /* FIXME also force standby when in call as some audio path switches do not work
  953. * while in call and an output stream is active (e.g BT SCO => earpiece) */
  954. /* FIXME workaround for audio being dropped when switching path without forcing standby
  955. * (several hundred ms of audio can be lost: e.g beginning of a ringtone. We must understand
  956. * the root cause in audio HAL, driver or ABE.
  957. if (((val & AUDIO_DEVICE_OUT_AUX_DIGITAL) ^
  958. (adev->out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) ||
  959. ((val & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) ^
  960. (adev->out_device & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)) ||
  961. (adev->out_device & (AUDIO_DEVICE_OUT_AUX_DIGITAL |
  962. AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)))
  963. */
  964. if (((val & AUDIO_DEVICE_OUT_AUX_DIGITAL) ^
  965. (adev->out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL)) ||
  966. ((val & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) ^
  967. (adev->out_device & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)) ||
  968. (adev->out_device & (AUDIO_DEVICE_OUT_AUX_DIGITAL |
  969. AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)) ||
  970. ((val & AUDIO_DEVICE_OUT_SPEAKER) ^
  971. (adev->out_device & AUDIO_DEVICE_OUT_SPEAKER)) ||
  972. (adev->mode == AUDIO_MODE_IN_CALL))
  973. do_output_standby(out);
  974. }
  975. if (out != adev->outputs[OUTPUT_HDMI]) {
  976. adev->out_device = val;
  977. select_output_device(adev);
  978. }
  979. }
  980. pthread_mutex_unlock(&out->lock);
  981. if (force_input_standby) {
  982. in = adev->active_input;
  983. pthread_mutex_lock(&in->lock);
  984. do_input_standby(in);
  985. pthread_mutex_unlock(&in->lock);
  986. }
  987. pthread_mutex_unlock(&adev->lock);
  988. }
  989. str_parms_destroy(parms);
  990. return ret;
  991. }
  992. static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
  993. {
  994. struct m0_stream_out *out = (struct m0_stream_out *)stream;
  995. struct str_parms *query = str_parms_create_str(keys);
  996. char *str;
  997. char value[256];
  998. struct str_parms *reply = str_parms_create();
  999. size_t i, j;
  1000. int ret;
  1001. bool first = true;
  1002. ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
  1003. if (ret >= 0) {
  1004. value[0] = '\0';
  1005. i = 0;
  1006. while (out->sup_channel_masks[i] != 0) {
  1007. for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
  1008. if (out_channels_name_to_enum_table[j].value == out->sup_channel_masks[i]) {
  1009. if (!first) {
  1010. strcat(value, "|");
  1011. }
  1012. strcat(value, out_channels_name_to_enum_table[j].name);
  1013. first = false;
  1014. break;
  1015. }
  1016. }
  1017. i++;
  1018. }
  1019. str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
  1020. str = strdup(str_parms_to_str(reply));
  1021. } else {
  1022. str = strdup(keys);
  1023. }
  1024. str_parms_destroy(query);
  1025. str_parms_destroy(reply);
  1026. return str;
  1027. }
  1028. static uint32_t out_get_latency_low_latency(const struct audio_stream_out *stream)
  1029. {
  1030. struct m0_stream_out *out = (struct m0_stream_out *)stream;
  1031. /* Note: we use the default rate here from pcm_config_mm.rate */
  1032. return (SHORT_PERIOD_SIZE * PLAYBACK_SHORT_PERIOD_COUNT * 1000) / pcm_config_tones.rate;
  1033. }
  1034. static uint32_t out_get_latency_deep_buffer(const struct audio_stream_out *stream)
  1035. {
  1036. struct m0_stream_out *out = (struct m0_stream_out *)stream;
  1037. /* Note: we use the default rate here from pcm_config_mm.rate */
  1038. return (DEEP_BUFFER_LONG_PERIOD_SIZE * PLAYBACK_DEEP_BUFFER_LONG_PERIOD_COUNT * 1000) /
  1039. pcm_config_mm.rate;
  1040. }
  1041. static int out_set_volume(struct audio_stream_out *stream, float left,
  1042. float right)
  1043. {
  1044. return -ENOSYS;
  1045. }
  1046. static ssize_t out_write_low_latency(struct audio_stream_out *stream, const void* buffer,
  1047. size_t bytes)
  1048. {
  1049. int ret;
  1050. struct m0_stream_out *out = (struct m0_stream_out *)stream;
  1051. struct m0_audio_device *adev = out->dev;
  1052. size_t frame_size = audio_stream_frame_size(&out->stream.common);
  1053. size_t in_frames = bytes / frame_size;
  1054. size_t out_frames = in_frames;
  1055. bool force_input_standby = false;
  1056. struct m0_stream_in *in;
  1057. int i;
  1058. /* acquiring hw device mutex systematically is useful if a low priority thread is waiting
  1059. * on the output stream mutex - e.g. executing select_mode() while holding the hw device
  1060. * mutex
  1061. */
  1062. pthread_mutex_lock(&adev->lock);
  1063. pthread_mutex_lock(&out->lock);
  1064. if (out->standby) {
  1065. ret = start_output_stream_low_latency(out);
  1066. if (ret != 0) {
  1067. pthread_mutex_unlock(&adev->lock);
  1068. goto exit;
  1069. }
  1070. out->standby = 0;
  1071. /* a change in output device may change the microphone selection */
  1072. if (adev->active_input &&
  1073. adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION)
  1074. force_input_standby = true;
  1075. }
  1076. pthread_mutex_unlock(&adev->lock);
  1077. for (i = 0; i < PCM_TOTAL; i++) {
  1078. /* only use resampler if required */
  1079. if (out->pcm[i] && (out->config[i].rate != DEFAULT_OUT_SAMPLING_RATE)) {
  1080. out_frames = out->buffer_frames;
  1081. out->resampler->resample_from_input(out->resampler,
  1082. (int16_t *)buffer,
  1083. &in_frames,
  1084. (int16_t *)out->buffer,
  1085. &out_frames);
  1086. break;
  1087. }
  1088. }
  1089. if (out->echo_reference != NULL) {
  1090. struct echo_reference_buffer b;
  1091. b.raw = (void *)buffer;
  1092. b.frame_count = in_frames;
  1093. get_playback_delay(out, out_frames, &b);
  1094. out->echo_reference->write(out->echo_reference, &b);
  1095. }
  1096. /* Write to all active PCMs */
  1097. for (i = 0; i < PCM_TOTAL; i++) {
  1098. if (out->pcm[i]) {
  1099. if (out->config[i].rate == DEFAULT_OUT_SAMPLING_RATE) {
  1100. /* PCM uses native sample rate */
  1101. ret = PCM_WRITE(out->pcm[i], (void *)buffer, bytes);
  1102. } else {
  1103. /* PCM needs resampler */
  1104. ret = PCM_WRITE(out->pcm[i], (void *)out->buffer, out_frames * frame_size);
  1105. }
  1106. if (ret)
  1107. break;
  1108. }
  1109. }
  1110. exit:
  1111. pthread_mutex_unlock(&out->lock);
  1112. if (ret != 0) {
  1113. usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) /
  1114. out_get_sample_rate(&stream->common));
  1115. }
  1116. if (force_input_standby) {
  1117. pthread_mutex_lock(&adev->lock);
  1118. if (adev->active_input) {
  1119. in = adev->active_input;
  1120. pthread_mutex_lock(&in->lock);
  1121. do_input_standby(in);
  1122. pthread_mutex_unlock(&in->lock);
  1123. }
  1124. pthread_mutex_unlock(&adev->lock);
  1125. }
  1126. return bytes;
  1127. }
  1128. static ssize_t out_write_deep_buffer(struct audio_stream_out *stream, const void* buffer,
  1129. size_t bytes)
  1130. {
  1131. int ret;
  1132. struct m0_stream_out *out = (struct m0_stream_out *)stream;
  1133. struct m0_audio_device *adev = out->dev;
  1134. size_t frame_size = audio_stream_frame_size(&out->stream.common);
  1135. size_t in_frames = bytes / frame_size;
  1136. size_t out_frames;
  1137. bool use_long_periods;
  1138. int kernel_frames;
  1139. void *buf;
  1140. /* acquiring hw device mutex systematically is useful if a low priority thread is waiting
  1141. * on the output stream mutex - e.g. executing select_mode() while holding the hw device
  1142. * mutex
  1143. */
  1144. pthread_mutex_lock(&adev->lock);
  1145. pthread_mutex_lock(&out->lock);
  1146. if (out->standby) {
  1147. ret = start_output_stream_deep_buffer(out);
  1148. if (ret != 0) {
  1149. pthread_mutex_unlock(&adev->lock);
  1150. goto exit;
  1151. }
  1152. out->standby = 0;
  1153. }
  1154. use_long_periods = adev->screen_off && !adev->active_input;
  1155. pthread_mutex_unlock(&adev->lock);
  1156. if (use_long_periods != out->use_long_periods) {
  1157. size_t period_size;
  1158. size_t period_count;
  1159. if (use_long_periods) {
  1160. period_size = DEEP_BUFFER_LONG_PERIOD_SIZE;
  1161. period_count = PLAYBACK_DEEP_BUFFER_LONG_PERIOD_COUNT;
  1162. } else {
  1163. period_size = DEEP_BUFFER_SHORT_PERIOD_SIZE;
  1164. period_count = PLAYBACK_DEEP_BUFFER_SHORT_PERIOD_COUNT;
  1165. }
  1166. out->write_threshold = period_size * period_count;
  1167. pcm_set_avail_min(out->pcm[PCM_NORMAL], period_size);
  1168. out->use_long_periods = use_long_periods;
  1169. }
  1170. /* only use resampler if required */
  1171. if (out->config[PCM_NORMAL].rate != DEFAULT_OUT_SAMPLING_RATE) {
  1172. out_frames = out->buffer_frames;
  1173. out->resampler->resample_from_input(out->resampler,
  1174. (int16_t *)buffer,
  1175. &in_frames,
  1176. (int16_t *)out->buffer,
  1177. &out_frames);
  1178. buf = (void *)out->buffer;
  1179. } else {
  1180. out_frames = in_frames;
  1181. buf = (void *)buffer;
  1182. }
  1183. /* do not allow more than out->write_threshold frames in kernel pcm driver buffer */
  1184. do {
  1185. struct timespec time_stamp;
  1186. if (pcm_get_htimestamp(out->pcm[PCM_NORMAL],
  1187. (unsigned int *)&kernel_frames, &time_stamp) < 0)
  1188. break;
  1189. kernel_frames = pcm_get_buffer_size(out->pcm[PCM_NORMAL]) - kernel_frames;
  1190. if (kernel_frames > out->write_threshold) {
  1191. unsigned long time = (unsigned long)
  1192. (((int64_t)(kernel_frames - out->write_threshold) * 1000000) /
  1193. MM_FULL_POWER_SAMPLING_RATE);
  1194. if (time < MIN_WRITE_SLEEP_US)
  1195. time = MIN_WRITE_SLEEP_US;
  1196. usleep(time);
  1197. }
  1198. } while (kernel_frames > out->write_threshold);
  1199. ret = pcm_mmap_write(out->pcm[PCM_NORMAL], buf, out_frames * frame_size);
  1200. exit:
  1201. pthread_mutex_unlock(&out->lock);
  1202. if (ret != 0) {
  1203. usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) /
  1204. out_get_sample_rate(&stream->common));
  1205. }
  1206. return bytes;
  1207. }
  1208. static int out_get_render_position(const struct audio_stream_out *stream,
  1209. uint32_t *dsp_frames)
  1210. {
  1211. return -EINVAL;
  1212. }
  1213. static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
  1214. {
  1215. return 0;
  1216. }
  1217. static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
  1218. {
  1219. return 0;
  1220. }
  1221. /** audio_stream_in implementation **/
  1222. /* must be called with hw device and input stream mutexes locked */
  1223. static int start_input_stream(struct m0_stream_in *in)
  1224. {
  1225. int ret = 0;
  1226. struct m0_audio_device *adev = in->dev;
  1227. adev->active_input = in;
  1228. if (adev->mode != AUDIO_MODE_IN_CALL) {
  1229. adev->in_device = in->device;
  1230. select_input_device(adev);
  1231. }
  1232. if (in->aux_channels_changed)
  1233. {
  1234. in->aux_channels_changed = false;
  1235. in->config.channels = popcount(in->main_channels | in->aux_channels);
  1236. if (in->resampler) {
  1237. /* release and recreate the resampler with the new number of channel of the input */
  1238. release_resampler(in->resampler);
  1239. in->resampler = NULL;
  1240. ret = create_resampler(in->config.rate,
  1241. in->requested_rate,
  1242. in->config.channels,
  1243. RESAMPLER_QUALITY_DEFAULT,
  1244. &in->buf_provider,
  1245. &in->resampler);
  1246. }
  1247. ALOGV("%s: New channel configuration, "
  1248. "main_channels = [%04x…

Large files files are truncated, but you can click here to view the full file