jznomoney

Untitled

Aug 5th, 2011
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 117.44 KB | None | 0 0
  1. /*
  2. ** Copyright 2009, The Android Open-Source Project
  3. ** Copyright (c) 2009-2010, Code Aurora Forum. All rights reserved.
  4. ** Copyright (c) 2011, The CyanogenMod Project
  5. **
  6. ** Licensed under the Apache License, Version 2.0 (the "License");
  7. ** you may not use this file except in compliance with the License.
  8. ** You may obtain a copy of the License at
  9. **
  10. ** http://www.apache.org/licenses/LICENSE-2.0
  11. **
  12. ** Unless required by applicable law or agreed to in writing, software
  13. ** distributed under the License is distributed on an "AS IS" BASIS,
  14. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. ** See the License for the specific language governing permissions and
  16. ** limitations under the License.
  17. */
  18.  
  19. #include <math.h>
  20.  
  21. #define LOG_NDEBUG 0
  22. #define LOG_NDDEBUG 0
  23.  
  24. #define LOG_TAG "AudioHardwareMSM7X30"
  25. #include <utils/Log.h>
  26. #include <utils/String8.h>
  27.  
  28. #include <stdio.h>
  29. #include <unistd.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <dlfcn.h>
  34. #include <fcntl.h>
  35. #include <cutils/properties.h>
  36.  
  37. /* hardware specific functions */
  38. #include "control.h"
  39. #include "AudioHardware.h"
  40. #include <media/AudioSystem.h>
  41. #include <media/AudioRecord.h>
  42. #include <media/mediarecorder.h>
  43.  
  44. extern "C" {
  45. #include <linux/spi_aic3254.h>
  46. #include <linux/tpa2051d3.h>
  47. }
  48.  
  49. #define LOG_SND_RPC 0 /* Set to 1 to log sound RPC's */
  50.  
  51. #define DUALMIC_KEY "dualmic_enabled"
  52. #define TTY_MODE_KEY "tty_mode"
  53. #define DSP_EFFECT_KEY "dolby_srs_eq"
  54.  
  55. #ifdef WITH_QCOM_SPEECH
  56. #define AMRNB_DEVICE_IN "/dev/msm_amrnb_in"
  57. #define EVRC_DEVICE_IN "/dev/msm_evrc_in"
  58. #define QCELP_DEVICE_IN "/dev/msm_qcelp_in"
  59. #endif
  60. #define AAC_DEVICE_IN "/dev/msm_aac_in"
  61.  
  62. #ifdef WITH_QCOM_SPEECH
  63. #define AMRNB_FRAME_SIZE 32
  64. #define EVRC_FRAME_SIZE 23
  65. #define QCELP_FRAME_SIZE 35
  66. #endif
  67.  
  68. namespace android {
  69.  
  70. Mutex mDeviceSwitchLock;
  71. static void * acoustic;
  72. const uint32_t AudioHardware::inputSamplingRates[] = {
  73. 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
  74. };
  75.  
  76. static const uint32_t INVALID_DEVICE = 65535;
  77. static const uint32_t SND_DEVICE_HANDSET = 0;
  78. static const uint32_t SND_DEVICE_SPEAKER = 1;
  79. static const uint32_t SND_DEVICE_HEADSET = 2;
  80. static const uint32_t SND_DEVICE_BT = 3;
  81. static const uint32_t SND_DEVICE_CARKIT = 4;
  82. static const uint32_t SND_DEVICE_TTY_FULL = 5;
  83. static const uint32_t SND_DEVICE_TTY_VCO = 6;
  84. static const uint32_t SND_DEVICE_TTY_HCO = 7;
  85. static const uint32_t SND_DEVICE_NO_MIC_HEADSET = 8;
  86. static const uint32_t SND_DEVICE_FM_HEADSET = 9;
  87. static const uint32_t SND_DEVICE_HEADSET_AND_SPEAKER = 10;
  88. static const uint32_t SND_DEVICE_FM_SPEAKER = 11;
  89. static const uint32_t SND_DEVICE_FM_HANDSET = 12;
  90. static const uint32_t SND_DEVICE_HEADPHONE_AND_SPEAKER = 16;
  91. static const uint32_t SND_DEVICE_IN_S_SADC_OUT_HANDSET = 17;
  92. static const uint32_t SND_DEVICE_IN_S_SADC_OUT_SPEAKER_PHONE = 18;
  93. static const uint32_t SND_DEVICE_HANDSET_BACK_MIC = 20;
  94. static const uint32_t SND_DEVICE_SPEAKER_BACK_MIC = 21;
  95. static const uint32_t SND_DEVICE_NO_MIC_HEADSET_BACK_MIC = 28;
  96. static const uint32_t SND_DEVICE_HEADSET_AND_SPEAKER_BACK_MIC = 30;
  97. static const uint32_t SND_DEVICE_I2S_SPEAKER = 32;
  98. static const uint32_t SND_DEVICE_BT_EC_OFF = 45;
  99. static const uint32_t SND_DEVICE_HAC = 252;
  100. static const uint32_t SND_DEVICE_USB_HEADSET = 253;
  101.  
  102. static const uint32_t DEVICE_HANDSET_RX = 0; /* handset_rx */
  103. static const uint32_t DEVICE_HANDSET_TX = 1; /* handset_tx */
  104. static const uint32_t DEVICE_HEADSET_RX = 2; /* headset_stereo_rx */
  105. static const uint32_t DEVICE_HEADSET_MONO_RX = 3; /* headset_mono_rx */
  106. static const uint32_t DEVICE_HEADSET_TX = 4; /* headset_mono_tx */
  107. static const uint32_t DEVICE_FMRADIO_HANDSET_RX = 5; /* fmradio_handset_rx */
  108. static const uint32_t DEVICE_SPEAKER_RX = 6; /* speaker_mono_rx */
  109. static const uint32_t DEVICE_FMRADIO_SPEAKER_RX = 7; /* fmradio_speaker_rx */
  110. static const uint32_t DEVICE_FMRADIO_HEADSET_RX = 8; /* fmradio_headset_rx */
  111. static const uint32_t DEVICE_TTY_HEADSET_MONO_TX = 9; /* tty_headset_mono_tx */
  112. static const uint32_t DEVICE_TTY_HEADSET_MONO_RX = 10; /* tty_headset_mono_rx */
  113. static const uint32_t DEVICE_SPEAKER_TX = 11; /* speaker_mono_tx */
  114. static const uint32_t DEVICE_SPEAKER_HEADSET_RX = 12; /* headset_speaker_stereo_rx */
  115. static const uint32_t DEVICE_USB_HEADSET_RX = 13; /* usb_headset_stereo_rx */
  116. static const uint32_t DEVICE_HAC_RX = 14; /* hac_mono_rx */
  117. static const uint32_t DEVICE_ALT_RX = 15; /* alt_mono_rx */
  118. static const uint32_t DEVICE_VR_HANDSET = 16; /* handset_vr_tx */
  119. static const uint32_t DEVICE_BT_SCO_RX = 17; /* bt_sco_rx */
  120. static const uint32_t DEVICE_BT_SCO_TX = 18; /* bt_sco_tx */
  121. static const uint32_t DEVICE_DUALMIC_HANDSET_TX = 19; /* dual_mic_stereo_tx */
  122. static const uint32_t DEVICE_COUNT = DEVICE_DUALMIC_HANDSET_TX +1;
  123.  
  124. static bool support_aic3254 = true;
  125. static bool support_tpa2051 = true;
  126. static int alt_enable = 0;
  127. static int hac_enable = 0;
  128. static uint32_t cur_aic_tx = 0;
  129. static uint32_t cur_aic_rx = 0;
  130.  
  131. int dev_cnt = 0;
  132. const char ** name = NULL;
  133. int mixer_cnt = 0;
  134. static uint32_t cur_tx = INVALID_DEVICE;
  135. static uint32_t cur_rx = INVALID_DEVICE;
  136.  
  137. typedef struct routing_table {
  138. unsigned short dec_id;
  139. uint32_t dev_id;
  140. uint32_t dev_id_tx;
  141. int stream_type;
  142. bool active;
  143. struct routing_table *next;
  144. } Routing_table;
  145. Routing_table* head;
  146. Mutex mRoutingTableLock;
  147.  
  148. typedef struct device_table {
  149. int dev_id;
  150. int class_id;
  151. int capability;
  152. } Device_table;
  153. Device_table* device_list;
  154.  
  155. static void amr_transcode(unsigned char *src, unsigned char *dst);
  156.  
  157. enum STREAM_TYPES {
  158. PCM_PLAY=1,
  159. PCM_REC,
  160. LPA_DECODE,
  161. VOICE_CALL,
  162. FM_RADIO
  163. };
  164.  
  165. #define DEV_ID(X) device_list[X].dev_id
  166. void addToTable(int decoder_id, int device_id, int device_id_tx, int stream_type, bool active) {
  167. LOGD("addToTable (dec_id %d, dev_rx %d, dev_tx %d, type %d, active %d)", decoder_id, device_id, device_id_tx, stream_type, active);
  168. Routing_table* temp_ptr;
  169. Mutex::Autolock lock(mRoutingTableLock);
  170. temp_ptr = (Routing_table* ) malloc(sizeof(Routing_table));
  171. temp_ptr->next = NULL;
  172. temp_ptr->dec_id = decoder_id;
  173. temp_ptr->dev_id = device_id;
  174. temp_ptr->dev_id_tx = device_id_tx;
  175. temp_ptr->stream_type = stream_type;
  176. temp_ptr->active = active;
  177. /* add new Node to head */
  178. temp_ptr->next = head->next;
  179. head->next = temp_ptr;
  180. }
  181.  
  182. bool isStreamOn(int Stream_type) {
  183. Routing_table* temp_ptr;
  184. Mutex::Autolock lock(mRoutingTableLock);
  185. temp_ptr = head->next;
  186. while (temp_ptr!=NULL) {
  187. if (temp_ptr->stream_type == Stream_type)
  188. return true;
  189. temp_ptr = temp_ptr->next;
  190. }
  191. return false;
  192. }
  193.  
  194. bool isStreamOnAndActive(int Stream_type) {
  195. Routing_table* temp_ptr;
  196. Mutex::Autolock lock(mRoutingTableLock);
  197. temp_ptr = head->next;
  198. while (temp_ptr!=NULL) {
  199. if (temp_ptr->stream_type == Stream_type) {
  200. if (temp_ptr->active == true)
  201. return true;
  202. return false;
  203. }
  204. temp_ptr = temp_ptr->next;
  205. }
  206. return false;
  207. }
  208.  
  209. bool isStreamOnAndInactive(int Stream_type) {
  210. Routing_table* temp_ptr;
  211. Mutex::Autolock lock(mRoutingTableLock);
  212. temp_ptr = head->next;
  213. while (temp_ptr!=NULL) {
  214. if (temp_ptr->stream_type == Stream_type) {
  215. if (temp_ptr->active == false)
  216. return true;
  217. return false;
  218. }
  219. temp_ptr = temp_ptr->next;
  220. }
  221. return false;
  222. }
  223.  
  224. Routing_table* getNodeByStreamType(int Stream_type) {
  225. Routing_table* temp_ptr;
  226. Mutex::Autolock lock(mRoutingTableLock);
  227. temp_ptr = head->next;
  228. while (temp_ptr!=NULL) {
  229. if (temp_ptr->stream_type == Stream_type)
  230. return temp_ptr;
  231. temp_ptr=temp_ptr->next;
  232. }
  233. return NULL;
  234. }
  235.  
  236. void modifyActiveStateOfStream(int Stream_type, bool Active) {
  237. Routing_table* temp_ptr;
  238. Mutex::Autolock lock(mRoutingTableLock);
  239. temp_ptr = head->next;
  240. while (temp_ptr!=NULL) {
  241. if (temp_ptr->stream_type == Stream_type) {
  242. temp_ptr->active = Active;
  243. return;
  244. }
  245. temp_ptr = temp_ptr->next;
  246. }
  247. }
  248.  
  249. void modifyActiveDeviceOfStream(int Stream_type, int Device_id, int Device_id_tx) {
  250. Routing_table* temp_ptr;
  251. temp_ptr = head->next;
  252. while (temp_ptr!=NULL) {
  253. if (temp_ptr->stream_type == Stream_type) {
  254. temp_ptr->dev_id = Device_id;
  255. temp_ptr->dev_id_tx = Device_id_tx;
  256. return;
  257. }
  258. temp_ptr = temp_ptr->next;
  259. }
  260. }
  261.  
  262. void printTable() {
  263. Routing_table * temp_ptr;
  264. Mutex::Autolock lock(mRoutingTableLock);
  265. temp_ptr = head->next;
  266. while (temp_ptr!=NULL) {
  267. printf("%d %d %d %d %d\n", temp_ptr->dec_id, temp_ptr->dev_id, temp_ptr->dev_id_tx,
  268. temp_ptr->stream_type, temp_ptr->active);
  269. temp_ptr = temp_ptr->next;
  270. }
  271. }
  272.  
  273. void deleteFromTable(int Stream_type) {
  274. LOGD("deleteFromTable, type %d", Stream_type);
  275. Routing_table *temp_ptr,*temp1;
  276. Mutex::Autolock lock(mRoutingTableLock);
  277. temp_ptr = head;
  278. while (temp_ptr->next!=NULL) {
  279. if (temp_ptr->next->stream_type == Stream_type) {
  280. temp1 = temp_ptr->next;
  281. temp_ptr->next = temp_ptr->next->next;
  282. free(temp1);
  283. return;
  284. }
  285. temp_ptr = temp_ptr->next;
  286. }
  287. }
  288.  
  289. bool isDeviceListEmpty() {
  290. if (head->next == NULL)
  291. return true;
  292. return false;
  293. }
  294.  
  295. int enableDevice(int device, short enable) {
  296. LOGD("value of device and enable is %d %d", device, enable);
  297. if (msm_en_device(DEV_ID(device), enable)) {
  298. LOGE("msm_en_device(%d, %d) failed errno = %d", DEV_ID(device), enable, errno);
  299. return -1;
  300. }
  301. return 0;
  302. }
  303.  
  304. void updateACDB(uint32_t new_rx_device, uint32_t new_tx_device,
  305. uint32_t new_rx_acdb, uint32_t new_tx_acdb) {
  306. LOGD("updateACDB: (%d, %d, %d, %d) ", new_tx_device, new_rx_device, new_tx_acdb, new_rx_acdb);
  307.  
  308. int rc = -1;
  309. int (*update_acdb_id)(uint32_t, uint32_t, uint32_t, uint32_t);
  310.  
  311. update_acdb_id = (int (*)(uint32_t, uint32_t, uint32_t, uint32_t))::dlsym(acoustic, "update_acdb_id");
  312. if ((*update_acdb_id) == 0)
  313. LOGE("Could not open update_acdb_id()");
  314. else {
  315. rc = update_acdb_id(new_tx_device, new_rx_device, new_tx_acdb, new_rx_acdb);
  316. if (rc < 0)
  317. LOGE("Could not set update_acdb_id: %d", rc);
  318. }
  319. }
  320.  
  321. static status_t updateDeviceInfo(uint32_t rx_device, uint32_t tx_device,
  322. uint32_t rx_acdb_id, uint32_t tx_acdb_id ) {
  323. LOGD("updateDeviceInfo: E rx_device %d and tx_device %d", rx_device, tx_device);
  324. bool isRxDeviceEnabled = false, isTxDeviceEnabled = false;
  325. Routing_table *temp_ptr, *temp_head;
  326. temp_head = head;
  327.  
  328. Mutex::Autolock lock(mDeviceSwitchLock);
  329.  
  330. if (!getNodeByStreamType(VOICE_CALL) && !getNodeByStreamType(PCM_PLAY) &&
  331. !getNodeByStreamType(LPA_DECODE) && !getNodeByStreamType(FM_RADIO)) {
  332. LOGD("No active voicecall/playback, disabling cur_rx %d", cur_rx);
  333. if (cur_rx != INVALID_DEVICE && enableDevice(cur_rx, 0)) {
  334. LOGE("Disabling device failed for cur_rx %d", cur_rx);
  335. }
  336. cur_rx = rx_device;
  337. }
  338.  
  339. if (!getNodeByStreamType(VOICE_CALL) && !getNodeByStreamType(PCM_REC)) {
  340. LOGD("No active voicecall/recording, disabling cur_tx %d", cur_tx);
  341. if (cur_tx != INVALID_DEVICE && enableDevice(cur_tx, 0)) {
  342. LOGE("Disabling device failed for cur_tx %d", cur_tx);
  343. }
  344. cur_tx = tx_device;
  345. }
  346.  
  347. Mutex::Autolock lock_1(mRoutingTableLock);
  348.  
  349. while (temp_head->next != NULL) {
  350. temp_ptr = temp_head->next;
  351. switch (temp_ptr->stream_type) {
  352. case PCM_PLAY:
  353. case LPA_DECODE:
  354. case FM_RADIO:
  355. LOGD("The node type is %d and cur device %d new device %d ", temp_ptr->stream_type, temp_ptr->dev_id, rx_device);
  356. if (rx_device == INVALID_DEVICE)
  357. return -1;
  358. if (rx_device == temp_ptr->dev_id)
  359. break;
  360. LOGV("rx_device = %d,temp_ptr->dev_id = %d", rx_device, temp_ptr->dev_id);
  361. if (isRxDeviceEnabled == false) {
  362. enableDevice(temp_ptr->dev_id, 0);
  363. enableDevice(rx_device, 1);
  364. isRxDeviceEnabled = true;
  365. }
  366. if (msm_route_stream(PCM_PLAY, temp_ptr->dec_id, DEV_ID(temp_ptr->dev_id), 0)) {
  367. LOGE("msm_route_stream(PCM_PLAY,%d,%d,0) failed", temp_ptr->dec_id, DEV_ID(temp_ptr->dev_id));
  368. }
  369. if(msm_route_stream(PCM_PLAY,temp_ptr->dec_id,DEV_ID(rx_device),1)) {
  370. LOGE("msm_route_stream(PCM_PLAY,%d,%d,1) failed", temp_ptr->dec_id, DEV_ID(rx_device));
  371. }
  372. modifyActiveDeviceOfStream(temp_ptr->stream_type, rx_device, INVALID_DEVICE);
  373. cur_tx = tx_device;
  374. cur_rx = rx_device;
  375. break;
  376. case PCM_REC:
  377. LOGD("case PCM_REC");
  378. if (tx_device == INVALID_DEVICE)
  379. return -1;
  380. if (tx_device == temp_ptr->dev_id)
  381. break;
  382.  
  383. if (isTxDeviceEnabled == false) {
  384. enableDevice(temp_ptr->dev_id, 0);
  385. enableDevice(tx_device, 1);
  386. isTxDeviceEnabled = true;
  387. }
  388. if (msm_route_stream(PCM_REC, temp_ptr->dec_id, DEV_ID(temp_ptr->dev_id), 0)) {
  389. LOGE("msm_route_stream(PCM_PLAY, %d, %d, 0) failed", temp_ptr->dec_id, DEV_ID(temp_ptr->dev_id));
  390. }
  391. if (msm_route_stream(PCM_REC, temp_ptr->dec_id, DEV_ID(tx_device), 1)) {
  392. LOGE("msm_route_stream(PCM_REC, %d, %d, 1) failed", temp_ptr->dec_id, DEV_ID(tx_device));
  393. }
  394. modifyActiveDeviceOfStream(PCM_REC, tx_device, INVALID_DEVICE);
  395. cur_tx = tx_device;
  396. cur_rx = rx_device;
  397. break;
  398. case VOICE_CALL:
  399. LOGD("case VOICE_CALL");
  400. if (rx_device == INVALID_DEVICE || tx_device == INVALID_DEVICE)
  401. return -1;
  402. if (rx_device == temp_ptr->dev_id && tx_device == temp_ptr->dev_id_tx)
  403. break;
  404.  
  405. updateACDB(rx_device, tx_device, rx_acdb_id, tx_acdb_id);
  406. msm_route_voice(DEV_ID(rx_device), DEV_ID(tx_device), 1);
  407.  
  408. /* Temporary work around for Speaker mode. The driver is not
  409. supporting Speaker Rx and Handset Tx combo */
  410. if (isRxDeviceEnabled == false) {
  411. if (rx_device != temp_ptr->dev_id)
  412. enableDevice(temp_ptr->dev_id, 0);
  413. isRxDeviceEnabled = true;
  414. }
  415. if(isTxDeviceEnabled == false) {
  416. if (tx_device != temp_ptr->dev_id_tx)
  417. enableDevice(temp_ptr->dev_id_tx, 0);
  418. isTxDeviceEnabled = true;
  419. }
  420.  
  421. if (rx_device != temp_ptr->dev_id)
  422. enableDevice(rx_device, 1);
  423. if (tx_device != temp_ptr->dev_id_tx)
  424. enableDevice(tx_device, 1);
  425.  
  426. cur_rx = rx_device;
  427. cur_tx = tx_device;
  428. modifyActiveDeviceOfStream(VOICE_CALL, cur_rx, cur_tx);
  429. break;
  430. default:
  431. break;
  432. }
  433. temp_head = temp_head->next;
  434. }
  435.  
  436. LOGD("updateDeviceInfo: X cur_rx %d cur_tx %d", cur_rx, cur_tx);
  437. return NO_ERROR;
  438. }
  439.  
  440. void freeMemory() {
  441. Routing_table *temp;
  442. while (head != NULL) {
  443. temp = head->next;
  444. free(head);
  445. head = temp;
  446. }
  447. free(device_list);
  448. }
  449.  
  450. /* ---------------------------------------------------------------------------- */
  451.  
  452. AudioHardware::AudioHardware() :
  453. mInit(false), mMicMute(true), mBluetoothNrec(true), mBluetoothId(0),
  454. mHACSetting(false), mBluetoothIdTx(0), mBluetoothIdRx(0),
  455. mOutput(0), mCurSndDevice(INVALID_DEVICE), mVoiceVolume(VOICE_VOLUME_MAX),
  456. mTtyMode(TTY_OFF), mDualMicEnabled(true), mRecordState(false),
  457. mEffectEnabled(false)
  458. {
  459. int (*snd_get_num)();
  460. int (*snd_get_bt_endpoint)(msm_bt_endpoint *);
  461. int (*set_acoustic_parameters)();
  462. int (*set_tpa2051_parameters)();
  463. int (*set_aic3254_parameters)();
  464. int (*support_back_mic)();
  465.  
  466. struct msm_bt_endpoint *ept;
  467.  
  468. int control;
  469. int i = 0,index = 0;
  470.  
  471. acoustic =:: dlopen("/system/lib/libhtc_acoustic.so", RTLD_NOW);
  472. if (acoustic == NULL ) {
  473. LOGD("Could not open libhtc_acoustic.so");
  474. /* this is not really an error on non-htc devices... */
  475. mNumBTEndpoints = 0;
  476. mInit = true;
  477. return;
  478. }
  479.  
  480. head = (Routing_table* ) malloc(sizeof(Routing_table));
  481. head->next = NULL;
  482.  
  483. LOGD("msm_mixer_open: Opening the device");
  484. control = msm_mixer_open("/dev/snd/controlC0", 0);
  485. if(control< 0)
  486. LOGE("ERROR opening the device");
  487.  
  488. mixer_cnt = msm_mixer_count();
  489. LOGD("msm_mixer_count:mixer_cnt = %d", mixer_cnt);
  490.  
  491. dev_cnt = msm_get_device_count();
  492. LOGV("got device_count %d", dev_cnt);
  493. if (dev_cnt <= 0) {
  494. LOGE("NO devices registered");
  495. return;
  496. }
  497.  
  498. name = msm_get_device_list();
  499. device_list = (Device_table* )malloc(sizeof(Device_table)*DEVICE_COUNT);
  500. if (device_list == NULL) {
  501. LOGE("malloc failed for device list");
  502. return;
  503. }
  504. for (i = 0;i<dev_cnt;i++)
  505. device_list[i].dev_id = INVALID_DEVICE;
  506.  
  507. for (i = 0; i < dev_cnt;i++) {
  508. LOGV("******* name[%d] = [%s] *********", i, (char* )name[i]);
  509. if (strcmp((char*)name[i], "handset_rx") == 0)
  510. index = DEVICE_HANDSET_RX;
  511. else if (strcmp((char*)name[i], "handset_tx") == 0)
  512. index = DEVICE_HANDSET_TX;
  513. else if (strcmp((char*)name[i], "headset_stereo_rx") == 0)
  514. index = DEVICE_HEADSET_RX;
  515. else if (strcmp((char*)name[i], "headset_mono_rx") == 0)
  516. index = DEVICE_HEADSET_MONO_RX;
  517. else if (strcmp((char*)name[i], "headset_mono_tx") == 0)
  518. index = DEVICE_HEADSET_TX;
  519. else if (strcmp((char*)name[i], "fmradio_handset_rx") == 0)
  520. index = DEVICE_FMRADIO_HANDSET_RX;
  521. else if (strcmp((char*)name[i], "speaker_mono_rx") == 0)
  522. index = DEVICE_SPEAKER_RX;
  523. else if (strcmp((char*)name[i], "fmradio_speaker_rx") == 0)
  524. index = DEVICE_FMRADIO_SPEAKER_RX;
  525. else if (strcmp((char*)name[i], "fmradio_headset_rx") == 0)
  526. index = DEVICE_FMRADIO_HEADSET_RX;
  527. else if (strcmp((char*)name[i], "tty_headset_mono_tx") == 0)
  528. index = DEVICE_TTY_HEADSET_MONO_TX;
  529. else if (strcmp((char*)name[i], "tty_headset_mono_rx") == 0)
  530. index = DEVICE_TTY_HEADSET_MONO_RX;
  531. else if (strcmp((char*)name[i], "speaker_mono_tx") == 0)
  532. index = DEVICE_SPEAKER_TX;
  533. else if (strcmp((char*)name[i], "headset_speaker_stereo_rx") == 0)
  534. index = DEVICE_SPEAKER_HEADSET_RX;
  535. else if (strcmp((char*)name[i], "usb_headset_stereo_rx") == 0)
  536. index = DEVICE_USB_HEADSET_RX;
  537. else if (strcmp((char*)name[i], "hac_mono_rx") == 0)
  538. index = DEVICE_HAC_RX;
  539. else if (strcmp((char*)name[i], "alt_mono_rx") == 0)
  540. index = DEVICE_ALT_RX;
  541. else if (strcmp((char*)name[i], "handset_vr_tx") == 0)
  542. index = DEVICE_VR_HANDSET;
  543. else if (strcmp((char*)name[i], "bt_sco_rx") == 0)
  544. index = DEVICE_BT_SCO_RX;
  545. else if (strcmp((char*)name[i], "bt_sco_tx") == 0)
  546. index = DEVICE_BT_SCO_TX;
  547. else if (strcmp((char*)name[i], "dual_mic_stereo_tx") == 0)
  548. index = DEVICE_DUALMIC_HANDSET_TX;
  549. else
  550. continue;
  551. LOGV("index = %d", index);
  552.  
  553. device_list[index].dev_id = msm_get_device((char* )name[i]);
  554. if (device_list[index].dev_id >= 0) {
  555. LOGV("Found device: %s:index = %d, dev_id: %d", ( char* )name[i],
  556. index,device_list[index].dev_id);
  557. }
  558. device_list[index].class_id = msm_get_device_class(device_list[index].dev_id);
  559. device_list[index].capability = msm_get_device_capability(device_list[index].dev_id);
  560. LOGV("class ID = %d,capablity = %d for device %d", device_list[index].class_id,
  561. device_list[index].capability,
  562. device_list[index].dev_id);
  563. }
  564.  
  565. set_acoustic_parameters = (int (*)(void))::dlsym(acoustic, "set_acoustic_parameters");
  566. if ((*set_acoustic_parameters) == 0 ) {
  567. LOGE("Could not open set_acoustic_parameters()");
  568. return;
  569. }
  570.  
  571. int rc = set_acoustic_parameters();
  572. if (rc < 0) {
  573. LOGD("Could not set acoustic parameters to share memory: %d", rc);
  574. }
  575.  
  576. snd_get_num = (int (*)(void))::dlsym(acoustic, "snd_get_num");
  577. if ((*snd_get_num) == 0 ) {
  578. LOGD("Could not open snd_get_num()");
  579. }
  580.  
  581. mNumBTEndpoints = snd_get_num();
  582. LOGV("mNumBTEndpoints = %d", mNumBTEndpoints);
  583. mBTEndpoints = new msm_bt_endpoint[mNumBTEndpoints];
  584. mInit = true;
  585. LOGV("constructed %d SND endpoints)", mNumBTEndpoints);
  586. ept = mBTEndpoints;
  587. snd_get_bt_endpoint = (int (*)(msm_bt_endpoint *))::dlsym(acoustic, "snd_get_bt_endpoint");
  588. if ((*snd_get_bt_endpoint) == 0 ) {
  589. LOGE("Could not open snd_get_bt_endpoint()");
  590. return;
  591. }
  592. snd_get_bt_endpoint(mBTEndpoints);
  593.  
  594. for (int i = 0; i < mNumBTEndpoints; i++) {
  595. LOGV("BT name %s (tx,rx)=(%d,%d)", mBTEndpoints[i].name, mBTEndpoints[i].tx, mBTEndpoints[i].rx);
  596. }
  597.  
  598. char value[PROPERTY_VALUE_MAX];
  599. /* Check the system property for enable or not the ALT function */
  600. property_get("htc.audio.alt.enable", value, "0");
  601. alt_enable = atoi(value);
  602. LOGV("Enable ALT function: %d", alt_enable);
  603.  
  604. /* Check the system property for enable or not the HAC function */
  605. property_get("htc.audio.hac.enable", value, "0");
  606. hac_enable = atoi(value);
  607. LOGV("Enable HAC function: %d", hac_enable);
  608.  
  609. set_tpa2051_parameters = (int (*)(void))::dlsym(acoustic, "set_tpa2051_parameters");
  610. if ((*set_tpa2051_parameters) == 0) {
  611. LOGD("set_tpa2051_parameters() not present");
  612. support_tpa2051 = false;
  613. }
  614.  
  615. if (support_tpa2051) {
  616. rc = set_tpa2051_parameters();
  617. if (rc < 0) {
  618. LOGD("Speaker amplifies tpa2051 is not supported");
  619. support_tpa2051 = false;
  620. }
  621. }
  622.  
  623. set_aic3254_parameters = (int (*)(void))::dlsym(acoustic, "set_aic3254_parameters");
  624. if ((*set_aic3254_parameters) == 0 ) {
  625. LOGD("set_aic3254_parameters() not present");
  626. support_aic3254 = false;
  627. }
  628.  
  629. if (support_aic3254) {
  630. rc = set_aic3254_parameters();
  631. if (rc < 0) {
  632. LOGD("AIC3254 DSP is not supported");
  633. support_aic3254 = false;
  634. }
  635. }
  636.  
  637. support_back_mic = (int (*)(void))::dlsym(acoustic, "support_back_mic");
  638. if ((*support_back_mic) == 0 ) {
  639. LOGD("support_back_mic() not present");
  640. mDualMicEnabled = false;
  641. }
  642.  
  643. if (mDualMicEnabled) {
  644. rc = support_back_mic();
  645. if (rc < 0) {
  646. LOGD("DualMic is not supported");
  647. mDualMicEnabled = false;
  648. }
  649. }
  650.  
  651. mInit = true;
  652. }
  653.  
  654. AudioHardware::~AudioHardware() {
  655. for (size_t index = 0; index < mInputs.size(); index++) {
  656. closeInputStream((AudioStreamIn*)mInputs[index]);
  657. }
  658. mInputs.clear();
  659. closeOutputStream((AudioStreamOut*)mOutput);
  660. if (acoustic) {
  661. ::dlclose(acoustic);
  662. acoustic = 0;
  663. }
  664. msm_mixer_close();
  665. freeMemory();
  666.  
  667. mInit = false;
  668. }
  669.  
  670. status_t AudioHardware::initCheck() {
  671. return mInit ? NO_ERROR : NO_INIT;
  672. }
  673.  
  674. AudioStreamOut* AudioHardware::openOutputStream(uint32_t devices, int *format,
  675. uint32_t *channels, uint32_t *sampleRate,
  676. status_t *status) {
  677. { /* scope for the lock */
  678. Mutex::Autolock lock(mLock);
  679.  
  680. LOGD("AudioHardware::openOutputStream");
  681.  
  682. /* only one output stream allowed */
  683. if (mOutput) {
  684. if (status)
  685. *status = INVALID_OPERATION;
  686.  
  687. return 0;
  688. }
  689.  
  690. /* create new output stream */
  691. AudioStreamOutMSM72xx* out = new AudioStreamOutMSM72xx();
  692. status_t lStatus = out->set(this, devices, format, channels, sampleRate);
  693. if (status)
  694. *status = lStatus;
  695.  
  696. if (lStatus == NO_ERROR)
  697. mOutput = out;
  698. else
  699. delete out;
  700. }
  701. return mOutput;
  702. }
  703.  
  704. #ifdef WITH_QCOM_LPA
  705. AudioStreamOut* AudioHardware::openOutputSession(
  706. uint32_t devices, int *format, status_t *status, int sessionId) {
  707. AudioSessionOutMSM7xxx* out;
  708. { /* scope for the lock */
  709. Mutex::Autolock lock(mLock);
  710.  
  711. /* create new output stream */
  712. out = new AudioSessionOutMSM7xxx();
  713. status_t lStatus = out->set(this, devices, format, sessionId);
  714. if (status)
  715. *status = lStatus;
  716.  
  717. if (lStatus != NO_ERROR) {
  718. delete out;
  719. out = NULL;
  720. }
  721. }
  722. return out;
  723. }
  724. #endif
  725.  
  726. void AudioHardware::closeOutputStream(AudioStreamOut* out) {
  727. Mutex::Autolock lock(mLock);
  728. if (mOutput == 0 || mOutput != out)
  729. LOGW("Attempt to close invalid output stream");
  730. else {
  731. delete mOutput;
  732. mOutput = 0;
  733. }
  734. }
  735.  
  736. AudioStreamIn* AudioHardware::openInputStream(
  737. uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate, status_t *status,
  738. AudioSystem::audio_in_acoustics acoustic_flags) {
  739. /* check for valid input source */
  740. if (!AudioSystem::isInputDevice((AudioSystem::audio_devices)devices))
  741. return 0;
  742.  
  743. mLock.lock();
  744.  
  745. AudioStreamInMSM72xx* in = new AudioStreamInMSM72xx();
  746. status_t lStatus = in->set(this, devices, format, channels, sampleRate, acoustic_flags);
  747. if (status)
  748. *status = lStatus;
  749.  
  750. if (lStatus != NO_ERROR) {
  751. mLock.unlock();
  752. delete in;
  753. return 0;
  754. }
  755.  
  756. mInputs.add(in);
  757. mLock.unlock();
  758.  
  759. return in;
  760. }
  761.  
  762. void AudioHardware::closeInputStream(AudioStreamIn* in) {
  763. Mutex::Autolock lock(mLock);
  764.  
  765. ssize_t index = mInputs.indexOf((AudioStreamInMSM72xx *)in);
  766. if (index < 0)
  767. LOGW("Attempt to close invalid input stream");
  768. else {
  769. mLock.unlock();
  770. delete mInputs[index];
  771. mLock.lock();
  772. mInputs.removeAt(index);
  773. }
  774. }
  775.  
  776. status_t AudioHardware::setMode(int mode) {
  777. int prevMode = mMode;
  778. status_t status = AudioHardwareBase::setMode(mode);
  779. if (status == NO_ERROR) {
  780. /* make sure that doAudioRouteOrMute() is called by doRouting()
  781. even if the new device selected is the same as current one. */
  782. if (((prevMode != AudioSystem::MODE_IN_CALL) && (mMode == AudioSystem::MODE_IN_CALL)) ||
  783. ((prevMode == AudioSystem::MODE_IN_CALL) && (mMode != AudioSystem::MODE_IN_CALL))) {
  784. clearCurDevice();
  785. }
  786. }
  787. return status;
  788. }
  789.  
  790. bool AudioHardware::checkOutputStandby() {
  791. if (mOutput)
  792. if (!mOutput->checkStandby())
  793. return false;
  794.  
  795. return true;
  796. }
  797.  
  798. status_t AudioHardware::setMicMute(bool state) {
  799. Mutex::Autolock lock(mLock);
  800. return setMicMute_nosync(state);
  801. }
  802.  
  803. /* always call with mutex held */
  804. status_t AudioHardware::setMicMute_nosync(bool state) {
  805. if (mMicMute != state) {
  806. mMicMute = state;
  807. LOGD("setMicMute_nosync calling voice mute with the mMicMute %d", mMicMute);
  808. msm_set_voice_tx_mute(mMicMute);
  809. }
  810. return NO_ERROR;
  811. }
  812.  
  813. status_t AudioHardware::getMicMute(bool* state) {
  814. *state = mMicMute;
  815. return NO_ERROR;
  816. }
  817.  
  818. status_t AudioHardware::setParameters(const String8& keyValuePairs) {
  819. AudioParameter param = AudioParameter(keyValuePairs);
  820. String8 value;
  821. String8 key;
  822. const char BT_NREC_KEY[] = "bt_headset_nrec";
  823. const char BT_NAME_KEY[] = "bt_headset_name";
  824. const char BT_NREC_VALUE_ON[] = "on";
  825. const char HAC_KEY[] = "HACSetting";
  826. const char HAC_VALUE_ON[] = "ON";
  827. const char ACTIVE_AP[] = "active_ap";
  828. const char EFFECT_ENABLED[] = "sound_effect_enable";
  829.  
  830. LOGV("setParameters() %s", keyValuePairs.string());
  831.  
  832. if (keyValuePairs.length() == 0) return BAD_VALUE;
  833.  
  834. if (hac_enable) {
  835. key = String8(HAC_KEY);
  836. if (param.get(key, value) == NO_ERROR) {
  837. if (value == HAC_VALUE_ON) {
  838. mHACSetting = true;
  839. LOGD("Enable HAC");
  840. } else {
  841. mHACSetting = false;
  842. LOGD("Disable HAC");
  843. }
  844. }
  845. }
  846.  
  847. key = String8(BT_NREC_KEY);
  848. if (param.get(key, value) == NO_ERROR) {
  849. if (value == BT_NREC_VALUE_ON)
  850. mBluetoothNrec = true;
  851. else {
  852. mBluetoothNrec = false;
  853. LOGI("Turning noise reduction and echo cancellation off for BT "
  854. "headset");
  855. }
  856. }
  857.  
  858. key = String8(BT_NAME_KEY);
  859. if (param.get(key, value) == NO_ERROR) {
  860. doRouting(NULL);
  861. }
  862.  
  863. key = String8(DUALMIC_KEY);
  864. if (param.get(key, value) == NO_ERROR) {
  865. if (value == "true") {
  866. mDualMicEnabled = true;
  867. LOGI("DualMike feature Enabled");
  868. } else {
  869. mDualMicEnabled = false;
  870. LOGI("DualMike feature Disabled");
  871. }
  872. doRouting(NULL);
  873. }
  874.  
  875. key = String8(TTY_MODE_KEY);
  876. if (param.get(key, value) == NO_ERROR) {
  877. if (value == "full")
  878. mTtyMode = TTY_FULL;
  879. else if (value == "hco")
  880. mTtyMode = TTY_HCO;
  881. else if (value == "vco")
  882. mTtyMode = TTY_VCO;
  883. else
  884. mTtyMode = TTY_OFF;
  885.  
  886. if (mMode != AudioSystem::MODE_IN_CALL)
  887. return NO_ERROR;
  888.  
  889. LOGI("Changed TTY Mode=%s", value.string());
  890. doRouting(NULL);
  891. }
  892.  
  893. key = String8(ACTIVE_AP);
  894. if (param.get(key, value) == NO_ERROR) {
  895. const char* active_ap = value.string();
  896. strcpy(mActiveAP, active_ap);
  897. LOGD("Active AP = %s", active_ap);
  898.  
  899. key = String8(EFFECT_ENABLED);
  900. if (param.get(key, value) == NO_ERROR) {
  901. const char* sound_effect_enable = value.string();
  902. LOGD("Sound Effect Enabled = %s", sound_effect_enable);
  903. if (value == "on")
  904. mEffectEnabled = true;
  905. else
  906. mEffectEnabled = false;
  907. }
  908.  
  909. key = String8(DSP_EFFECT_KEY);
  910. if (param.get(key, value) == NO_ERROR) {
  911. LOGI("DSP Effect = %s", value.string());
  912. aic3254_config(get_snd_dev(), active_ap, value.string());
  913. }
  914. }
  915.  
  916. return NO_ERROR;
  917. }
  918.  
  919. String8 AudioHardware::getParameters(const String8& keys) {
  920. AudioParameter param = AudioParameter(keys);
  921. String8 value;
  922.  
  923. String8 key = String8(DUALMIC_KEY);
  924. if (param.get(key, value) == NO_ERROR) {
  925. value = String8(mDualMicEnabled ? "true" : "false");
  926. param.add(key, value);
  927. }
  928.  
  929. key = String8(DSP_EFFECT_KEY);
  930. if (param.get(key, value) == NO_ERROR) {
  931. value = String8(mCurDspProfile);
  932. param.add(key, value);
  933. }
  934.  
  935. #ifdef WITH_QCOM_SPEECH
  936. key = String8("tunneled-input-formats");
  937. if (param.get(key,value) == NO_ERROR) {
  938. param.addInt(String8("AMR"), true );
  939. param.addInt(String8("QCELP"), true );
  940. param.addInt(String8("EVRC"), true );
  941. }
  942. #endif
  943.  
  944. LOGV("AudioHardware::getParameters() %s", param.toString().string());
  945. return param.toString();
  946. }
  947.  
  948. size_t AudioHardware::getInputBufferSize(uint32_t sampleRate, int format, int channelCount) {
  949. if ((format != AudioSystem::PCM_16_BIT) &&
  950. #ifdef WITH_QCOM_SPEECH
  951. (format != AudioSystem::AMR_NB) &&
  952. (format != AudioSystem::EVRC) &&
  953. (format != AudioSystem::QCELP) &&
  954. #endif
  955. (format != AudioSystem::AAC)){
  956. LOGW("getInputBufferSize bad format: %d", format);
  957. return 0;
  958. }
  959. if (channelCount < 1 || channelCount > 2) {
  960. LOGW("getInputBufferSize bad channel count: %d", channelCount);
  961. return 0;
  962. }
  963.  
  964. #ifdef WITH_QCOM_SPEECH
  965. if (format == AudioSystem::AMR_NB)
  966. return 320*channelCount;
  967. else if (format == AudioSystem::EVRC)
  968. return 230*channelCount;
  969. else if (format == AudioSystem::QCELP)
  970. return 350*channelCount;
  971. else if (format == AudioSystem::AAC)
  972. #else
  973. if (format == AudioSystem::AAC)
  974. #endif
  975. return 2048;
  976. else
  977. return 2048*channelCount;
  978. }
  979.  
  980. static status_t set_volume_rpc(uint32_t device,
  981. uint32_t method,
  982. uint32_t volume) {
  983. LOGV("set_volume_rpc(%d, %d, %d)", device, method, volume);
  984.  
  985. return NO_ERROR;
  986. }
  987.  
  988. status_t AudioHardware::setVoiceVolume(float v) {
  989. if (v < 0.0) {
  990. LOGW("setVoiceVolume(%f) under 0.0, assuming 0.0", v);
  991. v = 0.0;
  992. } else if (v > 1.0) {
  993. LOGW("setVoiceVolume(%f) over 1.0, assuming 1.0", v);
  994. v = 1.0;
  995. }
  996.  
  997. int vol = lrint(v * 100.0);
  998. if (mCurSndDevice == SND_DEVICE_HAC) {
  999. LOGI("HAC enable: Setting in-call volume to maximum.");
  1000. if (msm_set_voice_rx_vol(VOICE_VOLUME_MAX)) {
  1001. LOGE("msm_set_voice_rx_vol(%d) failed errno = %d", VOICE_VOLUME_MAX, errno);
  1002. return -1;
  1003. }
  1004. } else {
  1005. LOGD("setVoiceVolume(%f)", v);
  1006. LOGI("Setting in-call volume to %d (available range is 0 to 100)", vol);
  1007.  
  1008. if (msm_set_voice_rx_vol(vol)) {
  1009. LOGE("msm_set_voice_rx_vol(%d) failed errno = %d", vol, errno);
  1010. return -1;
  1011. }
  1012. }
  1013.  
  1014. mVoiceVolume = vol;
  1015. LOGV("msm_set_voice_rx_vol(%d) succeeded", vol);
  1016.  
  1017. if (mMode == AudioSystem::MODE_IN_CALL &&
  1018. mCurSndDevice != SND_DEVICE_BT &&
  1019. mCurSndDevice != SND_DEVICE_CARKIT &&
  1020. mCurSndDevice != SND_DEVICE_BT_EC_OFF)
  1021. {
  1022. uint32_t new_tx_acdb = getACDB(MOD_TX, mCurSndDevice);
  1023. uint32_t new_rx_acdb = getACDB(MOD_RX, mCurSndDevice);
  1024.  
  1025. int (*update_voice_acdb)(uint32_t, uint32_t);
  1026.  
  1027. update_voice_acdb = (int (*)(uint32_t, uint32_t))::dlsym(acoustic, "update_voice_acdb");
  1028. if ((*update_voice_acdb) == 0 ) {
  1029. LOGE("Could not open update_voice_acdb()");
  1030. }
  1031.  
  1032. int rc = update_voice_acdb(new_tx_acdb, new_rx_acdb);
  1033. if (rc < 0)
  1034. LOGE("Could not set update_voice_acdb: %d", rc);
  1035. else
  1036. LOGI("update_voice_acdb(%d, %d) succeeded", new_tx_acdb, new_rx_acdb);
  1037. }
  1038.  
  1039. return NO_ERROR;
  1040. }
  1041.  
  1042. static status_t set_volume_fm(uint32_t volume) {
  1043. int returnval = 0;
  1044. float ratio = 2.5;
  1045.  
  1046. char s1[100] = "hcitool cmd 0x3f 0xa 0x5 0xc0 0x41 0xf 0 0x20 0 0 0";
  1047. char s2[100] = "hcitool cmd 0x3f 0xa 0x5 0xe4 0x41 0xf 0 0x00 0 0 0";
  1048. char s3[100] = "hcitool cmd 0x3f 0xa 0x5 0xe0 0x41 0xf 0 ";
  1049.  
  1050. char stemp[10] = "";
  1051. char *pstarget = s3;
  1052.  
  1053. volume = (unsigned int)(volume * ratio);
  1054.  
  1055. sprintf(stemp, "0x%x ", volume);
  1056. pstarget = strcat(s3, stemp);
  1057. pstarget = strcat(s3, "0 0 0");
  1058.  
  1059. system(s1);
  1060. system(s2);
  1061. system(s3);
  1062.  
  1063. return returnval;
  1064. }
  1065.  
  1066. status_t AudioHardware::setFmVolume(float v) {
  1067. int vol = AudioSystem::logToLinear(v);
  1068.  
  1069. if (vol > 100)
  1070. vol = 100;
  1071. else if (vol < 0)
  1072. vol = 0;
  1073.  
  1074. LOGV("setFmVolume %d", vol);
  1075. set_volume_fm(vol);
  1076. return NO_ERROR;
  1077. }
  1078.  
  1079. status_t AudioHardware::setMasterVolume(float v) {
  1080. Mutex::Autolock lock(mLock);
  1081. int vol = ceil(v * 7.0);
  1082. LOGI("Set master volume to %d.", vol);
  1083.  
  1084. set_volume_rpc(SND_DEVICE_HANDSET, SND_METHOD_VOICE, vol);
  1085. set_volume_rpc(SND_DEVICE_SPEAKER, SND_METHOD_VOICE, vol);
  1086. set_volume_rpc(SND_DEVICE_BT, SND_METHOD_VOICE, vol);
  1087. set_volume_rpc(SND_DEVICE_HEADSET, SND_METHOD_VOICE, vol);
  1088. /* TBD - does HDMI require this handling
  1089.  
  1090. We return an error code here to let the audioflinger do in-software
  1091. volume on top of the maximum volume that we set through the SND API.
  1092. return error - software mixer will handle it */
  1093.  
  1094. return -1;
  1095. }
  1096.  
  1097. status_t get_batt_temp(int *batt_temp) {
  1098. LOGD("Enable ALT for speaker");
  1099.  
  1100. int i, fd, len;
  1101. char get_batt_temp[6] = { 0 };
  1102. const char *fn[] = {
  1103. "/sys/devices/platform/rs30100001:00000000.0/power_supply/battery/batt_temp",
  1104. "/sys/devices/platform/rs30100001:00000000/power_supply/battery/batt_temp" };
  1105.  
  1106. for (i=0; i<2; i++) {
  1107. if ((fd = open(fn[i], O_RDONLY)) >= 0)
  1108. break;
  1109. }
  1110. if (fd <= 0) {
  1111. LOGE("Couldn't open sysfs file batt_temp");
  1112. return UNKNOWN_ERROR;
  1113. }
  1114.  
  1115. if ((len = read(fd, get_batt_temp, sizeof(get_batt_temp))) <= 1) {
  1116. LOGE("read battery temp fail: %s", strerror(errno));
  1117. close(fd);
  1118. return BAD_VALUE;
  1119. }
  1120.  
  1121. *batt_temp = strtol(get_batt_temp, NULL, 10);
  1122. LOGD("ALT batt_temp = %d", *batt_temp);
  1123.  
  1124. close(fd);
  1125. return NO_ERROR;
  1126. }
  1127.  
  1128. status_t do_tpa2051_control(int mode)
  1129. {
  1130. int fd, rc;
  1131. int tpa_mode = 0;
  1132. int batt_temp = 0;
  1133.  
  1134. if (mode) {
  1135. if (cur_rx == DEVICE_HEADSET_RX)
  1136. tpa_mode = TPA2051_MODE_VOICECALL_HEADSET;
  1137. else if (cur_rx == DEVICE_SPEAKER_RX)
  1138. tpa_mode = TPA2051_MODE_VOICECALL_SPKR;
  1139. } else {
  1140. switch (cur_rx) {
  1141. case DEVICE_FMRADIO_HEADSET_RX:
  1142. tpa_mode = TPA2051_MODE_FM_HEADSET;
  1143. break;
  1144. case DEVICE_FMRADIO_SPEAKER_RX:
  1145. tpa_mode = TPA2051_MODE_FM_SPKR;
  1146. break;
  1147. case DEVICE_SPEAKER_HEADSET_RX:
  1148. tpa_mode = TPA2051_MODE_RING;
  1149. break;
  1150. case DEVICE_HEADSET_RX:
  1151. tpa_mode = TPA2051_MODE_PLAYBACK_HEADSET;
  1152. break;
  1153. case DEVICE_SPEAKER_RX:
  1154. tpa_mode = TPA2051_MODE_PLAYBACK_SPKR;
  1155. break;
  1156. default:
  1157. break;
  1158. }
  1159. }
  1160.  
  1161. fd = open("/dev/tpa2051d3", O_RDWR);
  1162. if (fd < 0) {
  1163. LOGE("can't open /dev/tpa2051d3 %d", fd);
  1164. return -1;
  1165. }
  1166.  
  1167. if (tpa_mode) {
  1168. rc = ioctl(fd, TPA2051_SET_MODE, &tpa_mode);
  1169. if (rc < 0)
  1170. LOGE("ioctl TPA2051_SET_MODE failed: %s", strerror(errno));
  1171. else
  1172. LOGD("update TPA2051_SET_MODE to mode %d success", tpa_mode);
  1173. }
  1174.  
  1175. if (alt_enable && cur_rx == DEVICE_SPEAKER_RX) {
  1176. if (get_batt_temp(&batt_temp) == NO_ERROR) {
  1177. if (batt_temp < 50) {
  1178. tpa_mode = 629276672;
  1179. rc = ioctl(fd, TPA2051_SET_CONFIG, &tpa_mode);
  1180. if (rc < 0)
  1181. LOGE("ioctl TPA2051_SET_CONFIG failed: %s", strerror(errno));
  1182. else
  1183. LOGD("update TPA2051_SET_CONFIG to mode %d success", tpa_mode);
  1184. }
  1185. }
  1186. }
  1187.  
  1188. close(fd);
  1189. return 0;
  1190. }
  1191.  
  1192. static status_t do_route_audio_rpc(uint32_t device,
  1193. bool ear_mute, bool mic_mute,
  1194. uint32_t rx_acdb_id, uint32_t tx_acdb_id) {
  1195. uint32_t new_rx_device = INVALID_DEVICE, new_tx_device = INVALID_DEVICE;
  1196.  
  1197. Routing_table* temp = NULL;
  1198. LOGV("do_route_audio_rpc(%d, %d, %d)", device, ear_mute, mic_mute);
  1199.  
  1200. switch (device) {
  1201. case SND_DEVICE_HANDSET:
  1202. new_rx_device = DEVICE_HANDSET_RX;
  1203. new_tx_device = DEVICE_HANDSET_TX;
  1204. LOGV("In HANDSET");
  1205. break;
  1206. case SND_DEVICE_SPEAKER:
  1207. new_rx_device = DEVICE_SPEAKER_RX;
  1208. new_tx_device = DEVICE_SPEAKER_TX;
  1209. LOGV("In SPEAKER");
  1210. break;
  1211. case SND_DEVICE_HEADSET:
  1212. new_rx_device = DEVICE_HEADSET_RX;
  1213. new_tx_device = DEVICE_HEADSET_TX;
  1214. LOGV("In HEADSET");
  1215. break;
  1216. case SND_DEVICE_NO_MIC_HEADSET:
  1217. new_rx_device = DEVICE_HEADSET_RX;
  1218. new_tx_device = DEVICE_HANDSET_TX;
  1219. LOGV("In NO MIC HEADSET");
  1220. break;
  1221. case SND_DEVICE_FM_HANDSET:
  1222. new_rx_device = DEVICE_FMRADIO_HANDSET_RX;
  1223. new_tx_device = DEVICE_HANDSET_TX;
  1224. LOGV("In FM HANDSET");
  1225. break;
  1226. case SND_DEVICE_FM_SPEAKER:
  1227. new_rx_device = DEVICE_FMRADIO_SPEAKER_RX;
  1228. new_tx_device = DEVICE_HANDSET_TX;
  1229. LOGV("In FM SPEAKER");
  1230. break;
  1231. case SND_DEVICE_FM_HEADSET:
  1232. new_rx_device = DEVICE_FMRADIO_HEADSET_RX;
  1233. new_tx_device = DEVICE_HANDSET_TX;
  1234. LOGV("In FM HEADSET");
  1235. break;
  1236. case SND_DEVICE_IN_S_SADC_OUT_HANDSET:
  1237. new_rx_device = DEVICE_HANDSET_RX;
  1238. new_tx_device = DEVICE_DUALMIC_HANDSET_TX;
  1239. LOGV("In DUALMIC_HANDSET");
  1240. break;
  1241. /*
  1242. case SND_DEVICE_IN_S_SADC_OUT_SPEAKER_PHONE:
  1243. new_rx_device = DEVICE_SPEAKER_RX;
  1244. new_tx_device = DEVICE_DUALMIC_SPEAKER_TX;
  1245. LOGV("In DUALMIC_SPEAKER");
  1246. break;
  1247. */
  1248. case SND_DEVICE_TTY_FULL:
  1249. new_rx_device = DEVICE_TTY_HEADSET_MONO_RX;
  1250. new_tx_device = DEVICE_TTY_HEADSET_MONO_TX;
  1251. LOGV("In TTY_FULL");
  1252. break;
  1253. case SND_DEVICE_TTY_VCO:
  1254. new_rx_device = DEVICE_TTY_HEADSET_MONO_RX;
  1255. new_tx_device = DEVICE_HANDSET_TX;
  1256. LOGV("In TTY_VCO");
  1257. break;
  1258. case SND_DEVICE_TTY_HCO:
  1259. new_rx_device = DEVICE_HANDSET_RX;
  1260. new_tx_device = DEVICE_TTY_HEADSET_MONO_TX;
  1261. LOGV("In TTY_HCO");
  1262. break;
  1263. case SND_DEVICE_BT:
  1264. case SND_DEVICE_BT_EC_OFF:
  1265. new_rx_device = DEVICE_BT_SCO_RX;
  1266. new_tx_device = DEVICE_BT_SCO_TX;
  1267. LOGV("In BT_HCO");
  1268. break;
  1269. case SND_DEVICE_HEADSET_AND_SPEAKER:
  1270. new_rx_device = DEVICE_SPEAKER_HEADSET_RX;
  1271. new_tx_device = DEVICE_HEADSET_TX;
  1272. LOGV("In DEVICE_SPEAKER_HEADSET_RX and DEVICE_HEADSET_TX");
  1273. case SND_DEVICE_HEADPHONE_AND_SPEAKER:
  1274. new_rx_device = DEVICE_SPEAKER_HEADSET_RX;
  1275. new_tx_device = DEVICE_HANDSET_TX;
  1276. LOGV("In DEVICE_SPEAKER_HEADSET_RX and DEVICE_HANDSET_TX");
  1277. break;
  1278. case SND_DEVICE_USB_HEADSET:
  1279. new_rx_device = DEVICE_USB_HEADSET_RX;
  1280. new_tx_device = DEVICE_SPEAKER_TX;
  1281. LOGV("In USB_HEADSET");
  1282. break;
  1283. case SND_DEVICE_HAC:
  1284. new_rx_device = DEVICE_HAC_RX;
  1285. new_tx_device = DEVICE_HANDSET_TX;
  1286. LOGV("In HAC");
  1287. break;
  1288. /* No framework support
  1289. case SND_DEVICE_HDMI:
  1290. new_rx_device = DEVICE_HDMI_STERO_RX;
  1291. new_tx_device = cur_tx;
  1292. LOGV("In DEVICE_HDMI_STERO_RX and cur_tx");
  1293. break;
  1294. */
  1295. default:
  1296. break;
  1297. }
  1298.  
  1299. if(new_rx_device != INVALID_DEVICE)
  1300. LOGD("new_rx = %d", DEV_ID(new_rx_device));
  1301. if(new_tx_device != INVALID_DEVICE)
  1302. LOGD("new_tx = %d", DEV_ID(new_tx_device));
  1303.  
  1304. if (ear_mute == false && !isStreamOn(VOICE_CALL)) {
  1305. LOGV("Going to enable RX/TX device for voice stream");
  1306. /* Routing Voice */
  1307. if ( (new_rx_device != INVALID_DEVICE) && (new_tx_device != INVALID_DEVICE)) {
  1308. LOGD("Starting voice on Rx %d and Tx %d device", DEV_ID(new_rx_device), DEV_ID(new_tx_device));
  1309. updateACDB(new_rx_device, new_tx_device, rx_acdb_id, tx_acdb_id);
  1310. msm_route_voice(DEV_ID(new_rx_device), DEV_ID(new_tx_device), 1);
  1311. } else
  1312. return -1;
  1313.  
  1314. if (cur_rx == INVALID_DEVICE || new_rx_device == INVALID_DEVICE)
  1315. return -1;
  1316.  
  1317. if (cur_tx == INVALID_DEVICE || new_tx_device == INVALID_DEVICE)
  1318. return -1;
  1319.  
  1320. /* Enable RX device */
  1321. if (new_rx_device != cur_rx)
  1322. enableDevice(cur_rx, 0);
  1323.  
  1324. enableDevice(new_rx_device, 1);
  1325.  
  1326. /* Enable TX device */
  1327. if (new_tx_device != cur_tx)
  1328. enableDevice(cur_tx, 0);
  1329.  
  1330. enableDevice(new_tx_device, 1);
  1331.  
  1332. /* start Voice call */
  1333. LOGD("Starting voice call and UnMuting the call");
  1334. msm_start_voice();
  1335. msm_set_voice_tx_mute(0);
  1336. cur_rx = new_rx_device;
  1337. cur_tx = new_tx_device;
  1338. addToTable(0, cur_rx, cur_tx, VOICE_CALL, true);
  1339. updateDeviceInfo(new_rx_device, new_tx_device, rx_acdb_id, tx_acdb_id);
  1340. } else if (ear_mute == true && isStreamOnAndActive(VOICE_CALL)) {
  1341. LOGV("Going to disable RX/TX device during end of voice call");
  1342. temp = getNodeByStreamType(VOICE_CALL);
  1343. if (temp == NULL)
  1344. return 0;
  1345.  
  1346. /* Ending voice call */
  1347. LOGD("Ending Voice call");
  1348. msm_end_voice();
  1349. deleteFromTable(VOICE_CALL);
  1350. updateDeviceInfo(new_rx_device, new_tx_device, 0, 0);
  1351. if (new_rx_device != INVALID_DEVICE && new_tx_device != INVALID_DEVICE) {
  1352. cur_rx = new_rx_device;
  1353. cur_tx = new_tx_device;
  1354. }
  1355. } else {
  1356. updateDeviceInfo(new_rx_device, new_tx_device, rx_acdb_id, tx_acdb_id);
  1357. }
  1358.  
  1359. if (support_tpa2051)
  1360. do_tpa2051_control(ear_mute ^1);
  1361.  
  1362. return NO_ERROR;
  1363. }
  1364.  
  1365. status_t AudioHardware::doAudioRouteOrMute(uint32_t device) {
  1366. uint32_t rx_acdb_id = 0;
  1367. uint32_t tx_acdb_id = 0;
  1368.  
  1369. if (device == SND_DEVICE_BT) {
  1370. if (!mBluetoothNrec)
  1371. device = SND_DEVICE_BT_EC_OFF;
  1372. }
  1373.  
  1374. if (support_aic3254) {
  1375. aic3254_config(device, "", "");
  1376. do_aic3254_control(device);
  1377. }
  1378.  
  1379. if (device == SND_DEVICE_BT) {
  1380. if (mBluetoothIdTx != 0) {
  1381. rx_acdb_id = mBluetoothIdRx;
  1382. tx_acdb_id = mBluetoothIdTx;
  1383. } else {
  1384. /* use default BT entry defined in AudioBTID.csv */
  1385. rx_acdb_id = mBTEndpoints[0].rx;
  1386. tx_acdb_id = mBTEndpoints[0].tx;
  1387. LOGD("Update ACDB ID to default BT setting");
  1388. }
  1389. } else if (device == SND_DEVICE_CARKIT ||
  1390. device == SND_DEVICE_BT_EC_OFF) {
  1391. if (mBluetoothIdTx != 0) {
  1392. rx_acdb_id = mBluetoothIdRx;
  1393. tx_acdb_id = mBluetoothIdTx;
  1394. } else {
  1395. /* use default carkit entry defined in AudioBTID.csv */
  1396. rx_acdb_id = mBTEndpoints[1].rx;
  1397. tx_acdb_id = mBTEndpoints[1].tx;
  1398. LOGD("Update ACDB ID to default carkit setting");
  1399. }
  1400. } else if (mMode == AudioSystem::MODE_IN_CALL
  1401. && hac_enable && mHACSetting &&
  1402. device == SND_DEVICE_HANDSET) {
  1403. LOGD("Update acdb id to hac profile.");
  1404. rx_acdb_id = ACDB_ID_HAC_HANDSET_SPKR;
  1405. tx_acdb_id = ACDB_ID_HAC_HANDSET_MIC;
  1406. } else {
  1407. if (mMode == AudioSystem::MODE_IN_CALL) {
  1408. rx_acdb_id = getACDB(MOD_RX, device);
  1409. tx_acdb_id = getACDB(MOD_TX, device);
  1410. } else {
  1411. if (!checkOutputStandby())
  1412. rx_acdb_id = getACDB(MOD_PLAY, device);
  1413.  
  1414. if (mRecordState)
  1415. tx_acdb_id = getACDB(MOD_REC, device);
  1416. }
  1417. }
  1418.  
  1419. LOGV("doAudioRouteOrMute: rx acdb %d, tx acdb %d", rx_acdb_id, tx_acdb_id);
  1420. LOGV("doAudioRouteOrMute() device %d, mMode %d, mMicMute %d", device, mMode, mMicMute);
  1421.  
  1422. return do_route_audio_rpc(device, mMode != AudioSystem::MODE_IN_CALL,
  1423. mMicMute, rx_acdb_id, tx_acdb_id);
  1424. }
  1425.  
  1426. status_t AudioHardware::get_mMode(void) {
  1427. return mMode;
  1428. }
  1429.  
  1430. status_t AudioHardware::set_mRecordState(bool onoff) {
  1431. mRecordState = onoff;
  1432. return 0;
  1433. }
  1434.  
  1435. status_t AudioHardware::get_mRecordState(void) {
  1436. return mRecordState;
  1437. }
  1438.  
  1439. status_t AudioHardware::get_snd_dev(void) {
  1440. Mutex::Autolock lock(mLock);
  1441. return mCurSndDevice;
  1442. }
  1443.  
  1444. uint32_t AudioHardware::getACDB(int mode, uint32_t device) {
  1445. uint32_t acdb_id = 0;
  1446. int batt_temp = 0;
  1447.  
  1448. if (mMode == AudioSystem::MODE_IN_CALL &&
  1449. device <= SND_DEVICE_NO_MIC_HEADSET) {
  1450. if (mode == MOD_RX) {
  1451. switch (device) {
  1452. case SND_DEVICE_HANDSET:
  1453. acdb_id = mVoiceVolume / 20 + 201;
  1454. break;
  1455. case SND_DEVICE_HEADSET:
  1456. case SND_DEVICE_NO_MIC_HEADSET:
  1457. acdb_id = mVoiceVolume / 20 + 401;
  1458. break;
  1459. case SND_DEVICE_SPEAKER:
  1460. acdb_id = mVoiceVolume / 20 + 601;
  1461. break;
  1462. default:
  1463. break;
  1464. }
  1465. } else if (mode == MOD_TX) {
  1466. switch (device) {
  1467. case SND_DEVICE_HANDSET:
  1468. acdb_id = mVoiceVolume / 20 + 101;
  1469. break;
  1470. case SND_DEVICE_HEADSET:
  1471. case SND_DEVICE_NO_MIC_HEADSET:
  1472. acdb_id = mVoiceVolume / 20 + 301;
  1473. break;
  1474. case SND_DEVICE_SPEAKER:
  1475. acdb_id = mVoiceVolume / 20 + 501;
  1476. break;
  1477. default:
  1478. break;
  1479. }
  1480. }
  1481. } else {
  1482. if (mode == MOD_PLAY) {
  1483. switch (device) {
  1484. case SND_DEVICE_HEADSET:
  1485. case SND_DEVICE_NO_MIC_HEADSET:
  1486. case SND_DEVICE_NO_MIC_HEADSET_BACK_MIC:
  1487. case SND_DEVICE_FM_HEADSET:
  1488. acdb_id = ACDB_ID_HEADSET_PLAYBACK;
  1489. break;
  1490. case SND_DEVICE_SPEAKER:
  1491. case SND_DEVICE_FM_SPEAKER:
  1492. case SND_DEVICE_SPEAKER_BACK_MIC:
  1493. acdb_id = ACDB_ID_SPKR_PLAYBACK;
  1494. if (alt_enable) {
  1495. if (get_batt_temp(&batt_temp) == NO_ERROR) {
  1496. if (batt_temp < 50)
  1497. acdb_id = ACDB_ID_ALT_SPKR_PLAYBACK;
  1498. }
  1499. }
  1500. break;
  1501. case SND_DEVICE_HEADSET_AND_SPEAKER:
  1502. case SND_DEVICE_HEADSET_AND_SPEAKER_BACK_MIC:
  1503. acdb_id = ACDB_ID_HEADSET_RINGTONE_PLAYBACK;
  1504. break;
  1505. default:
  1506. break;
  1507. }
  1508. } else if (mode == MOD_REC) {
  1509. switch (device) {
  1510. case SND_DEVICE_HEADSET:
  1511. case SND_DEVICE_FM_HEADSET:
  1512. case SND_DEVICE_FM_SPEAKER:
  1513. case SND_DEVICE_HEADSET_AND_SPEAKER:
  1514. acdb_id = ACDB_ID_EXT_MIC_REC;
  1515. break;
  1516. case SND_DEVICE_HANDSET:
  1517. case SND_DEVICE_NO_MIC_HEADSET:
  1518. case SND_DEVICE_SPEAKER:
  1519. acdb_id = ACDB_ID_INT_MIC_REC;
  1520. break;
  1521. case SND_DEVICE_SPEAKER_BACK_MIC:
  1522. case SND_DEVICE_NO_MIC_HEADSET_BACK_MIC:
  1523. case SND_DEVICE_HANDSET_BACK_MIC:
  1524. case SND_DEVICE_HEADSET_AND_SPEAKER_BACK_MIC:
  1525. acdb_id = ACDB_ID_CAMCORDER;
  1526. break;
  1527. default:
  1528. break;
  1529. }
  1530. }
  1531. }
  1532.  
  1533. LOGV("getACDB, return ID %d", acdb_id);
  1534. return acdb_id;
  1535. }
  1536.  
  1537. status_t AudioHardware::do_aic3254_control(uint32_t device) {
  1538. LOGD("do_aic3254_control device: %d mode: %d record: %d", device, mMode, mRecordState);
  1539.  
  1540. uint32_t new_aic_txmode = UPLINK_OFF;
  1541. uint32_t new_aic_rxmode = DOWNLINK_OFF;
  1542.  
  1543. if (mMode == AudioSystem::MODE_IN_CALL) {
  1544. switch (device ) {
  1545. case SND_DEVICE_HEADSET:
  1546. new_aic_rxmode = CALL_DOWNLINK_EMIC_HEADSET;
  1547. new_aic_txmode = CALL_UPLINK_EMIC_HEADSET;
  1548. break;
  1549. case SND_DEVICE_SPEAKER:
  1550. case SND_DEVICE_SPEAKER_BACK_MIC:
  1551. new_aic_rxmode = CALL_DOWNLINK_IMIC_SPEAKER;
  1552. new_aic_txmode = CALL_UPLINK_IMIC_SPEAKER;
  1553. break;
  1554. case SND_DEVICE_HEADSET_AND_SPEAKER:
  1555. case SND_DEVICE_HEADSET_AND_SPEAKER_BACK_MIC:
  1556. new_aic_rxmode = RING_HEADSET_SPEAKER;
  1557. break;
  1558. case SND_DEVICE_NO_MIC_HEADSET:
  1559. case SND_DEVICE_NO_MIC_HEADSET_BACK_MIC:
  1560. new_aic_rxmode = CALL_DOWNLINK_IMIC_HEADSET;
  1561. new_aic_txmode = CALL_UPLINK_IMIC_HEADSET;
  1562. break;
  1563. case SND_DEVICE_HANDSET:
  1564. case SND_DEVICE_HANDSET_BACK_MIC:
  1565. case SND_DEVICE_IN_S_SADC_OUT_HANDSET:
  1566. new_aic_rxmode = CALL_DOWNLINK_IMIC_RECEIVER;
  1567. new_aic_txmode = CALL_UPLINK_IMIC_RECEIVER;
  1568. break;
  1569. default:
  1570. break;
  1571. }
  1572. } else {
  1573. if (checkOutputStandby()) {
  1574. if (device == SND_DEVICE_FM_HEADSET) {
  1575. new_aic_rxmode = FM_OUT_HEADSET;
  1576. new_aic_txmode = FM_IN_HEADSET;
  1577. } else if (device == SND_DEVICE_FM_SPEAKER) {
  1578. new_aic_rxmode = FM_OUT_SPEAKER;
  1579. new_aic_txmode = FM_IN_SPEAKER;
  1580. }
  1581. } else {
  1582. switch (device) {
  1583. case SND_DEVICE_HEADSET_AND_SPEAKER:
  1584. case SND_DEVICE_HEADSET_AND_SPEAKER_BACK_MIC:
  1585. new_aic_rxmode = RING_HEADSET_SPEAKER;
  1586. break;
  1587. case SND_DEVICE_SPEAKER:
  1588. case SND_DEVICE_SPEAKER_BACK_MIC:
  1589. new_aic_rxmode = PLAYBACK_SPEAKER;
  1590. break;
  1591. case SND_DEVICE_HANDSET:
  1592. case SND_DEVICE_HANDSET_BACK_MIC:
  1593. new_aic_rxmode = PLAYBACK_RECEIVER;
  1594. break;
  1595. case SND_DEVICE_HEADSET:
  1596. case SND_DEVICE_NO_MIC_HEADSET:
  1597. case SND_DEVICE_NO_MIC_HEADSET_BACK_MIC:
  1598. new_aic_rxmode = PLAYBACK_HEADSET;
  1599. break;
  1600. default:
  1601. break;
  1602. }
  1603. }
  1604.  
  1605. if (mRecordState) {
  1606. switch (device) {
  1607. case SND_DEVICE_HEADSET:
  1608. new_aic_txmode = VOICERECORD_EMIC;
  1609. case SND_DEVICE_HANDSET_BACK_MIC:
  1610. case SND_DEVICE_SPEAKER_BACK_MIC:
  1611. case SND_DEVICE_NO_MIC_HEADSET_BACK_MIC:
  1612. case SND_DEVICE_HEADSET_AND_SPEAKER_BACK_MIC:
  1613. new_aic_txmode = VIDEORECORD_IMIC;
  1614. break;
  1615. case SND_DEVICE_HANDSET:
  1616. case SND_DEVICE_SPEAKER:
  1617. case SND_DEVICE_NO_MIC_HEADSET:
  1618. case SND_DEVICE_HEADSET_AND_SPEAKER:
  1619. new_aic_txmode = VOICERECORD_IMIC;
  1620. break;
  1621. default:
  1622. break;
  1623. }
  1624. }
  1625. }
  1626.  
  1627. LOGD("aic3254_ioctl: new_aic_rxmode %d cur_aic_rx %d", new_aic_rxmode, cur_aic_rx);
  1628. if (new_aic_rxmode != cur_aic_rx)
  1629. if (aic3254_ioctl(AIC3254_CONFIG_RX, new_aic_rxmode) >= 0)
  1630. cur_aic_rx = new_aic_rxmode;
  1631.  
  1632. LOGD("aic3254_ioctl: new_aic_txmode %d cur_aic_tx %d", new_aic_txmode, cur_aic_tx);
  1633. if (new_aic_txmode != cur_aic_tx)
  1634. if (aic3254_ioctl(AIC3254_CONFIG_TX, new_aic_txmode) >= 0)
  1635. cur_aic_tx = new_aic_txmode;
  1636.  
  1637. if (cur_aic_tx == UPLINK_OFF && cur_aic_rx == DOWNLINK_OFF)
  1638. aic3254_powerdown();
  1639.  
  1640. return NO_ERROR;
  1641. }
  1642.  
  1643. void AudioHardware::aic3254_config(uint32_t device, const char* active_ap,
  1644. const char* aic_effect) {
  1645. int (*set_sound_effect)(const char* effect);
  1646. char name[18] = "\0";
  1647. char base[18] = "\0";
  1648. char desi[6] = "\0";
  1649. char aap[8] = "\0";
  1650.  
  1651. if (mMode == AudioSystem::MODE_IN_CALL) {
  1652. strcpy(base, "Original_Phone");
  1653. switch (device) {
  1654. case SND_DEVICE_HANDSET:
  1655. case SND_DEVICE_HANDSET_BACK_MIC:
  1656. case SND_DEVICE_IN_S_SADC_OUT_HANDSET:
  1657. strcat(base, "_REC");
  1658. break;
  1659. case SND_DEVICE_HEADSET:
  1660. case SND_DEVICE_HEADSET_AND_SPEAKER:
  1661. case SND_DEVICE_HEADSET_AND_SPEAKER_BACK_MIC:
  1662. case SND_DEVICE_NO_MIC_HEADSET:
  1663. strcat(base, "_HP");
  1664. break;
  1665. case SND_DEVICE_SPEAKER:
  1666. strcat(base, "_SPK");
  1667. break;
  1668. default:
  1669. break;
  1670. }
  1671. } else {
  1672. if (mRecordState)
  1673. strcpy(base, "Recording");
  1674. else if (strlen(aic_effect) == 0 && !mEffectEnabled)
  1675. strcpy(base, "Original");
  1676. else {
  1677. if (strlen(aic_effect) == 0 && mEffectEnabled)
  1678. strcpy(base, mCurDspProfile);
  1679. else
  1680. strcpy(base, aic_effect);
  1681.  
  1682. if (strlen(active_ap) == 0)
  1683. strcpy(aap, mActiveAP);
  1684. else
  1685. strcpy(aap, active_ap);
  1686.  
  1687. if (strcasecmp(base, "Srs") == 0 ||
  1688. strcasecmp(base, "Dolby") == 0) {
  1689. if (strcasecmp(aap, "Music") == 0)
  1690. strcat(desi, "_a");
  1691. else if (strcasecmp(aap, "Video") == 0)
  1692. strcat(desi, "_v");
  1693. if (device == SND_DEVICE_SPEAKER)
  1694. strcat(desi, "_spk");
  1695. else
  1696. strcat(desi, "_hp");
  1697. }
  1698. }
  1699. }
  1700.  
  1701. strcat(name, base);
  1702. strcat(name, desi);
  1703.  
  1704. if (strcmp(mCurDspProfile, name))
  1705. LOGD("aic3254_config: loading effect %s", name);
  1706. else {
  1707. LOGD("aic3254_config: effect %s already loaded", name);
  1708. return;
  1709. }
  1710.  
  1711. set_sound_effect = (int (*)(const char*))::dlsym(acoustic, "set_sound_effect");
  1712. if ((*set_sound_effect) == 0 ) {
  1713. LOGE("Could not open set_sound_effect()");
  1714. return;
  1715. }
  1716.  
  1717. int rc = set_sound_effect(name);
  1718. if (rc < 0)
  1719. LOGE("Could not set sound effect %s: %d", name, rc);
  1720. else
  1721. strcpy(mCurDspProfile, base);
  1722. }
  1723.  
  1724. int AudioHardware::aic3254_ioctl(int cmd, const int argc) {
  1725. int rc = -1;
  1726. int (*set_aic3254_ioctl)(int, const int*);
  1727.  
  1728. LOGD("aic3254_ioctl()");
  1729.  
  1730. set_aic3254_ioctl = (int (*)(int, const int*))::dlsym(acoustic, "set_aic3254_ioctl");
  1731. if ((*set_aic3254_ioctl) == 0) {
  1732. LOGE("Could not open set_aic3254_ioctl()");
  1733. return rc;
  1734. }
  1735.  
  1736. LOGD("aic3254_ioctl: try ioctl 0x%x with arg %d", cmd, argc);
  1737.  
  1738. rc = set_aic3254_ioctl(cmd, &argc);
  1739. if (rc < 0)
  1740. LOGE("aic3254_ioctl failed");
  1741.  
  1742. return rc;
  1743. }
  1744.  
  1745. void AudioHardware::aic3254_powerdown() {
  1746. LOGD("aic3254_powerdown");
  1747. int rc = aic3254_ioctl(AIC3254_POWERDOWN, 0);
  1748. if (rc < 0)
  1749. LOGE("aic3254_powerdown failed");
  1750. }
  1751.  
  1752. int AudioHardware::aic3254_set_volume(int volume) {
  1753. LOGD("aic3254_set_volume = %d", volume);
  1754.  
  1755. if (aic3254_ioctl(AIC3254_CONFIG_VOLUME_L, volume) < 0)
  1756. LOGE("aic3254_set_volume: could not set aic3254 LEFT volume %d", volume);
  1757. int rc = aic3254_ioctl(AIC3254_CONFIG_VOLUME_R, volume);
  1758. if (rc < 0)
  1759. LOGE("aic3254_set_volume: could not set aic3254 RIGHT volume %d", volume);
  1760. return rc;
  1761. }
  1762.  
  1763. status_t AudioHardware::doRouting(AudioStreamInMSM72xx *input) {
  1764. Mutex::Autolock lock(mLock);
  1765. uint32_t outputDevices = mOutput->devices();
  1766. status_t ret = NO_ERROR;
  1767. uint32_t sndDevice = INVALID_DEVICE;
  1768.  
  1769. if (input != NULL) {
  1770. uint32_t inputDevice = input->devices();
  1771. LOGI("do input routing device %x", inputDevice);
  1772. /* ignore routing device information when we start a recording in voice call
  1773. Recording will happen through currently active tx device */
  1774. if((inputDevice == AudioSystem::DEVICE_IN_VOICE_CALL) ||
  1775. (inputDevice == AudioSystem::DEVICE_IN_FM_RX) ||
  1776. (inputDevice == AudioSystem::DEVICE_IN_FM_RX_A2DP))
  1777. return NO_ERROR;
  1778. if (inputDevice != 0) {
  1779. if (inputDevice & AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
  1780. LOGI("Routing audio to Bluetooth PCM");
  1781. sndDevice = SND_DEVICE_BT;
  1782. } else if (inputDevice & AudioSystem::DEVICE_IN_WIRED_HEADSET) {
  1783. if ((outputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET) &&
  1784. (outputDevices & AudioSystem::DEVICE_OUT_SPEAKER)) {
  1785. LOGI("Routing audio to Wired Headset and Speaker");
  1786. sndDevice = SND_DEVICE_HEADSET_AND_SPEAKER;
  1787. } else {
  1788. LOGI("Routing audio to Wired Headset");
  1789. sndDevice = SND_DEVICE_HEADSET;
  1790. }
  1791. } else {
  1792. if (outputDevices & AudioSystem::DEVICE_OUT_SPEAKER) {
  1793. LOGI("Routing audio to Speakerphone");
  1794. sndDevice = SND_DEVICE_SPEAKER;
  1795. } else if (outputDevices == AudioSystem::DEVICE_OUT_WIRED_HEADPHONE) {
  1796. LOGI("Routing audio to Speakerphone");
  1797. sndDevice = SND_DEVICE_NO_MIC_HEADSET;
  1798. } else {
  1799. LOGI("Routing audio to Handset");
  1800. sndDevice = SND_DEVICE_HANDSET;
  1801. }
  1802. }
  1803. }
  1804. /* if inputDevice == 0, restore output routing */
  1805. }
  1806.  
  1807. if (sndDevice == INVALID_DEVICE) {
  1808. if (outputDevices & (outputDevices - 1)) {
  1809. if ((outputDevices & AudioSystem::DEVICE_OUT_SPEAKER) == 0) {
  1810. LOGW("Hardware does not support requested route combination (%#X),"
  1811. " picking closest possible route...", outputDevices);
  1812. }
  1813. }
  1814. if ((mTtyMode != TTY_OFF) && (mMode == AudioSystem::MODE_IN_CALL) &&
  1815. (outputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET)) {
  1816. if (mTtyMode == TTY_FULL) {
  1817. LOGI("Routing audio to TTY FULL Mode");
  1818. sndDevice = SND_DEVICE_TTY_FULL;
  1819. } else if (mTtyMode == TTY_VCO) {
  1820. LOGI("Routing audio to TTY VCO Mode");
  1821. sndDevice = SND_DEVICE_TTY_VCO;
  1822. } else if (mTtyMode == TTY_HCO) {
  1823. LOGI("Routing audio to TTY HCO Mode");
  1824. sndDevice = SND_DEVICE_TTY_HCO;
  1825. }
  1826. } else if (outputDevices &
  1827. (AudioSystem::DEVICE_OUT_BLUETOOTH_SCO | AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET)) {
  1828. LOGI("Routing audio to Bluetooth PCM");
  1829. sndDevice = SND_DEVICE_BT;
  1830. } else if (outputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT) {
  1831. LOGI("Routing audio to Bluetooth PCM");
  1832. sndDevice = SND_DEVICE_CARKIT;
  1833.  
  1834. /* No framework support
  1835. } else if (outputDevices & AudioSystem::DEVICE_OUT_AUX_HDMI) {
  1836. LOGI("Routing audio to HDMI");
  1837. sndDevice = SND_DEVICE_HDMI;
  1838. */
  1839. } else if ((outputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET) &&
  1840. (outputDevices & AudioSystem::DEVICE_OUT_SPEAKER)) {
  1841. LOGI("Routing audio to Wired Headset and Speaker");
  1842. sndDevice = SND_DEVICE_HEADSET_AND_SPEAKER;
  1843. } else if (outputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE) {
  1844. if (outputDevices & AudioSystem::DEVICE_OUT_SPEAKER) {
  1845. LOGI("Routing audio to No microphone Wired Headset and Speaker (%d,%x)", mMode, outputDevices);
  1846. sndDevice = SND_DEVICE_HEADPHONE_AND_SPEAKER;
  1847. } else {
  1848. if (outputDevices & AudioSystem::DEVICE_OUT_FM) {
  1849. LOGI("Routing FM audio to No microphone Wired Headset (%d,%x)", mMode, outputDevices);
  1850. sndDevice = SND_DEVICE_FM_HEADSET;
  1851. } else {
  1852. LOGI("Routing audio to No microphone Wired Headset (%d,%x)", mMode, outputDevices);
  1853. sndDevice = SND_DEVICE_NO_MIC_HEADSET;
  1854. }
  1855. }
  1856. } else if (outputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET) {
  1857. if (outputDevices & AudioSystem::DEVICE_OUT_FM) {
  1858. LOGI("Routing FM audio to Wired Headset");
  1859. sndDevice = SND_DEVICE_FM_HEADSET;
  1860. } else {
  1861. LOGI("Routing audio to Wired Headset");
  1862. sndDevice = SND_DEVICE_HEADSET;
  1863. }
  1864. } else if (outputDevices & AudioSystem::DEVICE_OUT_SPEAKER) {
  1865. if (outputDevices & AudioSystem::DEVICE_OUT_FM) {
  1866. LOGI("Routing FM audio to Speakerphone");
  1867. sndDevice = SND_DEVICE_FM_SPEAKER;
  1868. } else {
  1869. LOGI("Routing audio to Speakerphone");
  1870. sndDevice = SND_DEVICE_SPEAKER;
  1871. }
  1872. } else if (outputDevices & AudioSystem::DEVICE_OUT_EARPIECE) {
  1873. LOGI("Routing audio to Handset");
  1874. sndDevice = SND_DEVICE_HANDSET;
  1875. }
  1876. }
  1877.  
  1878. if (mDualMicEnabled && mMode == AudioSystem::MODE_IN_CALL) {
  1879. if (sndDevice == SND_DEVICE_HANDSET) {
  1880. LOGI("Routing audio to handset with DualMike enabled");
  1881. sndDevice = SND_DEVICE_IN_S_SADC_OUT_HANDSET;
  1882. } else if (sndDevice == SND_DEVICE_SPEAKER) {
  1883. LOGI("Routing audio to speakerphone with DualMike enabled");
  1884. sndDevice = SND_DEVICE_IN_S_SADC_OUT_SPEAKER_PHONE;
  1885. }
  1886. }
  1887.  
  1888. if (outputDevices & AudioSystem::DEVICE_OUT_FM)
  1889. enableFM(sndDevice);
  1890. else
  1891. disableFM();
  1892.  
  1893. if ((sndDevice != INVALID_DEVICE && sndDevice != mCurSndDevice)) {
  1894. ret = doAudioRouteOrMute(sndDevice);
  1895. mCurSndDevice = sndDevice;
  1896. if (mMode == AudioSystem::MODE_IN_CALL) {
  1897. if (mHACSetting && hac_enable && mCurSndDevice == SND_DEVICE_HAC) {
  1898. LOGD("HAC enable: Setting in-call volume to maximum.");
  1899. if (msm_set_voice_rx_vol(VOICE_VOLUME_MAX))
  1900. LOGE("msm_set_voice_rx_vol(%d) failed errno = %d", VOICE_VOLUME_MAX, errno);
  1901. } else {
  1902. if (msm_set_voice_rx_vol(mVoiceVolume))
  1903. LOGE("msm_set_voice_rx_vol(%d) failed errno = %d", mVoiceVolume, errno);
  1904. }
  1905. }
  1906. }
  1907.  
  1908. return ret;
  1909. }
  1910.  
  1911. status_t AudioHardware::enableFM(int sndDevice) {
  1912. LOGD("enableFM");
  1913.  
  1914. addToTable(0, cur_rx, INVALID_DEVICE, FM_RADIO, true);
  1915. if (sndDevice == mCurSndDevice || mCurSndDevice == INVALID_DEVICE) {
  1916. enableDevice(cur_rx, 1);
  1917. msm_route_stream(PCM_PLAY, 0, DEV_ID(cur_rx), 1);
  1918. }
  1919.  
  1920. return NO_ERROR;
  1921. }
  1922.  
  1923. status_t AudioHardware::disableFM() {
  1924. LOGD("disableFM");
  1925.  
  1926. Routing_table* temp = NULL;
  1927. temp = getNodeByStreamType(FM_RADIO);
  1928.  
  1929. if(temp == NULL)
  1930. return NO_ERROR;
  1931.  
  1932. LOGD("Deroute FM stream");
  1933. if(msm_route_stream(PCM_PLAY, temp->dec_id, DEV_ID(cur_rx), 0)) {
  1934. LOGE("msm_route_stream failed");
  1935. deleteFromTable(FM_RADIO);
  1936. return 0;
  1937. }
  1938.  
  1939. deleteFromTable(FM_RADIO);
  1940. updateDeviceInfo(cur_rx, cur_tx, 0, 0);
  1941.  
  1942. return NO_ERROR;
  1943. }
  1944.  
  1945. status_t AudioHardware::checkMicMute() {
  1946. Mutex::Autolock lock(mLock);
  1947. if (mMode != AudioSystem::MODE_IN_CALL)
  1948. setMicMute_nosync(true);
  1949.  
  1950. return NO_ERROR;
  1951. }
  1952.  
  1953. status_t AudioHardware::dumpInternals(int fd, const Vector<String16>& args) {
  1954. const size_t SIZE = 256;
  1955. char buffer[SIZE];
  1956. String8 result;
  1957. result.append("AudioHardware::dumpInternals\n");
  1958. snprintf(buffer, SIZE, "\tmInit: %s\n", mInit? "true": "false");
  1959. result.append(buffer);
  1960. snprintf(buffer, SIZE, "\tmMicMute: %s\n", mMicMute? "true": "false");
  1961. result.append(buffer);
  1962. snprintf(buffer, SIZE, "\tmBluetoothNrec: %s\n", mBluetoothNrec? "true": "false");
  1963. result.append(buffer);
  1964. snprintf(buffer, SIZE, "\tmBluetoothId: %d\n", mBluetoothId);
  1965. result.append(buffer);
  1966. ::write(fd, result.string(), result.size());
  1967. return NO_ERROR;
  1968. }
  1969.  
  1970. status_t AudioHardware::dump(int fd, const Vector<String16>& args) {
  1971. dumpInternals(fd, args);
  1972. for (size_t index = 0; index < mInputs.size(); index++) {
  1973. mInputs[index]->dump(fd, args);
  1974. }
  1975.  
  1976. if (mOutput)
  1977. mOutput->dump(fd, args);
  1978.  
  1979. return NO_ERROR;
  1980. }
  1981.  
  1982. uint32_t AudioHardware::getInputSampleRate(uint32_t sampleRate) {
  1983. uint32_t i;
  1984. uint32_t prevDelta;
  1985. uint32_t delta;
  1986.  
  1987. for (i = 0, prevDelta = 0xFFFFFFFF; i < sizeof(inputSamplingRates)/sizeof(uint32_t); i++, prevDelta = delta) {
  1988. delta = abs(sampleRate - inputSamplingRates[i]);
  1989. if (delta > prevDelta)
  1990. break;
  1991. }
  1992. /* i is always > 0 here */
  1993. return inputSamplingRates[i-1];
  1994. }
  1995.  
  1996.  
  1997. /* ---------------------------------------------------------------------------- */
  1998.  
  1999. #ifdef WITH_QCOM_LPA
  2000. AudioHardware::AudioSessionOutMSM7xxx::AudioSessionOutMSM7xxx() :
  2001. mHardware(0), mStartCount(0), mRetryCount(0), mStandby(true), mDevices(0), mSessionId(-1) {
  2002. }
  2003.  
  2004. status_t AudioHardware::AudioSessionOutMSM7xxx::set(AudioHardware* hw,
  2005. uint32_t devices, int *pFormat, int32_t sessionId) {
  2006. int lFormat = pFormat ? *pFormat : 0;
  2007.  
  2008. mHardware = hw;
  2009. mDevices = devices;
  2010.  
  2011. if (sessionId >= 0) {
  2012. LOGD("AudioSessionOutMSM7xxx::set() Adding LPA_DECODE Node to Table");
  2013.  
  2014. Mutex::Autolock lock(mDeviceSwitchLock);
  2015.  
  2016. addToTable(sessionId,cur_rx, INVALID_DEVICE, LPA_DECODE, true);
  2017.  
  2018. if (enableDevice(cur_rx, 1)) {
  2019. LOGE("enableDevice failed for device cur_rx %d", cur_rx);
  2020. return 0;
  2021. }
  2022.  
  2023. LOGV("msm_route_stream(PCM_PLAY, %d, %d, 0)", sessionId, DEV_ID(cur_rx));
  2024. if (msm_route_stream(PCM_PLAY, sessionId,DEV_ID(cur_rx), 1))
  2025. LOGE("msm_route_stream(PCM_PLAY, %d, %d, 1) failed", sessionId, DEV_ID(cur_rx));
  2026.  
  2027. mSessionId = sessionId;
  2028. }
  2029. return NO_ERROR;
  2030. }
  2031.  
  2032. AudioHardware::AudioSessionOutMSM7xxx::~AudioSessionOutMSM7xxx() {
  2033. }
  2034.  
  2035. status_t AudioHardware::AudioSessionOutMSM7xxx::standby() {
  2036. Routing_table* temp = NULL;
  2037. LOGD("AudioSessionOutMSM7xxx::standby()");
  2038. status_t status = NO_ERROR;
  2039.  
  2040. temp = getNodeByStreamType(LPA_DECODE);
  2041.  
  2042. if (temp == NULL)
  2043. return NO_ERROR;
  2044.  
  2045. LOGD("Deroute lpa playback stream");
  2046. if (msm_route_stream(PCM_PLAY, temp->dec_id,DEV_ID(temp->dev_id), 0)) {
  2047. LOGE("could not set stream routing");
  2048. deleteFromTable(LPA_DECODE);
  2049. return -1;
  2050. }
  2051. deleteFromTable(LPA_DECODE);
  2052. updateDeviceInfo(cur_rx, cur_tx, 0, 0);
  2053. mStandby = true;
  2054. return status;
  2055. }
  2056.  
  2057. bool AudioHardware::AudioSessionOutMSM7xxx::checkStandby() {
  2058. return mStandby;
  2059. }
  2060.  
  2061. status_t AudioHardware::AudioSessionOutMSM7xxx::setParameters(const String8& keyValuePairs) {
  2062. AudioParameter param = AudioParameter(keyValuePairs);
  2063. String8 key = String8(AudioParameter::keyRouting);
  2064. status_t status = NO_ERROR;
  2065. int device;
  2066. LOGV("AudioSessionOutMSM7xxx::setParameters() %s", keyValuePairs.string());
  2067.  
  2068. if (param.getInt(key, device) == NO_ERROR) {
  2069. mDevices = device;
  2070. LOGV("set output routing %x", mDevices);
  2071. status = mHardware->doRouting(NULL);
  2072. param.remove(key);
  2073. }
  2074.  
  2075. if (param.size()) {
  2076. status = BAD_VALUE;
  2077. }
  2078. return status;
  2079. }
  2080.  
  2081. String8 AudioHardware::AudioSessionOutMSM7xxx::getParameters(const String8& keys) {
  2082. AudioParameter param = AudioParameter(keys);
  2083. String8 value;
  2084. String8 key = String8(AudioParameter::keyRouting);
  2085.  
  2086. if (param.get(key, value) == NO_ERROR) {
  2087. LOGV("get routing %x", mDevices);
  2088. param.addInt(key, (int)mDevices);
  2089. }
  2090.  
  2091. LOGV("AudioSessionOutMSM7xxx::getParameters() %s", param.toString().string());
  2092. return param.toString();
  2093. }
  2094.  
  2095. status_t AudioHardware::AudioSessionOutMSM7xxx::getRenderPosition(uint32_t *dspFrames) {
  2096. /* TODO: enable when supported by driver */
  2097. return INVALID_OPERATION;
  2098. }
  2099.  
  2100. status_t AudioHardware::AudioSessionOutMSM7xxx::setVolume(float left, float right) {
  2101. float v = (left + right) / 2;
  2102. if (v < 0.0) {
  2103. LOGW("AudioSessionOutMSM7xxx::setVolume(%f) under 0.0, assuming 0.0", v);
  2104. v = 0.0;
  2105. } else if (v > 1.0) {
  2106. LOGW("AudioSessionOutMSM7xxx::setVolume(%f) over 1.0, assuming 1.0", v);
  2107. v = 1.0;
  2108. }
  2109.  
  2110. /* Ensure to convert the log volume back to linear for LPA */
  2111. float vol = v * 100;
  2112. LOGV("AudioSessionOutMSM7xxx::setVolume(%f)", v);
  2113. LOGV("Setting session volume to %f (available range is 0 to 100)", vol);
  2114.  
  2115. if (msm_set_volume(mSessionId, vol)) {
  2116. LOGE("msm_set_volume(%d %f) failed errno = %d", mSessionId, vol, errno);
  2117. return -1;
  2118. }
  2119. LOGV("msm_set_volume(%f) succeeded", vol);
  2120. return NO_ERROR;
  2121. }
  2122. #endif
  2123.  
  2124. /* ---------------------------------------------------------------------------- */
  2125.  
  2126. AudioHardware::AudioStreamOutMSM72xx::AudioStreamOutMSM72xx() :
  2127. mHardware(0), mFd(-1), mStartCount(0), mRetryCount(0), mStandby(true), mDevices(0)
  2128. {
  2129. }
  2130.  
  2131. status_t AudioHardware::AudioStreamOutMSM72xx::set(
  2132. AudioHardware* hw, uint32_t devices, int *pFormat, uint32_t *pChannels, uint32_t *pRate) {
  2133. int lFormat = pFormat ? *pFormat : 0;
  2134. uint32_t lChannels = pChannels ? *pChannels : 0;
  2135. uint32_t lRate = pRate ? *pRate : 0;
  2136.  
  2137. mHardware = hw;
  2138.  
  2139. /* fix up defaults */
  2140. if (lFormat == 0) lFormat = format();
  2141. if (lChannels == 0) lChannels = channels();
  2142. if (lRate == 0) lRate = sampleRate();
  2143.  
  2144. /* check values */
  2145. if ((lFormat != format()) ||
  2146. (lChannels != channels()) ||
  2147. (lRate != sampleRate())) {
  2148. if (pFormat) *pFormat = format();
  2149. if (pChannels) *pChannels = channels();
  2150. if (pRate) *pRate = sampleRate();
  2151. return BAD_VALUE;
  2152. }
  2153.  
  2154. if (pFormat) *pFormat = lFormat;
  2155. if (pChannels) *pChannels = lChannels;
  2156. if (pRate) *pRate = lRate;
  2157.  
  2158. mDevices = devices;
  2159.  
  2160. return NO_ERROR;
  2161. }
  2162.  
  2163. AudioHardware::AudioStreamOutMSM72xx::~AudioStreamOutMSM72xx() {
  2164. if (mFd >= 0)
  2165. close(mFd);
  2166. }
  2167.  
  2168. ssize_t AudioHardware::AudioStreamOutMSM72xx::write(const void* buffer, size_t bytes) {
  2169. /* LOGD("AudioStreamOutMSM72xx::write(%p, %u)", buffer, bytes); */
  2170. status_t status = NO_INIT;
  2171. size_t count = bytes;
  2172. const uint8_t* p = static_cast<const uint8_t*>(buffer);
  2173. unsigned short dec_id = INVALID_DEVICE;
  2174.  
  2175. if (mStandby) {
  2176. /* open driver */
  2177. LOGV("open driver");
  2178. status = ::open("/dev/msm_pcm_out", O_WRONLY/*O_RDWR*/);
  2179. if (status < 0) {
  2180. LOGE("Cannot open /dev/msm_pcm_out errno: %d", errno);
  2181. goto Error;
  2182. }
  2183. mFd = status;
  2184.  
  2185. /* configuration */
  2186. LOGV("get config");
  2187. struct msm_audio_config config;
  2188. status = ioctl(mFd, AUDIO_GET_CONFIG, &config);
  2189. if (status < 0) {
  2190. LOGE("Cannot read config");
  2191. goto Error;
  2192. }
  2193.  
  2194. LOGV("set config");
  2195. config.channel_count = AudioSystem::popCount(channels());
  2196. config.sample_rate = sampleRate();
  2197. config.buffer_size = bufferSize();
  2198. config.buffer_count = AUDIO_HW_NUM_OUT_BUF;
  2199. config.codec_type = CODEC_TYPE_PCM;
  2200. status = ioctl(mFd, AUDIO_SET_CONFIG, &config);
  2201. if (status < 0) {
  2202. LOGE("Cannot set config");
  2203. goto Error;
  2204. }
  2205.  
  2206. LOGV("buffer_size: %u", config.buffer_size);
  2207. LOGV("buffer_count: %u", config.buffer_count);
  2208. LOGV("channel_count: %u", config.channel_count);
  2209. LOGV("sample_rate: %u", config.sample_rate);
  2210.  
  2211. /* fill 2 buffers before AUDIO_START */
  2212. mStartCount = AUDIO_HW_NUM_OUT_BUF;
  2213. mStandby = false;
  2214.  
  2215. if (support_tpa2051)
  2216. do_tpa2051_control(0);
  2217.  
  2218. if (support_aic3254)
  2219. mHardware->do_aic3254_control(mHardware->get_snd_dev());
  2220. }
  2221.  
  2222. while (count) {
  2223. ssize_t written = ::write(mFd, p, count);
  2224. if (written >= 0) {
  2225. count -= written;
  2226. p += written;
  2227. } else {
  2228. if (errno != EAGAIN)
  2229. return written;
  2230. mRetryCount++;
  2231. LOGW("EAGAIN - retry");
  2232. }
  2233. }
  2234.  
  2235. /* start audio after we fill 2 buffers */
  2236. if (mStartCount) {
  2237. if (--mStartCount == 0) {
  2238. if (ioctl(mFd, AUDIO_GET_SESSION_ID, &dec_id)) {
  2239. LOGE("AUDIO_GET_SESSION_ID failed*********");
  2240. return 0;
  2241. }
  2242. LOGV("dec_id = %d",dec_id);
  2243. if (cur_rx == INVALID_DEVICE)
  2244. return 0;
  2245.  
  2246. Mutex::Autolock lock(mDeviceSwitchLock);
  2247.  
  2248. LOGV("cur_rx for pcm playback = %d",cur_rx);
  2249. if (enableDevice(cur_rx, 1)) {
  2250. LOGE("enableDevice failed for device cur_rx %d", cur_rx);
  2251. return 0;
  2252. }
  2253.  
  2254. uint32_t rx_acdb_id = mHardware->getACDB(MOD_PLAY, mHardware->get_snd_dev());
  2255. updateACDB(cur_rx, cur_tx, rx_acdb_id, 0);
  2256.  
  2257. if (msm_route_stream(PCM_PLAY, dec_id, DEV_ID(cur_rx), 1)) {
  2258. LOGE("msm_route_stream failed");
  2259. return 0;
  2260. }
  2261. addToTable(dec_id, cur_rx, INVALID_DEVICE, PCM_PLAY, true);
  2262. ioctl(mFd, AUDIO_START, 0);
  2263. }
  2264. }
  2265. return bytes;
  2266.  
  2267. Error:
  2268. if (mFd >= 0) {
  2269. ::close(mFd);
  2270. mFd = -1;
  2271. }
  2272. /* Simulate audio output timing in case of error */
  2273. usleep(bytes * 1000000 / frameSize() / sampleRate());
  2274.  
  2275. return status;
  2276. }
  2277.  
  2278. status_t AudioHardware::AudioStreamOutMSM72xx::standby() {
  2279. Routing_table* temp = NULL;
  2280. LOGD("AudioStreamOutMSM72xx::standby()");
  2281. status_t status = NO_ERROR;
  2282.  
  2283. temp = getNodeByStreamType(PCM_PLAY);
  2284.  
  2285. if (temp == NULL)
  2286. return NO_ERROR;
  2287.  
  2288. LOGD("Deroute pcm out stream");
  2289. if (msm_route_stream(PCM_PLAY, temp->dec_id, DEV_ID(temp->dev_id), 0)) {
  2290. LOGE("could not set stream routing");
  2291. deleteFromTable(PCM_PLAY);
  2292. return -1;
  2293. }
  2294. deleteFromTable(PCM_PLAY);
  2295. updateDeviceInfo(cur_rx, cur_tx, 0, 0);
  2296.  
  2297. if (!mStandby && mFd >= 0) {
  2298. ::close(mFd);
  2299. mFd = -1;
  2300. }
  2301.  
  2302. mStandby = true;
  2303.  
  2304. if (support_aic3254)
  2305. mHardware->do_aic3254_control(mHardware->get_snd_dev());
  2306.  
  2307. return status;
  2308. }
  2309.  
  2310. status_t AudioHardware::AudioStreamOutMSM72xx::dump(int fd, const Vector<String16>& args) {
  2311. const size_t SIZE = 256;
  2312. char buffer[SIZE];
  2313. String8 result;
  2314. result.append("AudioStreamOutMSM72xx::dump\n");
  2315. snprintf(buffer, SIZE, "\tsample rate: %d\n", sampleRate());
  2316. result.append(buffer);
  2317. snprintf(buffer, SIZE, "\tbuffer size: %d\n", bufferSize());
  2318. result.append(buffer);
  2319. snprintf(buffer, SIZE, "\tchannels: %d\n", channels());
  2320. result.append(buffer);
  2321. snprintf(buffer, SIZE, "\tformat: %d\n", format());
  2322. result.append(buffer);
  2323. snprintf(buffer, SIZE, "\tmHardware: %p\n", mHardware);
  2324. result.append(buffer);
  2325. snprintf(buffer, SIZE, "\tmFd: %d\n", mFd);
  2326. result.append(buffer);
  2327. snprintf(buffer, SIZE, "\tmStartCount: %d\n", mStartCount);
  2328. result.append(buffer);
  2329. snprintf(buffer, SIZE, "\tmRetryCount: %d\n", mRetryCount);
  2330. result.append(buffer);
  2331. snprintf(buffer, SIZE, "\tmStandby: %s\n", mStandby? "true": "false");
  2332. result.append(buffer);
  2333. ::write(fd, result.string(), result.size());
  2334. return NO_ERROR;
  2335. }
  2336.  
  2337. bool AudioHardware::AudioStreamOutMSM72xx::checkStandby() {
  2338. return mStandby;
  2339. }
  2340.  
  2341.  
  2342. status_t AudioHardware::AudioStreamOutMSM72xx::setParameters(const String8& keyValuePairs) {
  2343. AudioParameter param = AudioParameter(keyValuePairs);
  2344. String8 key = String8(AudioParameter::keyRouting);
  2345. status_t status = NO_ERROR;
  2346. int device;
  2347. LOGV("AudioStreamOutMSM72xx::setParameters() %s", keyValuePairs.string());
  2348.  
  2349. if (param.getInt(key, device) == NO_ERROR) {
  2350. mDevices = device;
  2351. LOGV("set output routing %x", mDevices);
  2352. status = mHardware->doRouting(NULL);
  2353. param.remove(key);
  2354. }
  2355.  
  2356. if (param.size()) {
  2357. status = BAD_VALUE;
  2358. }
  2359. return status;
  2360. }
  2361.  
  2362. String8 AudioHardware::AudioStreamOutMSM72xx::getParameters(const String8& keys) {
  2363. AudioParameter param = AudioParameter(keys);
  2364. String8 value;
  2365. String8 key = String8(AudioParameter::keyRouting);
  2366.  
  2367. if (param.get(key, value) == NO_ERROR) {
  2368. LOGV("get routing %x", mDevices);
  2369. param.addInt(key, (int)mDevices);
  2370. }
  2371.  
  2372. LOGV("AudioStreamOutMSM72xx::getParameters() %s", param.toString().string());
  2373. return param.toString();
  2374. }
  2375.  
  2376. status_t AudioHardware::AudioStreamOutMSM72xx::getRenderPosition(uint32_t *dspFrames) {
  2377. /* TODO: enable when supported by driver */
  2378. return INVALID_OPERATION;
  2379. }
  2380.  
  2381.  
  2382. // ----------------------------------------------------------------------------
  2383.  
  2384. AudioHardware::AudioStreamInMSM72xx::AudioStreamInMSM72xx() :
  2385. mHardware(0), mFd(-1), mState(AUDIO_INPUT_CLOSED), mRetryCount(0),
  2386. mFormat(AUDIO_HW_IN_FORMAT), mChannels(AUDIO_HW_IN_CHANNELS),
  2387. mSampleRate(AUDIO_HW_IN_SAMPLERATE), mBufferSize(AUDIO_HW_IN_BUFFERSIZE),
  2388. mAcoustics((AudioSystem::audio_in_acoustics)0), mDevices(0) {
  2389. }
  2390.  
  2391. status_t AudioHardware::AudioStreamInMSM72xx::set(
  2392. AudioHardware* hw, uint32_t devices, int *pFormat, uint32_t *pChannels, uint32_t *pRate,
  2393. AudioSystem::audio_in_acoustics acoustic_flags) {
  2394. if ((pFormat == 0) ||
  2395. ((*pFormat != AUDIO_HW_IN_FORMAT) &&
  2396. #ifdef WITH_QCOM_SPEECH
  2397. (*pFormat != AudioSystem::AMR_NB) &&
  2398. (*pFormat != AudioSystem::EVRC) &&
  2399. (*pFormat != AudioSystem::QCELP) &&
  2400. #endif
  2401. (*pFormat != AudioSystem::AAC))) {
  2402. *pFormat = AUDIO_HW_IN_FORMAT;
  2403. return BAD_VALUE;
  2404. }
  2405.  
  2406. if ((*pFormat == AudioSystem::AAC) && (*pChannels & (AudioSystem::CHANNEL_IN_VOICE_DNLINK | AudioSystem::CHANNEL_IN_VOICE_UPLINK))) {
  2407. LOGE("voice call recording in AAC format does not support");
  2408. return BAD_VALUE;
  2409. }
  2410.  
  2411. if (pRate == 0)
  2412. return BAD_VALUE;
  2413.  
  2414. uint32_t rate = hw->getInputSampleRate(*pRate);
  2415. if (rate != *pRate) {
  2416. *pRate = rate;
  2417. return BAD_VALUE;
  2418. }
  2419.  
  2420. if (pChannels == 0 || (*pChannels & (AudioSystem::CHANNEL_IN_MONO | AudioSystem::CHANNEL_IN_STEREO)) == 0) {
  2421. *pChannels = AUDIO_HW_IN_CHANNELS;
  2422. return BAD_VALUE;
  2423. }
  2424.  
  2425. mHardware = hw;
  2426.  
  2427. LOGV("AudioStreamInMSM72xx::set(%d, %d, %u)", *pFormat, *pChannels, *pRate);
  2428. if (mFd >= 0) {
  2429. LOGE("Audio record already open");
  2430. return -EPERM;
  2431. }
  2432. status_t status =0;
  2433. struct msm_voicerec_mode voc_rec_cfg;
  2434. if (devices == AudioSystem::DEVICE_IN_FM_RX_A2DP) {
  2435. status = ::open("/dev/msm_a2dp_in", O_RDONLY);
  2436. if (status < 0) {
  2437. LOGE("Cannot open /dev/msm_a2dp_in errno: %d", errno);
  2438. goto Error;
  2439. }
  2440. mFd = status;
  2441. /* configuration */
  2442. LOGV("get config");
  2443. struct msm_audio_config config;
  2444. status = ioctl(mFd, AUDIO_GET_CONFIG, &config);
  2445. if (status < 0) {
  2446. LOGE("Cannot read config");
  2447. goto Error;
  2448. }
  2449.  
  2450. LOGV("set config");
  2451. config.channel_count = AudioSystem::popCount((*pChannels) & (AudioSystem::CHANNEL_IN_STEREO | AudioSystem::CHANNEL_IN_MONO));
  2452. config.sample_rate = *pRate;
  2453. config.buffer_size = bufferSize();
  2454. config.buffer_count = 2;
  2455. config.codec_type = CODEC_TYPE_PCM;
  2456. status = ioctl(mFd, AUDIO_SET_CONFIG, &config);
  2457. if (status < 0) {
  2458. LOGE("Cannot set config");
  2459. if (ioctl(mFd, AUDIO_GET_CONFIG, &config) == 0) {
  2460. if (config.channel_count == 1)
  2461. *pChannels = AudioSystem::CHANNEL_IN_MONO;
  2462. else
  2463. *pChannels = AudioSystem::CHANNEL_IN_STEREO;
  2464.  
  2465. *pRate = config.sample_rate;
  2466. }
  2467. goto Error;
  2468. }
  2469.  
  2470. LOGV("confirm config");
  2471. status = ioctl(mFd, AUDIO_GET_CONFIG, &config);
  2472. if (status < 0) {
  2473. LOGE("Cannot read config");
  2474. goto Error;
  2475. }
  2476. LOGV("buffer_size: %u", config.buffer_size);
  2477. LOGV("buffer_count: %u", config.buffer_count);
  2478. LOGV("channel_count: %u", config.channel_count);
  2479. LOGV("sample_rate: %u", config.sample_rate);
  2480.  
  2481. mDevices = devices;
  2482. mFormat = AUDIO_HW_IN_FORMAT;
  2483. mChannels = *pChannels;
  2484. mSampleRate = config.sample_rate;
  2485. mBufferSize = config.buffer_size;
  2486. } else if (*pFormat == AUDIO_HW_IN_FORMAT) {
  2487. /* open audio input device */
  2488. status = ::open("/dev/msm_pcm_in", O_RDONLY);
  2489. if (status < 0) {
  2490. LOGE("Cannot open /dev/msm_pcm_in errno: %d", errno);
  2491. goto Error;
  2492. }
  2493. mFd = status;
  2494.  
  2495. /* configuration */
  2496. LOGV("get config");
  2497. struct msm_audio_config config;
  2498. status = ioctl(mFd, AUDIO_GET_CONFIG, &config);
  2499. if (status < 0) {
  2500. LOGE("Cannot read config");
  2501. goto Error;
  2502. }
  2503.  
  2504. LOGV("set config");
  2505. config.channel_count = AudioSystem::popCount((*pChannels) & (AudioSystem::CHANNEL_IN_STEREO | AudioSystem::CHANNEL_IN_MONO));
  2506. config.sample_rate = *pRate;
  2507. config.buffer_size = bufferSize();
  2508. config.buffer_count = 2;
  2509. config.codec_type = CODEC_TYPE_PCM;
  2510. status = ioctl(mFd, AUDIO_SET_CONFIG, &config);
  2511. if (status < 0) {
  2512. LOGE("Cannot set config");
  2513. if (ioctl(mFd, AUDIO_GET_CONFIG, &config) == 0) {
  2514. if (config.channel_count == 1)
  2515. *pChannels = AudioSystem::CHANNEL_IN_MONO;
  2516. else
  2517. *pChannels = AudioSystem::CHANNEL_IN_STEREO;
  2518.  
  2519. *pRate = config.sample_rate;
  2520. }
  2521. goto Error;
  2522. }
  2523.  
  2524. LOGV("confirm config");
  2525. status = ioctl(mFd, AUDIO_GET_CONFIG, &config);
  2526. if (status < 0) {
  2527. LOGE("Cannot read config");
  2528. goto Error;
  2529. }
  2530. LOGV("buffer_size: %u", config.buffer_size);
  2531. LOGV("buffer_count: %u", config.buffer_count);
  2532. LOGV("channel_count: %u", config.channel_count);
  2533. LOGV("sample_rate: %u", config.sample_rate);
  2534.  
  2535. mDevices = devices;
  2536. mFormat = AUDIO_HW_IN_FORMAT;
  2537. mChannels = *pChannels;
  2538. if (mDevices == AudioSystem::DEVICE_IN_VOICE_CALL) {
  2539. if ((mChannels & AudioSystem::CHANNEL_IN_VOICE_DNLINK) &&
  2540. (mChannels & AudioSystem::CHANNEL_IN_VOICE_UPLINK)) {
  2541. LOGI("Recording Source: Voice Call Both Uplink and Downlink");
  2542. voc_rec_cfg.rec_mode = VOC_REC_BOTH;
  2543. } else if (mChannels & AudioSystem::CHANNEL_IN_VOICE_DNLINK) {
  2544. LOGI("Recording Source: Voice Call DownLink");
  2545. voc_rec_cfg.rec_mode = VOC_REC_DOWNLINK;
  2546. } else if (mChannels & AudioSystem::CHANNEL_IN_VOICE_UPLINK) {
  2547. LOGI("Recording Source: Voice Call UpLink");
  2548. voc_rec_cfg.rec_mode = VOC_REC_UPLINK;
  2549. }
  2550. if (ioctl(mFd, AUDIO_SET_INCALL, &voc_rec_cfg)) {
  2551. LOGE("Error: AUDIO_SET_INCALL failed");
  2552. goto Error;
  2553. }
  2554. }
  2555. mSampleRate = config.sample_rate;
  2556. mBufferSize = config.buffer_size;
  2557. }
  2558. #ifdef WITH_QCOM_SPEECH
  2559. else if (*pFormat == AudioSystem::EVRC) {
  2560. LOGI("Recording format: EVRC");
  2561. /* open evrc input device */
  2562. status = ::open(EVRC_DEVICE_IN, O_RDONLY);
  2563. if (status < 0) {
  2564. LOGE("Cannot open evrc device for read");
  2565. goto Error;
  2566. }
  2567. mFd = status;
  2568. mDevices = devices;
  2569. mChannels = *pChannels;
  2570.  
  2571. if (mDevices == AudioSystem::DEVICE_IN_VOICE_CALL) {
  2572. if ((mChannels & AudioSystem::CHANNEL_IN_VOICE_DNLINK) &&
  2573. (mChannels & AudioSystem::CHANNEL_IN_VOICE_UPLINK)) {
  2574. LOGI("Recording Source: Voice Call Both Uplink and Downlink");
  2575. voc_rec_cfg.rec_mode = VOC_REC_BOTH;
  2576. } else if (mChannels & AudioSystem::CHANNEL_IN_VOICE_DNLINK) {
  2577. LOGI("Recording Source: Voice Call DownLink");
  2578. voc_rec_cfg.rec_mode = VOC_REC_DOWNLINK;
  2579. } else if (mChannels & AudioSystem::CHANNEL_IN_VOICE_UPLINK) {
  2580. LOGI("Recording Source: Voice Call UpLink");
  2581. voc_rec_cfg.rec_mode = VOC_REC_UPLINK;
  2582. }
  2583.  
  2584. if (ioctl(mFd, AUDIO_SET_INCALL, &voc_rec_cfg)) {
  2585. LOGE("Error: AUDIO_SET_INCALL failed");
  2586. goto Error;
  2587. }
  2588. }
  2589.  
  2590. /* Config param */
  2591. struct msm_audio_stream_config config;
  2592. if (ioctl(mFd, AUDIO_GET_STREAM_CONFIG, &config)) {
  2593. LOGE(" Error getting buf config param AUDIO_GET_STREAM_CONFIG");
  2594. goto Error;
  2595. }
  2596.  
  2597. LOGV("The Config buffer size is %d", config.buffer_size);
  2598. LOGV("The Config buffer count is %d", config.buffer_count);
  2599.  
  2600. mSampleRate = 8000;
  2601. mFormat = *pFormat;
  2602. mBufferSize = 230;
  2603. struct msm_audio_evrc_enc_config evrc_enc_cfg;
  2604.  
  2605. if (ioctl(mFd, AUDIO_GET_EVRC_ENC_CONFIG, &evrc_enc_cfg)) {
  2606. LOGE("Error: AUDIO_GET_EVRC_ENC_CONFIG failed");
  2607. goto Error;
  2608. }
  2609.  
  2610. LOGV("The Config cdma_rate is %d", evrc_enc_cfg.cdma_rate);
  2611. LOGV("The Config min_bit_rate is %d", evrc_enc_cfg.min_bit_rate);
  2612. LOGV("The Config max_bit_rate is %d", evrc_enc_cfg.max_bit_rate);
  2613.  
  2614. evrc_enc_cfg.min_bit_rate = 4;
  2615. evrc_enc_cfg.max_bit_rate = 4;
  2616.  
  2617. if (ioctl(mFd, AUDIO_SET_EVRC_ENC_CONFIG, &evrc_enc_cfg)) {
  2618. LOGE("Error: AUDIO_SET_EVRC_ENC_CONFIG failed");
  2619. goto Error;
  2620. }
  2621. } else if (*pFormat == AudioSystem::QCELP) {
  2622. LOGI("Recording format: QCELP");
  2623. /* open qcelp input device */
  2624. status = ::open(QCELP_DEVICE_IN, O_RDONLY);
  2625. if (status < 0) {
  2626. LOGE("Cannot open qcelp device for read");
  2627. goto Error;
  2628. }
  2629. mFd = status;
  2630. mDevices = devices;
  2631. mChannels = *pChannels;
  2632.  
  2633. if (mDevices == AudioSystem::DEVICE_IN_VOICE_CALL) {
  2634. if ((mChannels & AudioSystem::CHANNEL_IN_VOICE_DNLINK) &&
  2635. (mChannels & AudioSystem::CHANNEL_IN_VOICE_UPLINK)) {
  2636. LOGI("Recording Source: Voice Call Both Uplink and Downlink");
  2637. voc_rec_cfg.rec_mode = VOC_REC_BOTH;
  2638. } else if (mChannels & AudioSystem::CHANNEL_IN_VOICE_DNLINK) {
  2639. LOGI("Recording Source: Voice Call DownLink");
  2640. voc_rec_cfg.rec_mode = VOC_REC_DOWNLINK;
  2641. } else if (mChannels & AudioSystem::CHANNEL_IN_VOICE_UPLINK) {
  2642. LOGI("Recording Source: Voice Call UpLink");
  2643. voc_rec_cfg.rec_mode = VOC_REC_UPLINK;
  2644. }
  2645.  
  2646. if (ioctl(mFd, AUDIO_SET_INCALL, &voc_rec_cfg)) {
  2647. LOGE("Error: AUDIO_SET_INCALL failed");
  2648. goto Error;
  2649. }
  2650. }
  2651.  
  2652. /* Config param */
  2653. struct msm_audio_stream_config config;
  2654. if (ioctl(mFd, AUDIO_GET_STREAM_CONFIG, &config)) {
  2655. LOGE("Error getting buf config param AUDIO_GET_STREAM_CONFIG");
  2656. goto Error;
  2657. }
  2658.  
  2659. LOGV("The Config buffer size is %d", config.buffer_size);
  2660. LOGV("The Config buffer count is %d", config.buffer_count);
  2661.  
  2662. mSampleRate = 8000;
  2663. mFormat = *pFormat;
  2664. mBufferSize = 350;
  2665.  
  2666. struct msm_audio_qcelp_enc_config qcelp_enc_cfg;
  2667.  
  2668. if (ioctl(mFd, AUDIO_GET_QCELP_ENC_CONFIG, &qcelp_enc_cfg)) {
  2669. LOGE("Error: AUDIO_GET_QCELP_ENC_CONFIG failed");
  2670. goto Error;
  2671. }
  2672.  
  2673. LOGV("The Config cdma_rate is %d", qcelp_enc_cfg.cdma_rate);
  2674. LOGV("The Config min_bit_rate is %d", qcelp_enc_cfg.min_bit_rate);
  2675. LOGV("The Config max_bit_rate is %d", qcelp_enc_cfg.max_bit_rate);
  2676.  
  2677. qcelp_enc_cfg.min_bit_rate = 4;
  2678. qcelp_enc_cfg.max_bit_rate = 4;
  2679.  
  2680. if (ioctl(mFd, AUDIO_SET_QCELP_ENC_CONFIG, &qcelp_enc_cfg)) {
  2681. LOGE("Error: AUDIO_SET_QCELP_ENC_CONFIG failed");
  2682. goto Error;
  2683. }
  2684. } else if (*pFormat == AudioSystem::AMR_NB) {
  2685. LOGI("Recording format: AMR_NB");
  2686. /* open amr_nb input device */
  2687. status = ::open(AMRNB_DEVICE_IN, O_RDONLY);
  2688. if (status < 0) {
  2689. LOGE("Cannot open amr_nb device for read");
  2690. goto Error;
  2691. }
  2692. mFd = status;
  2693. mDevices = devices;
  2694. mChannels = *pChannels;
  2695.  
  2696. if (mDevices == AudioSystem::DEVICE_IN_VOICE_CALL) {
  2697. if ((mChannels & AudioSystem::CHANNEL_IN_VOICE_DNLINK) &&
  2698. (mChannels & AudioSystem::CHANNEL_IN_VOICE_UPLINK)) {
  2699. LOGI("Recording Source: Voice Call Both Uplink and Downlink");
  2700. voc_rec_cfg.rec_mode = VOC_REC_BOTH;
  2701. } else if (mChannels & AudioSystem::CHANNEL_IN_VOICE_DNLINK) {
  2702. LOGI("Recording Source: Voice Call DownLink");
  2703. voc_rec_cfg.rec_mode = VOC_REC_DOWNLINK;
  2704. } else if (mChannels & AudioSystem::CHANNEL_IN_VOICE_UPLINK) {
  2705. LOGI("Recording Source: Voice Call UpLink");
  2706. voc_rec_cfg.rec_mode = VOC_REC_UPLINK;
  2707. }
  2708.  
  2709. if (ioctl(mFd, AUDIO_SET_INCALL, &voc_rec_cfg)) {
  2710. LOGE("Error: AUDIO_SET_INCALL failed");
  2711. goto Error;
  2712. }
  2713. }
  2714.  
  2715. /* Config param */
  2716. struct msm_audio_stream_config config;
  2717. if (ioctl(mFd, AUDIO_GET_STREAM_CONFIG, &config)) {
  2718. LOGE(" Error getting buf config param AUDIO_GET_STREAM_CONFIG");
  2719. goto Error;
  2720. }
  2721.  
  2722. LOGV("The Config buffer size is %d", config.buffer_size);
  2723. LOGV("The Config buffer count is %d", config.buffer_count);
  2724.  
  2725. mSampleRate = 8000;
  2726. mFormat = *pFormat;
  2727. mBufferSize = 320;
  2728. struct msm_audio_amrnb_enc_config_v2 amr_nb_cfg;
  2729.  
  2730. if (ioctl(mFd, AUDIO_GET_AMRNB_ENC_CONFIG_V2, &amr_nb_cfg)) {
  2731. LOGE("Error: AUDIO_GET_AMRNB_ENC_CONFIG_V2 failed");
  2732. goto Error;
  2733. }
  2734.  
  2735. LOGV("The Config band_mode is %d", amr_nb_cfg.band_mode);
  2736. LOGV("The Config dtx_enable is %d", amr_nb_cfg.dtx_enable);
  2737. LOGV("The Config frame_format is %d", amr_nb_cfg.frame_format);
  2738.  
  2739. amr_nb_cfg.band_mode = 7; /* Bit Rate 12.2 kbps MR122 */
  2740. amr_nb_cfg.dtx_enable= 0;
  2741. amr_nb_cfg.frame_format = 0; /* IF1 */
  2742.  
  2743. if (ioctl(mFd, AUDIO_SET_AMRNB_ENC_CONFIG_V2, &amr_nb_cfg)) {
  2744. LOGE("Error: AUDIO_SET_AMRNB_ENC_CONFIG_V2 failed");
  2745. goto Error;
  2746. }
  2747. }
  2748. #endif
  2749. else if (*pFormat == AudioSystem::AAC) {
  2750. LOGI("Recording format: AAC");
  2751. /* open aac input device */
  2752. status = ::open(AAC_DEVICE_IN, O_RDWR);
  2753. if (status < 0) {
  2754. LOGE("Cannot open aac device for read");
  2755. goto Error;
  2756. }
  2757. mFd = status;
  2758.  
  2759. struct msm_audio_stream_config config;
  2760. if (ioctl(mFd, AUDIO_GET_STREAM_CONFIG, &config)) {
  2761. LOGE(" Error getting buf config param AUDIO_GET_STREAM_CONFIG");
  2762. goto Error;
  2763. }
  2764.  
  2765. LOGE("The Config buffer size is %d", config.buffer_size);
  2766. LOGE("The Config buffer count is %d", config.buffer_count);
  2767.  
  2768. struct msm_audio_aac_enc_config aac_enc_cfg;
  2769. if (ioctl(mFd, AUDIO_GET_AAC_ENC_CONFIG, &aac_enc_cfg)) {
  2770. LOGE("Error: AUDIO_GET_AAC_ENC_CONFIG failed");
  2771. goto Error;
  2772. }
  2773.  
  2774. LOGV("The Config channels is %d", aac_enc_cfg.channels);
  2775. LOGV("The Config sample_rate is %d", aac_enc_cfg.sample_rate);
  2776. LOGV("The Config bit_rate is %d", aac_enc_cfg.bit_rate);
  2777. LOGV("The Config stream_format is %d", aac_enc_cfg.stream_format);
  2778.  
  2779. mDevices = devices;
  2780. mChannels = *pChannels;
  2781. aac_enc_cfg.sample_rate = mSampleRate = *pRate;
  2782. mFormat = *pFormat;
  2783. mBufferSize = 2048;
  2784. if (*pChannels & (AudioSystem::CHANNEL_IN_MONO))
  2785. aac_enc_cfg.channels = 1;
  2786. else if (*pChannels & (AudioSystem::CHANNEL_IN_STEREO))
  2787. aac_enc_cfg.channels = 2;
  2788.  
  2789. aac_enc_cfg.bit_rate = 128000;
  2790.  
  2791. LOGV("Setting the Config channels is %d", aac_enc_cfg.channels);
  2792. LOGV("Setting the Config sample_rate is %d", aac_enc_cfg.sample_rate);
  2793. LOGV("Setting the Config bit_rate is %d", aac_enc_cfg.bit_rate);
  2794. LOGV("Setting the Config stream_format is %d", aac_enc_cfg.stream_format);
  2795.  
  2796. if (ioctl(mFd, AUDIO_SET_AAC_ENC_CONFIG, &aac_enc_cfg)) {
  2797. LOGE("Error: AUDIO_SET_AAC_ENC_CONFIG failed");
  2798. goto Error;
  2799. }
  2800. }
  2801. /* mHardware->setMicMute_nosync(false); */
  2802. mState = AUDIO_INPUT_OPENED;
  2803.  
  2804. return NO_ERROR;
  2805.  
  2806. Error:
  2807. if (mFd >= 0) {
  2808. ::close(mFd);
  2809. mFd = -1;
  2810. }
  2811. return status;
  2812. }
  2813.  
  2814. AudioHardware::AudioStreamInMSM72xx::~AudioStreamInMSM72xx() {
  2815. LOGV("AudioStreamInMSM72xx destructor");
  2816. standby();
  2817. }
  2818.  
  2819. ssize_t AudioHardware::AudioStreamInMSM72xx::read( void* buffer, ssize_t bytes) {
  2820. unsigned short dec_id = INVALID_DEVICE;
  2821. /* LOGV("AudioStreamInMSM72xx::read(%p, %ld)", buffer, bytes); */
  2822. if (!mHardware)
  2823. return -1;
  2824.  
  2825. size_t count = bytes;
  2826. size_t aac_framesize= bytes;
  2827. uint8_t* p = static_cast<uint8_t*>(buffer);
  2828. uint32_t* recogPtr = (uint32_t *)p;
  2829. uint16_t* frameCountPtr;
  2830. uint16_t* frameSizePtr;
  2831.  
  2832. if (mState < AUDIO_INPUT_OPENED) {
  2833. AudioHardware *hw = mHardware;
  2834. hw->mLock.lock();
  2835. status_t status = set(hw, mDevices, &mFormat, &mChannels, &mSampleRate, mAcoustics);
  2836. if (status != NO_ERROR) {
  2837. hw->mLock.unlock();
  2838. return -1;
  2839. }
  2840. if ((mDevices == AudioSystem::DEVICE_IN_FM_RX) ||
  2841. (mDevices == AudioSystem::DEVICE_IN_FM_RX_A2DP)) {
  2842. /*
  2843. if (ioctl(mFd, AUDIO_GET_SESSION_ID, &dec_id)) {
  2844. LOGE("AUDIO_GET_SESSION_ID failed*********");
  2845. hw->mLock.unlock();
  2846. return -1;
  2847. }
  2848.  
  2849. if (enableDevice(DEVICE_FMRADIO_STEREO_TX, 1)) {
  2850. LOGE("enableDevice failed for device %d",DEVICE_FMRADIO_STEREO_TX);
  2851. hw->mLock.unlock();
  2852. return -1;
  2853. }
  2854.  
  2855. if (msm_route_stream(PCM_REC, dec_id, DEV_ID(DEVICE_FMRADIO_STEREO_TX), 1)) {
  2856. LOGE("msm_route_stream failed");
  2857. hw->mLock.unlock();
  2858. return -1;
  2859. }
  2860. mFirstread = false;
  2861. if (mDevices == AudioSystem::DEVICE_IN_FM_RX_A2DP) {
  2862. addToTable(dec_id,cur_tx,INVALID_DEVICE,FM_A2DP,true);
  2863. mFmRec = FM_A2DP_REC;
  2864. }
  2865. else {
  2866. addToTable(dec_id,cur_tx,INVALID_DEVICE,FM_REC,true);
  2867. mFmRec = FM_FILE_REC;
  2868. }
  2869. hw->mLock.unlock();
  2870. */
  2871. } else {
  2872. hw->mLock.unlock();
  2873. if (ioctl(mFd, AUDIO_GET_SESSION_ID, &dec_id)) {
  2874. LOGE("AUDIO_GET_SESSION_ID failed*********");
  2875. return -1;
  2876. }
  2877. LOGV("dec_id = %d,cur_tx= %d", dec_id, cur_tx);
  2878. if (cur_tx == INVALID_DEVICE)
  2879. cur_tx = DEVICE_HANDSET_TX;
  2880.  
  2881. Mutex::Autolock lock(mDeviceSwitchLock);
  2882.  
  2883. if (enableDevice(cur_tx, 1)) {
  2884. LOGE("enableDevice failed, device %d", cur_tx);
  2885. return -1;
  2886. }
  2887.  
  2888. uint32_t tx_acdb_id = mHardware->getACDB(MOD_REC, mHardware->get_snd_dev());
  2889. updateACDB(cur_rx, cur_tx, 0, tx_acdb_id);
  2890.  
  2891. if (msm_route_stream(PCM_REC, dec_id, DEV_ID(cur_tx), 1)) {
  2892. LOGE("msm_route_stream failed");
  2893. return -1;
  2894. }
  2895. addToTable(dec_id,cur_tx, INVALID_DEVICE, PCM_REC, true);
  2896. mFirstread = false;
  2897. }
  2898. }
  2899.  
  2900. if (mState < AUDIO_INPUT_STARTED) {
  2901. /* force routing to input device */
  2902. mHardware->set_mRecordState(true);
  2903. mHardware->clearCurDevice();
  2904. mHardware->doRouting(this);
  2905. if (support_aic3254) {
  2906. int snd_dev = mHardware->get_snd_dev();
  2907. mHardware->aic3254_config(snd_dev, "", "");
  2908. mHardware->do_aic3254_control(snd_dev);
  2909. }
  2910. if (ioctl(mFd, AUDIO_START, 0)) {
  2911. LOGE("Error starting record");
  2912. standby();
  2913. return -1;
  2914. }
  2915. mState = AUDIO_INPUT_STARTED;
  2916. }
  2917.  
  2918. bytes = 0;
  2919. if (mFormat == AUDIO_HW_IN_FORMAT) {
  2920. while (count) {
  2921. ssize_t bytesRead = ::read(mFd, buffer, count);
  2922. if (bytesRead >= 0) {
  2923. count -= bytesRead;
  2924. p += bytesRead;
  2925. bytes += bytesRead;
  2926. if (!mFirstread) {
  2927. mFirstread = true;
  2928. break;
  2929. }
  2930. } else {
  2931. if (errno != EAGAIN)
  2932. return bytesRead;
  2933.  
  2934. mRetryCount++;
  2935. LOGW("EAGAIN - retrying");
  2936. }
  2937. }
  2938. }
  2939. #ifdef WITH_QCOM_SPEECH
  2940. else if ((mFormat == AudioSystem::EVRC) || (mFormat == AudioSystem::QCELP) || (mFormat == AudioSystem::AMR_NB)) {
  2941. uint8_t readBuf[36];
  2942. uint8_t *dataPtr;
  2943. while (count) {
  2944. dataPtr = readBuf;
  2945. ssize_t bytesRead = ::read(mFd, readBuf, 36);
  2946. if (bytesRead >= 0) {
  2947. if (mFormat == AudioSystem::AMR_NB) {
  2948. amr_transcode(dataPtr,p);
  2949. p += AMRNB_FRAME_SIZE;
  2950. count -= AMRNB_FRAME_SIZE;
  2951. bytes += AMRNB_FRAME_SIZE;
  2952. if (!mFirstread) {
  2953. mFirstread = true;
  2954. break;
  2955. }
  2956. } else {
  2957. dataPtr++;
  2958. if (mFormat == AudioSystem::EVRC) {
  2959. memcpy(p, dataPtr, EVRC_FRAME_SIZE);
  2960. p += EVRC_FRAME_SIZE;
  2961. count -= EVRC_FRAME_SIZE;
  2962. bytes += EVRC_FRAME_SIZE;
  2963. if (!mFirstread) {
  2964. mFirstread = true;
  2965. break;
  2966. }
  2967. } else if (mFormat == AudioSystem::QCELP) {
  2968. memcpy(p, dataPtr, QCELP_FRAME_SIZE);
  2969. p += QCELP_FRAME_SIZE;
  2970. count -= QCELP_FRAME_SIZE;
  2971. bytes += QCELP_FRAME_SIZE;
  2972. if (!mFirstread) {
  2973. mFirstread = true;
  2974. break;
  2975. }
  2976. }
  2977. }
  2978. } else {
  2979. if (errno != EAGAIN)
  2980. return bytesRead;
  2981.  
  2982. mRetryCount++;
  2983. LOGW("EAGAIN - retrying");
  2984. }
  2985. }
  2986. }
  2987. #endif
  2988. else if (mFormat == AudioSystem::AAC) {
  2989. *((uint32_t*)recogPtr) = 0x51434F4D ; /* ('Q','C','O','M') Number to identify format as AAC by higher layers */
  2990. recogPtr++;
  2991. frameCountPtr = (uint16_t*)recogPtr;
  2992. *frameCountPtr = 0;
  2993. p += 3*sizeof(uint16_t);
  2994. count -= 3*sizeof(uint16_t);
  2995.  
  2996. while (count > 0) {
  2997. frameSizePtr = (uint16_t *)p;
  2998. p += sizeof(uint16_t);
  2999. if (!(count > 2))
  3000. break;
  3001.  
  3002. count -= sizeof(uint16_t);
  3003.  
  3004. ssize_t bytesRead = ::read(mFd, p, count);
  3005. if (bytesRead > 0) {
  3006. LOGV("Number of Bytes read = %d", bytesRead);
  3007. count -= bytesRead;
  3008. p += bytesRead;
  3009. bytes += bytesRead;
  3010. LOGV("Total Number of Bytes read = %d", bytes);
  3011.  
  3012. *frameSizePtr = bytesRead;
  3013. (*frameCountPtr)++;
  3014. if (!mFirstread) {
  3015. mFirstread = true;
  3016. break;
  3017. }
  3018. /* Typical frame size for AAC is around 250 bytes. So we have
  3019. taken the minimum buffer size as twice of this size i.e.
  3020. 512 to avoid short reads from driver */
  3021. if (count < 512) {
  3022. LOGI("buffer passed to driver %d, is less than the min 512 bytes", count);
  3023. break;
  3024. }
  3025. } else if (bytesRead == 0) {
  3026. LOGI("Bytes Read = %d ,Buffer no longer sufficient", bytesRead);
  3027. break;
  3028. } else {
  3029. if (errno != EAGAIN)
  3030. return bytesRead;
  3031.  
  3032. mRetryCount++;
  3033. LOGW("EAGAIN - retrying");
  3034. }
  3035. }
  3036. }
  3037.  
  3038. if (mFormat == AudioSystem::AAC)
  3039. return aac_framesize;
  3040.  
  3041. return bytes;
  3042. }
  3043.  
  3044. status_t AudioHardware::AudioStreamInMSM72xx::standby() {
  3045. bool isDriverClosed = false;
  3046. LOGD("AudioStreamInMSM72xx::standby()");
  3047. Routing_table* temp = NULL;
  3048.  
  3049. if (mHardware) {
  3050. mHardware->set_mRecordState(false);
  3051. if (support_aic3254) {
  3052. int snd_dev = mHardware->get_snd_dev();
  3053. mHardware->aic3254_config(snd_dev, "", "");
  3054. mHardware->do_aic3254_control(snd_dev);
  3055. }
  3056. }
  3057.  
  3058. if (!mHardware)
  3059. return -1;
  3060.  
  3061. if (mState > AUDIO_INPUT_CLOSED) {
  3062. if (mFd >= 0) {
  3063. ::close(mFd);
  3064. mFd = -1;
  3065. LOGV("driver closed");
  3066. isDriverClosed = true;
  3067. }
  3068. /* mHardware->checkMicMute(); */
  3069. mState = AUDIO_INPUT_CLOSED;
  3070. }
  3071.  
  3072. temp = getNodeByStreamType(PCM_REC);
  3073. if (temp == NULL)
  3074. return NO_ERROR;
  3075.  
  3076. if (isDriverClosed) {
  3077. LOGD("Deroute pcm in stream");
  3078. if (msm_route_stream(PCM_REC, temp->dec_id, DEV_ID(temp->dev_id), 0)) {
  3079. LOGE("could not set stream routing");
  3080. deleteFromTable(PCM_REC);
  3081. return -1;
  3082. }
  3083. LOGV("Disable device");
  3084. deleteFromTable(PCM_REC);
  3085. updateDeviceInfo(cur_rx, cur_tx, 0, 0);
  3086. } /* mRecordingSession condition.
  3087. restore output routing if necessary */
  3088. mHardware->clearCurDevice();
  3089. mHardware->doRouting(this);
  3090.  
  3091. return NO_ERROR;
  3092. }
  3093.  
  3094. status_t AudioHardware::AudioStreamInMSM72xx::dump(int fd, const Vector<String16>& args) {
  3095. const size_t SIZE = 256;
  3096. char buffer[SIZE];
  3097. String8 result;
  3098. result.append("AudioStreamInMSM72xx::dump\n");
  3099. snprintf(buffer, SIZE, "\tsample rate: %d\n", sampleRate());
  3100. result.append(buffer);
  3101. snprintf(buffer, SIZE, "\tbuffer size: %d\n", bufferSize());
  3102. result.append(buffer);
  3103. snprintf(buffer, SIZE, "\tchannels: %d\n", channels());
  3104. result.append(buffer);
  3105. snprintf(buffer, SIZE, "\tformat: %d\n", format());
  3106. result.append(buffer);
  3107. snprintf(buffer, SIZE, "\tmHardware: %p\n", mHardware);
  3108. result.append(buffer);
  3109. snprintf(buffer, SIZE, "\tmFd count: %d\n", mFd);
  3110. result.append(buffer);
  3111. snprintf(buffer, SIZE, "\tmState: %d\n", mState);
  3112. result.append(buffer);
  3113. snprintf(buffer, SIZE, "\tmRetryCount: %d\n", mRetryCount);
  3114. result.append(buffer);
  3115. ::write(fd, result.string(), result.size());
  3116. return NO_ERROR;
  3117. }
  3118.  
  3119. status_t AudioHardware::AudioStreamInMSM72xx::setParameters(const String8& keyValuePairs) {
  3120. AudioParameter param = AudioParameter(keyValuePairs);
  3121. status_t status = NO_ERROR;
  3122. int device;
  3123. String8 key = String8(AudioParameter::keyInputSource);
  3124. int source;
  3125. LOGV("AudioStreamInMSM72xx::setParameters() %s", keyValuePairs.string());
  3126.  
  3127. key = String8(AudioParameter::keyRouting);
  3128. if (param.getInt(key, device) == NO_ERROR) {
  3129. LOGV("set input routing %x", device);
  3130. if (device & (device - 1))
  3131. status = BAD_VALUE;
  3132. else {
  3133. mDevices = device;
  3134. status = mHardware->doRouting(this);
  3135. }
  3136. param.remove(key);
  3137. }
  3138.  
  3139. if (param.size())
  3140. status = BAD_VALUE;
  3141.  
  3142. return status;
  3143. }
  3144.  
  3145. String8 AudioHardware::AudioStreamInMSM72xx::getParameters(const String8& keys) {
  3146. AudioParameter param = AudioParameter(keys);
  3147. String8 value;
  3148. String8 key = String8(AudioParameter::keyRouting);
  3149.  
  3150. if (param.get(key, value) == NO_ERROR) {
  3151. LOGV("get routing %x", mDevices);
  3152. param.addInt(key, (int)mDevices);
  3153. }
  3154.  
  3155. LOGV("AudioStreamInMSM72xx::getParameters() %s", param.toString().string());
  3156. return param.toString();
  3157. }
  3158.  
  3159. /* getActiveInput_l() must be called with mLock held */
  3160. AudioHardware::AudioStreamInMSM72xx *AudioHardware::getActiveInput_l() {
  3161. for (size_t i = 0; i < mInputs.size(); i++) {
  3162. /* return first input found not being in standby mode
  3163. as only one input can be in this state */
  3164. if (mInputs[i]->state() > AudioStreamInMSM72xx::AUDIO_INPUT_CLOSED)
  3165. return mInputs[i];
  3166. }
  3167.  
  3168. return NULL;
  3169. }
  3170. /* ---------------------------------------------------------------------------- */
  3171.  
  3172. extern "C" AudioHardwareInterface* createAudioHardware(void) {
  3173. return new AudioHardware();
  3174. }
  3175.  
  3176. #ifdef WITH_QCOM_SPEECH
  3177. /*===========================================================================
  3178. FUNCTION amrsup_frame_len
  3179.  
  3180. DESCRIPTION
  3181. This function will determine number of bytes of AMR vocoder frame length
  3182. based on the frame type and frame rate.
  3183.  
  3184. DEPENDENCIES
  3185. None.
  3186.  
  3187. RETURN VALUE
  3188. number of bytes of AMR frame
  3189.  
  3190. SIDE EFFECTS
  3191. None.
  3192. ===========================================================================*/
  3193. int amrsup_frame_len_bits(amrsup_frame_type frame_type,
  3194. amrsup_mode_type amr_mode) {
  3195. int frame_len=0;
  3196.  
  3197. switch (frame_type) {
  3198. case AMRSUP_SPEECH_GOOD:
  3199. case AMRSUP_SPEECH_DEGRADED:
  3200. case AMRSUP_ONSET:
  3201. case AMRSUP_SPEECH_BAD:
  3202. if (amr_mode >= AMRSUP_MODE_MAX)
  3203. frame_len = 0;
  3204. else
  3205. frame_len = amrsup_122_framing.len_a
  3206. + amrsup_122_framing.len_b
  3207. + amrsup_122_framing.len_c;
  3208. break;
  3209. case AMRSUP_SID_FIRST:
  3210. case AMRSUP_SID_UPDATE:
  3211. case AMRSUP_SID_BAD:
  3212. frame_len = AMR_CLASS_A_BITS_SID;
  3213. break;
  3214. case AMRSUP_NO_DATA:
  3215. case AMRSUP_SPEECH_LOST:
  3216. default:
  3217. frame_len = 0;
  3218. }
  3219.  
  3220. return frame_len;
  3221. }
  3222.  
  3223. /*===========================================================================
  3224. FUNCTION amrsup_frame_len
  3225.  
  3226. DESCRIPTION
  3227. This function will determine number of bytes of AMR vocoder frame length
  3228. based on the frame type and frame rate.
  3229.  
  3230. DEPENDENCIES
  3231. None.
  3232.  
  3233. RETURN VALUE
  3234. number of bytes of AMR frame
  3235.  
  3236. SIDE EFFECTS
  3237. None.
  3238. ===========================================================================*/
  3239. int amrsup_frame_len(amrsup_frame_type frame_type, amrsup_mode_type amr_mode) {
  3240. int frame_len = amrsup_frame_len_bits(frame_type, amr_mode);
  3241.  
  3242. frame_len = (frame_len + 7) / 8;
  3243. return frame_len;
  3244. }
  3245.  
  3246. /*===========================================================================
  3247. FUNCTION amrsup_tx_order
  3248.  
  3249. DESCRIPTION
  3250. Use a bit ordering table to order bits from their original sequence.
  3251.  
  3252. DEPENDENCIES
  3253. None.
  3254.  
  3255. RETURN VALUE
  3256. None.
  3257.  
  3258. SIDE EFFECTS
  3259. None.
  3260. ===========================================================================*/
  3261. void amrsup_tx_order(
  3262. unsigned char *dst_frame,
  3263. int *dst_bit_index,
  3264. unsigned char *src,
  3265. int num_bits,
  3266. const unsigned short *order) {
  3267.  
  3268. unsigned long dst_mask = 0x00000080 >> ((*dst_bit_index) & 0x7);
  3269. unsigned char *dst = &dst_frame[((unsigned int) *dst_bit_index) >> 3];
  3270. unsigned long src_bit, src_mask;
  3271.  
  3272. /* Prepare to process all bits */
  3273. *dst_bit_index += num_bits;
  3274. num_bits++;
  3275.  
  3276. while(--num_bits) {
  3277. /* Get the location of the bit in the input buffer */
  3278. src_bit = (unsigned long ) *order++;
  3279. src_mask = 0x00000080 >> (src_bit & 0x7);
  3280.  
  3281. /* Set the value of the output bit equal to the input bit */
  3282. if (src[src_bit >> 3] & src_mask) {
  3283. *dst |= (unsigned char ) dst_mask;
  3284. }
  3285.  
  3286. /* Set the destination bit mask and increment pointer if necessary */
  3287. dst_mask >>= 1;
  3288. if (dst_mask == 0) {
  3289. dst_mask = 0x00000080;
  3290. dst++;
  3291. }
  3292. }
  3293. }
  3294.  
  3295. /*===========================================================================
  3296. FUNCTION amrsup_if1_framing
  3297.  
  3298. DESCRIPTION
  3299. Performs the transmit side framing function. Generates AMR IF1 ordered data
  3300. from the vocoder packet and frame type.
  3301.  
  3302. DEPENDENCIES
  3303. None.
  3304.  
  3305. RETURN VALUE
  3306. number of bytes of encoded frame.
  3307. if1_frame : IF1-encoded frame.
  3308. if1_frame_info : holds frame information of IF1-encoded frame.
  3309.  
  3310. SIDE EFFECTS
  3311. None.
  3312. ===========================================================================*/
  3313. static int amrsup_if1_framing(
  3314. unsigned char *vocoder_packet,
  3315. amrsup_frame_type frame_type,
  3316. amrsup_mode_type amr_mode,
  3317. unsigned char *if1_frame,
  3318. amrsup_if1_frame_info_type *if1_frame_info) {
  3319.  
  3320. amrsup_frame_order_type *ordering_table;
  3321. int frame_len = 0;
  3322. int i;
  3323.  
  3324. if (amr_mode >= AMRSUP_MODE_MAX) {
  3325. LOGE("Invalid AMR_Mode : %d", amr_mode);
  3326. return 0;
  3327. }
  3328.  
  3329. /* Initialize IF1 frame data and info */
  3330. if1_frame_info->fqi = true;
  3331. if1_frame_info->amr_type = AMRSUP_CODEC_AMR_NB;
  3332. memset(if1_frame, 0, amrsup_frame_len(AMRSUP_SPEECH_GOOD, AMRSUP_MODE_1220));
  3333.  
  3334. switch (frame_type) {
  3335. case AMRSUP_SID_BAD:
  3336. if1_frame_info->fqi = false;
  3337. /* fall thru */
  3338. case AMRSUP_SID_FIRST:
  3339. case AMRSUP_SID_UPDATE:
  3340. /* Set frame type index */
  3341. if1_frame_info->frame_type_index = AMRSUP_FRAME_TYPE_INDEX_AMR_SID;
  3342. /* ===== Encoding SID frame ===== */
  3343. /* copy the sid frame to class_a data */
  3344. for (i=0; i<5; i++) {
  3345. if1_frame[i] = vocoder_packet[i];
  3346. }
  3347.  
  3348. /* Set the SID type : SID_FIRST: Bit 35 = 0, SID_UPDATE : Bit 35 = 1 */
  3349. if (frame_type == AMRSUP_SID_FIRST)
  3350. if1_frame[4] &= ~0x10;
  3351.  
  3352. if (frame_type == AMRSUP_SID_UPDATE)
  3353. if1_frame[4] |= 0x10;
  3354. else {
  3355. /* Set the mode (Bit 36 - 38 = amr_mode with bits swapped) */
  3356. if1_frame[4] |= (((unsigned char)amr_mode << 3) & 0x08) |
  3357. (((unsigned char)amr_mode << 1) & 0x04) |
  3358. (((unsigned char)amr_mode >> 1) & 0x02);
  3359.  
  3360. frame_len = AMR_CLASS_A_BITS_SID;
  3361. }
  3362. break;
  3363. case AMRSUP_SPEECH_BAD:
  3364. if1_frame_info->fqi = false;
  3365. /* fall thru */
  3366. case AMRSUP_SPEECH_GOOD:
  3367. /* Set frame type index */
  3368. if1_frame_info->frame_type_index = (amrsup_frame_type_index_type)(amr_mode);
  3369. /* ===== Encoding Speech frame ===== */
  3370. /* Clear num bits in frame */
  3371. frame_len = 0;
  3372.  
  3373. /* Select ordering table */
  3374. ordering_table = (amrsup_frame_order_type*)&amrsup_122_framing;
  3375.  
  3376. amrsup_tx_order(
  3377. if1_frame,
  3378. &frame_len,
  3379. vocoder_packet,
  3380. ordering_table->len_a,
  3381. ordering_table->class_a
  3382. );
  3383.  
  3384. amrsup_tx_order(
  3385. if1_frame,
  3386. &frame_len,
  3387. vocoder_packet,
  3388. ordering_table->len_b,
  3389. ordering_table->class_b
  3390. );
  3391.  
  3392. amrsup_tx_order(
  3393. if1_frame,
  3394. &frame_len,
  3395. vocoder_packet,
  3396. ordering_table->len_c,
  3397. ordering_table->class_c
  3398. );
  3399. /* frame_len already updated with correct number of bits */
  3400. break;
  3401. default:
  3402. LOGE("Unsupported frame type %d", frame_type);
  3403. /* fall thru */
  3404. case AMRSUP_NO_DATA:
  3405. /* Set frame type index */
  3406. if1_frame_info->frame_type_index = AMRSUP_FRAME_TYPE_INDEX_NO_DATA;
  3407. frame_len = 0;
  3408. break;
  3409. }
  3410.  
  3411. /* convert bit length to byte length */
  3412. frame_len = (frame_len + 7) / 8;
  3413.  
  3414. return frame_len;
  3415. }
  3416.  
  3417. static void amr_transcode(unsigned char *src, unsigned char *dst) {
  3418. amrsup_frame_type frame_type_in = (amrsup_frame_type) *(src++);
  3419. amrsup_mode_type frame_rate_in = (amrsup_mode_type) *(src++);
  3420. amrsup_if1_frame_info_type frame_info_out;
  3421. unsigned char frameheader;
  3422.  
  3423. amrsup_if1_framing(src, frame_type_in, frame_rate_in, dst+1, &frame_info_out);
  3424. frameheader = (frame_info_out.frame_type_index << 3) + (frame_info_out.fqi << 2);
  3425. *dst = frameheader;
  3426.  
  3427. return;
  3428. }
  3429. #endif
  3430.  
  3431. }; /* namespace android */
Advertisement
Add Comment
Please, Sign In to add comment