Advertisement
Guest User

Modded audio_hw.conf

a guest
Sep 19th, 2014
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 79.18 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2011 The Android Open Source Project
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16.  
  17. #define LOG_TAG "audio_hw_primary"
  18. //#define LOG_NDEBUG 0
  19.  
  20. #include <errno.h>
  21. #include <pthread.h>
  22. #include <stdint.h>
  23. #include <sys/time.h>
  24. #include <stdlib.h>
  25.  
  26. #include <cutils/log.h>
  27. #include <cutils/str_parms.h>
  28. #include <cutils/properties.h>
  29.  
  30. #include <hardware/hardware.h>
  31. #include <system/audio.h>
  32. #include <hardware/audio.h>
  33.  
  34. #include <tinyalsa/asoundlib.h>
  35. #include <audio_utils/resampler.h>
  36. #include <audio_utils/echo_reference.h>
  37. #include <hardware/audio_effect.h>
  38. #include <audio_effects/effect_aec.h>
  39. #include <fcntl.h>
  40.  
  41. #include "audio_ril.h"
  42.  
  43. #include <cutils/properties.h> // for property_get
  44.  
  45. #define F_LOG ALOGV("%s, line: %d", __FUNCTION__, __LINE__);
  46.  
  47. #define  CALL_VOLUME_MAX    59
  48. #define  CALL_VOLUME_MIN    0
  49. static int call_volume = CALL_VOLUME_MAX;
  50.  
  51. /* Mixer control names */
  52. #define MIXER_MASTER_PLAYBACK_VOLUME        "Master Playback Volume"
  53. #define MIXER_AUDIO_SPEAKER_OUT             "Audio speaker out"
  54.  
  55. /* ALSA cards for A1X */
  56. #define CARD_A1X_CODEC      0
  57. #define CARD_A1X_HDMI       1
  58. #define CARD_A1X_SPDIF      2
  59. #define CARD_A1X_DEFAULT    CARD_A1X_CODEC
  60.  
  61. /* ALSA ports for A10 */
  62. #define PORT_CODEC          0
  63. #define PORT_HDMI           0
  64. #define PORT_SPDIF          0
  65. #define PORT_MODEM          0
  66.  
  67. #define SAMPLING_RATE_8K    8000
  68. #define SAMPLING_RATE_11K   11025
  69. #define SAMPLING_RATE_44K   44100
  70. #define SAMPLING_RATE_48K   48000
  71.  
  72. /* constraint imposed by ABE: all period sizes must be multiples of 24 */
  73. #define ABE_BASE_FRAME_COUNT 24
  74. /* number of base blocks in a short period (low latency) */
  75. //#define SHORT_PERIOD_MULTIPLIER 44  /* 22 ms */
  76. #define SHORT_PERIOD_MULTIPLIER 44  /* 40 ms */
  77. /* number of frames per short period (low latency) */
  78. #define SHORT_PERIOD_SIZE (ABE_BASE_FRAME_COUNT * SHORT_PERIOD_MULTIPLIER)
  79. /* number of short periods in a long period (low power) */
  80. //#define LONG_PERIOD_MULTIPLIER 14  /* 308 ms */
  81. #define LONG_PERIOD_MULTIPLIER 6  /* 240 ms */
  82. /* number of frames per long period (low power) */
  83. #define LONG_PERIOD_SIZE (SHORT_PERIOD_SIZE * LONG_PERIOD_MULTIPLIER)
  84. /* number of pseudo periods for playback */
  85. #define PLAYBACK_PERIOD_COUNT 4
  86. /* number of periods for capture */
  87. #define CAPTURE_PERIOD_COUNT 4
  88. /* minimum sleep time in out_write() when write threshold is not reached */
  89. #define MIN_WRITE_SLEEP_US 5000
  90.  
  91. #define RESAMPLER_BUFFER_FRAMES (SHORT_PERIOD_SIZE * 2)
  92. #define RESAMPLER_BUFFER_SIZE (4 * RESAMPLER_BUFFER_FRAMES)
  93.  
  94. /* android default out sampling rate*/
  95. #define DEFAULT_OUT_SAMPLING_RATE SAMPLING_RATE_44K
  96.  
  97. /* audio codec default sampling rate*/
  98. #define MM_SAMPLING_RATE SAMPLING_RATE_44K
  99.  
  100. /*wifi display buffer size*/
  101. #define AF_BUFFER_SIZE 1024 * 80
  102.  
  103. enum tty_modes {
  104.     TTY_MODE_OFF,
  105.     TTY_MODE_VCO,
  106.     TTY_MODE_HCO,
  107.     TTY_MODE_FULL
  108. };
  109.  
  110. struct pcm_config pcm_config_mm_out = {
  111.     .channels = 2,
  112.     .rate = MM_SAMPLING_RATE,
  113.     .period_size = SHORT_PERIOD_SIZE,
  114.     .period_count = PLAYBACK_PERIOD_COUNT,
  115.     .format = PCM_FORMAT_S16_LE,
  116. };
  117.  
  118. struct pcm_config pcm_config_mm_in = {
  119.     .channels = 2,
  120.     .rate = MM_SAMPLING_RATE,
  121.     .period_size = 1024,
  122.     .period_count = CAPTURE_PERIOD_COUNT,
  123.     .format = PCM_FORMAT_S16_LE,
  124. };
  125.  
  126. struct route_setting
  127. {
  128.     char *ctl_name;
  129.     int intval;
  130.     char *strval;
  131. };
  132.  
  133. struct mixer_ctls
  134. {
  135.     struct mixer_ctl *master_playback_volume;
  136.     struct mixer_ctl *audio_speaker_out;
  137. };
  138.  
  139. struct pcm_buf_manager
  140. {
  141.     pthread_mutex_t lock;               /* see note below on mutex acquisition order */
  142.     bool            BufExist;
  143.     unsigned char   *BufStart;        
  144.     int             BufTotalLen;      
  145.     unsigned char   *BufReadPtr;      
  146.     int             DataLen;          
  147.     unsigned char   *BufWritPtr;      
  148.     int             BufValideLen;      
  149.     int             SampleRate;
  150.     int             Channel;
  151. };
  152.  
  153. struct sunxi_audio_device {
  154.     struct audio_hw_device hw_device;
  155.  
  156.     pthread_mutex_t lock;       /* see note below on mutex acquisition order */
  157.     struct mixer *mixer;
  158.     struct mixer_ctls mixer_ctls;
  159.     int mode;
  160.     int devices;
  161.     struct pcm *pcm_modem_dl;
  162.     struct pcm *pcm_modem_ul;
  163.     int in_call;
  164.     float voice_volume;
  165.     struct sunxi_stream_in *active_input;
  166.     struct sunxi_stream_out *active_output;
  167.     bool mic_mute;
  168.     int tty_mode;
  169.     struct echo_reference_itfe *echo_reference;
  170.     bool bluetooth_nrec;
  171.     int wb_amr;
  172.     bool raw_flag;      // flag for raw data
  173.  
  174.     bool af_capture_flag;
  175.     struct pcm_buf_manager PcmManager;
  176. };
  177.  
  178. struct sunxi_stream_out {
  179.     struct audio_stream_out stream;
  180.  
  181.     pthread_mutex_t lock;       /* see note below on mutex acquisition order */
  182.     struct pcm_config config;
  183.     struct pcm *pcm;
  184.     struct resampler_itfe *resampler;
  185.     char *buffer;
  186.     int standby;
  187.     struct echo_reference_itfe *echo_reference;
  188.     struct sunxi_audio_device *dev;
  189.     int write_threshold;
  190. };
  191.  
  192. #define MAX_PREPROCESSORS 3 /* maximum one AGC + one NS + one AEC per input stream */
  193.  
  194. struct sunxi_stream_in {
  195.     struct audio_stream_in stream;
  196.  
  197.     pthread_mutex_t lock;       /* see note below on mutex acquisition order */
  198.     struct pcm_config config;
  199.     struct pcm *pcm;
  200.     int device;
  201.     struct resampler_itfe *resampler;
  202.     struct resampler_buffer_provider buf_provider;
  203.     int16_t *buffer;
  204.     size_t frames_in;
  205.     unsigned int requested_rate;
  206.     int standby;
  207.     int source;
  208.     struct echo_reference_itfe *echo_reference;
  209.     bool need_echo_reference;
  210.     effect_handle_t preprocessors[MAX_PREPROCESSORS];
  211.     int num_preprocessors;
  212.     int16_t *proc_buf;
  213.     size_t proc_buf_size;
  214.     size_t proc_frames_in;
  215.     int16_t *ref_buf;
  216.     size_t ref_buf_size;
  217.     size_t ref_frames_in;
  218.     int read_status;
  219.  
  220.     struct sunxi_audio_device *dev;
  221. };
  222.  
  223. #if !LOG_NDEBUG
  224. // for test
  225. static void tinymix_print_enum(struct mixer_ctl *ctl, int print_all)
  226. {
  227.     unsigned int num_enums;
  228.     unsigned int i;
  229.     const char *string;
  230.  
  231.     num_enums = mixer_ctl_get_num_enums(ctl);
  232.  
  233.     for (i = 0; i < num_enums; i++) {
  234.         string = mixer_ctl_get_enum_string(ctl, i);
  235.         if (print_all)
  236.             printf("\t%s%s", mixer_ctl_get_value(ctl, 0) == (int)i ? ">" : "",
  237.                    string);
  238.         else if (mixer_ctl_get_value(ctl, 0) == (int)i)
  239.             printf(" %-s", string);
  240.     }
  241. }
  242.  
  243. static void tinymix_detail_control(struct mixer *mixer, unsigned int id,
  244.                                    int print_all)
  245. {
  246.     struct mixer_ctl *ctl;
  247.     enum mixer_ctl_type type;
  248.     unsigned int num_values;
  249.     unsigned int i;
  250.     int min, max;
  251.  
  252.     if (id >= mixer_get_num_ctls(mixer)) {
  253.         fprintf(stderr, "Invalid mixer control\n");
  254.         return;
  255.     }
  256.  
  257.     ctl = mixer_get_ctl(mixer, id);
  258.  
  259.     type = mixer_ctl_get_type(ctl);
  260.     num_values = mixer_ctl_get_num_values(ctl);
  261.  
  262.     if (print_all)
  263.         printf("%s:", mixer_ctl_get_name(ctl));
  264.  
  265.     for (i = 0; i < num_values; i++) {
  266.         switch (type)
  267.         {
  268.         case MIXER_CTL_TYPE_INT:
  269.             printf(" %d", mixer_ctl_get_value(ctl, i));
  270.             break;
  271.         case MIXER_CTL_TYPE_BOOL:
  272.             printf(" %s", mixer_ctl_get_value(ctl, i) ? "On" : "Off");
  273.             break;
  274.         case MIXER_CTL_TYPE_ENUM:
  275.             tinymix_print_enum(ctl, print_all);
  276.             break;
  277.          case MIXER_CTL_TYPE_BYTE:
  278.             printf(" 0x%02x", mixer_ctl_get_value(ctl, i));
  279.             break;
  280.         default:
  281.             printf(" unknown");
  282.             break;
  283.         };
  284.     }
  285.  
  286.     if (print_all) {
  287.         if (type == MIXER_CTL_TYPE_INT) {
  288.             min = mixer_ctl_get_range_min(ctl);
  289.             max = mixer_ctl_get_range_max(ctl);
  290.             printf(" (range %d->%d)", min, max);
  291.         }
  292.     }
  293.     printf("\n");
  294. }
  295.  
  296. static void tinymix_list_controls(struct mixer *mixer)
  297. {
  298.     struct mixer_ctl *ctl;
  299.     const char *name, *type;
  300.     unsigned int num_ctls, num_values;
  301.     unsigned int i;
  302.  
  303.     num_ctls = mixer_get_num_ctls(mixer);
  304.  
  305.     printf("Number of controls: %d\n", num_ctls);
  306.  
  307.     printf("ctl\ttype\tnum\t%-40s value\n", "name");
  308.     for (i = 0; i < num_ctls; i++) {
  309.         ctl = mixer_get_ctl(mixer, i);
  310.  
  311.         name = mixer_ctl_get_name(ctl);
  312.         type = mixer_ctl_get_type_string(ctl);
  313.         num_values = mixer_ctl_get_num_values(ctl);
  314.         printf("%d\t%s\t%d\t%-40s", i, type, num_values, name);
  315.         tinymix_detail_control(mixer, i, 0);
  316.     }
  317. }
  318. #endif
  319.  
  320. /*wifi display buffer manager*/
  321. static int WritePcmData(void * pInbuf, int inSize, struct pcm_buf_manager *PcmManager)
  322. {
  323.     ALOGV("RequestWriteBuf ++: size: %d", inSize);
  324.  
  325.     if (PcmManager->BufValideLen< inSize)
  326.     {
  327.         ALOGE("not enough buffer to write");
  328.         return -1;
  329.     }
  330.  
  331.     pthread_mutex_lock(&PcmManager->lock);
  332.  
  333.     if ((PcmManager->BufWritPtr + inSize)
  334.         > (PcmManager->BufStart + PcmManager->BufTotalLen))
  335.     {
  336.         int endSize = PcmManager->BufStart + PcmManager->BufTotalLen
  337.             - PcmManager->BufWritPtr;
  338.         memcpy(PcmManager->BufWritPtr, pInbuf, endSize);
  339.         memcpy(PcmManager->BufStart, (void *)((char *)pInbuf + endSize), inSize - endSize);
  340.  
  341.         PcmManager->BufWritPtr = PcmManager->BufWritPtr
  342.             + inSize - PcmManager->BufTotalLen;
  343.     }
  344.     else
  345.     {
  346.         memcpy(PcmManager->BufWritPtr, pInbuf, inSize);
  347.         PcmManager->BufWritPtr += inSize;
  348.     }
  349.  
  350.     PcmManager->BufValideLen -= inSize;
  351.     PcmManager->DataLen += inSize;
  352.  
  353.     ALOGV("after wr: BufTotalLen: %d, DataLen: %d, BufValideLen: %d, BufReadPtr: %p, BufWritPtr: %p",
  354.         PcmManager->BufTotalLen, PcmManager->DataLen, PcmManager->BufValideLen,
  355.         PcmManager->BufReadPtr, PcmManager->BufWritPtr);
  356.  
  357.     pthread_mutex_unlock(&PcmManager->lock);
  358.     ALOGV("RequestWriteBuf --");
  359.     return 0;
  360. }
  361.  
  362. static int ReadPcmData(void *pBuf, int uGetLen, struct pcm_buf_manager *PcmManager)
  363. {
  364.     int underflow = 0, fill_dc_size;
  365.     int size_read = uGetLen;
  366.     int timeout = 0, max_wait_count;
  367.  
  368.     max_wait_count = uGetLen * 100 / (PcmManager->SampleRate*PcmManager->Channel * 2) + 1; //normal
  369.     max_wait_count *= 2;//twice
  370.  
  371.     ALOGV("ReadPcmDataForEnc ++, getLen: %d max_wait_count=%d", uGetLen,max_wait_count);
  372.     while(PcmManager->DataLen < uGetLen)
  373.     {
  374.         ALOGV("pcm is not enough for audio encoder! uGetLen: %d, uDataLen: %d\n",
  375.             uGetLen, PcmManager->DataLen);
  376.         usleep(10*1000);
  377.         timeout++;
  378.         if(timeout > max_wait_count) {
  379.             if (PcmManager->DataLen < uGetLen) {
  380.                 underflow = 1;
  381.                 size_read = PcmManager->DataLen;
  382.                 fill_dc_size = uGetLen - PcmManager->DataLen;
  383.                 ALOGV("fill with dc size:%d",uGetLen - PcmManager->DataLen);
  384.             }
  385.             break;
  386.         }
  387.     }
  388.  
  389.     if((PcmManager->BufReadPtr + size_read)
  390.         > (PcmManager->BufStart + PcmManager->BufTotalLen))
  391.     {
  392.         int len1 = PcmManager->BufStart
  393.             + PcmManager->BufTotalLen - PcmManager->BufReadPtr;
  394.         memcpy((void *)pBuf, (void *)PcmManager->BufReadPtr, len1);
  395.         memcpy((void *)((char *)pBuf + len1), (void *)PcmManager->BufStart, size_read - len1);
  396.     }
  397.     else
  398.     {
  399.         memcpy(pBuf, PcmManager->BufReadPtr, size_read);
  400.     }
  401.  
  402.     pthread_mutex_lock(&PcmManager->lock);
  403.  
  404.     PcmManager->BufReadPtr += size_read;
  405.  
  406.     if(PcmManager->BufReadPtr
  407.         >= PcmManager->BufStart + PcmManager->BufTotalLen)
  408.     {
  409.         PcmManager->BufReadPtr -= PcmManager->BufTotalLen;
  410.     }
  411.     PcmManager->DataLen -= size_read;
  412.     PcmManager->BufValideLen += size_read;
  413.  
  414.     ALOGV("after rd: BufTotalLen: %d, DataLen: %d, BufValideLen: %d, pBufReadPtr: %p, pBufWritPtr: %p",
  415.         PcmManager->BufTotalLen, PcmManager->DataLen, PcmManager->BufValideLen,
  416.         PcmManager->BufReadPtr, PcmManager->BufWritPtr);
  417.    
  418.     pthread_mutex_unlock(&PcmManager->lock);
  419.     ALOGV("ReadPcmDataForEnc --");
  420.  
  421.     if (underflow) {
  422.         char *ptr = (char*)pBuf;
  423.         memset(ptr+size_read, ptr[size_read-1], fill_dc_size);
  424.     }
  425.  
  426.     return uGetLen;
  427. }
  428.  
  429. /**
  430.  * NOTE: when multiple mutexes have to be acquired, always respect the following order:
  431.  *        hw device > in stream > out stream
  432.  */
  433.  
  434. static void select_output_device(struct sunxi_audio_device *adev);
  435. static void select_input_device(struct sunxi_audio_device *adev);
  436. static int adev_set_voice_volume(struct audio_hw_device *dev, float volume);
  437. static int do_input_standby(struct sunxi_stream_in *in);
  438. static int do_output_standby(struct sunxi_stream_out *out);
  439.  
  440. /* The enable flag when 0 makes the assumption that enums are disabled by
  441.  * "Off" and integers/booleans by 0 */
  442. static int set_route_by_array(struct mixer *mixer, struct route_setting *route,
  443.                               int enable)
  444. {
  445.     struct mixer_ctl *ctl;
  446.     unsigned int i, j;
  447.  
  448.     /* Go through the route array and set each value */
  449.     i = 0;
  450.     while (route[i].ctl_name) {
  451.         ctl = mixer_get_ctl_by_name(mixer, route[i].ctl_name);
  452.         if (!ctl)
  453.             return -EINVAL;
  454.  
  455.         if (route[i].strval) {
  456.             if (enable)
  457.                 mixer_ctl_set_enum_by_string(ctl, route[i].strval);
  458.             else
  459.                 mixer_ctl_set_enum_by_string(ctl, "Off");
  460.         } else {
  461.             /* This ensures multiple (i.e. stereo) values are set jointly */
  462.             for (j = 0; j < mixer_ctl_get_num_values(ctl); j++) {
  463.                 if (enable)
  464.                     mixer_ctl_set_value(ctl, j, route[i].intval);
  465.                 else
  466.                     mixer_ctl_set_value(ctl, j, 0);
  467.             }
  468.         }
  469.         i++;
  470.     }
  471.  
  472.     return 0;
  473. }
  474.  
  475. static int start_call(struct sunxi_audio_device *adev)
  476. {
  477.     F_LOG;
  478.  
  479. //  set_route_by_array(adev->mixer, mic1_up_routing, 1);
  480. //  set_route_by_array(adev->mixer, line_in_routing, 1);
  481. //  mixer_ctl_set_value(adev->mixer_ctls.playback_pamute_switch, 0, 1);     // in call mode must switch pa unmute
  482.  
  483.     ril_set_call_volume(0, 1);
  484.  
  485. //  mixer_ctl_set_value(adev->mixer_ctls.master_playback_volume, 0, (call_volume ? call_volume : CALL_VOLUME_MIN));
  486.  
  487.     return 0;
  488. }
  489.  
  490. static void end_call(struct sunxi_audio_device *adev)
  491. {
  492.     F_LOG;
  493.    
  494. //  mixer_ctl_set_value(adev->mixer_ctls.playback_pamute_switch, 0, 0);
  495.     mixer_ctl_set_value(adev->mixer_ctls.audio_speaker_out, 0, 0);
  496.     usleep(5000);
  497. //  set_route_by_array(adev->mixer, mic1_up_routing, 0);
  498. //  set_route_by_array(adev->mixer, line_in_routing, 0);
  499.    
  500.     ril_set_call_audio_path(SOUND_AUDIO_PATH_SPEAKER);
  501.  
  502.     mixer_ctl_set_value(adev->mixer_ctls.master_playback_volume, 0, CALL_VOLUME_MAX);
  503. }
  504.  
  505. static void set_incall_device(struct sunxi_audio_device *adev)
  506. {
  507.     int device_type;
  508.  
  509.     F_LOG;
  510.  
  511.     switch(adev->devices & AUDIO_DEVICE_OUT_ALL) {
  512.         case AUDIO_DEVICE_OUT_EARPIECE:
  513.             device_type = SOUND_AUDIO_PATH_HANDSET;
  514.             break;
  515.         case AUDIO_DEVICE_OUT_SPEAKER:
  516.         case AUDIO_DEVICE_OUT_AUX_DIGITAL:
  517.             device_type = SOUND_AUDIO_PATH_SPEAKER;
  518.             break;
  519.         case AUDIO_DEVICE_OUT_WIRED_HEADSET:
  520.             device_type = SOUND_AUDIO_PATH_HEADSET;
  521.             break;
  522.         case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
  523.             device_type = SOUND_AUDIO_PATH_HEADPHONE;
  524.             break;
  525.         case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
  526.         case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
  527.         case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
  528.             if (adev->bluetooth_nrec)
  529.                 device_type = SOUND_AUDIO_PATH_BLUETOOTH;
  530.             else
  531.                 device_type = SOUND_AUDIO_PATH_BLUETOOTH_NO_NR;
  532.             break;
  533.         default:
  534.             device_type = SOUND_AUDIO_PATH_HANDSET;
  535.             break;
  536.     }
  537.  
  538.     ril_set_call_audio_path(device_type);
  539. }
  540.  
  541. static void force_all_standby(struct sunxi_audio_device *adev)
  542. {
  543.     struct sunxi_stream_in *in;
  544.     struct sunxi_stream_out *out;
  545.  
  546.     if (adev->active_output) {
  547.         out = adev->active_output;
  548.         pthread_mutex_lock(&out->lock);
  549.         do_output_standby(out);
  550.         pthread_mutex_unlock(&out->lock);
  551.     }
  552.  
  553.     if (adev->active_input) {
  554.         in = adev->active_input;
  555.         pthread_mutex_lock(&in->lock);
  556.         do_input_standby(in);
  557.         pthread_mutex_unlock(&in->lock);
  558.     }
  559. }
  560.  
  561. static void select_mode(struct sunxi_audio_device *adev)
  562. {
  563.     if (adev->mode == AUDIO_MODE_IN_CALL) {
  564.         ALOGV("Entering IN_CALL state, in_call=%d", adev->in_call);
  565.         if (!adev->in_call) {
  566.             force_all_standby(adev);
  567.             /* force earpiece route for in call state if speaker is the
  568.             only currently selected route. This prevents having to tear
  569.             down the modem PCMs to change route from speaker to earpiece
  570.             after the ringtone is played, but doesn't cause a route
  571.             change if a headset or bt device is already connected. If
  572.             speaker is not the only thing active, just remove it from
  573.             the route. We'll assume it'll never be used initally during
  574.             a call. This works because we're sure that the audio policy
  575.             manager will update the output device after the audio mode
  576.             change, even if the device selection did not change. */
  577.             if ((adev->devices & AUDIO_DEVICE_OUT_ALL) == AUDIO_DEVICE_OUT_SPEAKER)
  578.                 adev->devices = AUDIO_DEVICE_OUT_EARPIECE |
  579.                                 AUDIO_DEVICE_IN_BUILTIN_MIC;
  580.             else
  581.                 adev->devices &= ~AUDIO_DEVICE_OUT_SPEAKER;
  582.             select_output_device(adev);
  583.             start_call(adev);
  584.  
  585.             adev_set_voice_volume(&adev->hw_device, adev->voice_volume);
  586.             adev->in_call = 1;
  587.         }
  588.     } else {
  589.         ALOGV("Leaving IN_CALL state, in_call=%d, mode=%d",
  590.              adev->in_call, adev->mode);
  591.         if (adev->in_call) {
  592.             adev->in_call = 0;
  593.             end_call(adev);
  594.             force_all_standby(adev);
  595.             select_output_device(adev);
  596.             select_input_device(adev);
  597.         }
  598.     }
  599. }
  600.  
  601. static void select_output_device(struct sunxi_audio_device *adev)
  602. {
  603.     int ret = -1;
  604.     int headset_on;
  605.     int headphone_on;
  606.     int speaker_on;
  607.     int earpiece_on;
  608.     int bt_on;
  609.  
  610.     headset_on = adev->devices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
  611.     headphone_on = adev->devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
  612.     speaker_on = adev->devices & AUDIO_DEVICE_OUT_SPEAKER;
  613.     earpiece_on = adev->devices & AUDIO_DEVICE_OUT_EARPIECE;
  614.     bt_on = adev->devices & AUDIO_DEVICE_OUT_ALL_SCO;
  615.  
  616.     ALOGV("select_output_device, devices: %x, mode: %x", adev->devices, adev->mode);
  617.  
  618.     int pa_should_on = speaker_on;
  619.  
  620.     char prop_value[16];
  621.     ret = property_get("audio.without.earpiece", prop_value, "");
  622.     if (ret > 0)
  623.     {
  624.         ALOGD("get property audio.without.earpiece: %s", prop_value);
  625.         if (strcmp(prop_value, "true") == 0)
  626.         {
  627.             pa_should_on |= earpiece_on;
  628.         }
  629.     }
  630.  
  631.     // mute/unmute speaker
  632.     if (adev->mode == AUDIO_MODE_IN_CALL)
  633.     {      
  634.         if(pa_should_on)
  635.         {
  636.             //mixer_ctl_set_value(adev->mixer_ctls.audio_earpiece_out, 0, 0);
  637.             //mixer_ctl_set_value(adev->mixer_ctls.audio_headphone_out, 0, 0);
  638.             mixer_ctl_set_value(adev->mixer_ctls.audio_speaker_out, 0, 1);
  639.             ALOGV("****LINE:%d,FUNC:%s",__LINE__,__FUNCTION__);
  640.         }
  641.         else
  642.         {
  643.             if(earpiece_on)
  644.             {
  645.                 //mixer_ctl_set_value(adev->mixer_ctls.audio_earpiece_out, 0, 1);
  646.                 //mixer_ctl_set_value(adev->mixer_ctls.audio_headphone_out, 0, 0);
  647.                 mixer_ctl_set_value(adev->mixer_ctls.audio_speaker_out, 0, 0);
  648.                 ALOGV("****LINE:%d,FUNC:%s",__LINE__,__FUNCTION__);
  649.             }
  650.             else
  651.             {
  652.                 //mixer_ctl_set_value(adev->mixer_ctls.audio_earpiece_out, 0, 0);
  653.                 //mixer_ctl_set_value(adev->mixer_ctls.audio_headphone_out, 0, 1)
  654.                 mixer_ctl_set_value(adev->mixer_ctls.audio_speaker_out, 0, 0);
  655.                 ALOGV("****LINE:%d,FUNC:%s",__LINE__,__FUNCTION__);
  656.             }
  657.         }
  658.         set_incall_device(adev);
  659.     }
  660.     else
  661.     {
  662.         //mixer_ctl_set_value(adev->mixer_ctls.audio_spk_switch, 0, (pa_should_on ? 1 : 0));
  663.         if (pa_should_on)
  664.         {
  665.             mixer_ctl_set_value(adev->mixer_ctls.audio_speaker_out, 0, 1);
  666.             ALOGV("****LINE:%d,FUNC:%s",__LINE__,__FUNCTION__);
  667.         }
  668.         else
  669.         {
  670.             mixer_ctl_set_value(adev->mixer_ctls.audio_speaker_out, 0, 0);
  671.             ALOGV("****LINE:%d,FUNC:%s",__LINE__,__FUNCTION__);
  672.         }
  673.     }
  674. }
  675.  
  676. static void select_input_device(struct sunxi_audio_device *adev)
  677. {
  678.     int headset_on = 0;
  679.     int main_mic_on = 0;
  680.     int sub_mic_on = 0;
  681.     int bt_on = adev->devices & AUDIO_DEVICE_IN_ALL_SCO;
  682.  
  683.     if (!bt_on) {
  684.         if ((adev->mode != AUDIO_MODE_IN_CALL) && (adev->active_input != 0)) {
  685.             /* sub mic is used for camcorder or VoIP on speaker phone */
  686.             sub_mic_on = (adev->active_input->source == AUDIO_SOURCE_CAMCORDER) ||
  687.                          ((adev->devices & AUDIO_DEVICE_OUT_SPEAKER) &&
  688.                           (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION));
  689.         }
  690.         if (!sub_mic_on) {
  691.             headset_on = adev->devices & AUDIO_DEVICE_IN_WIRED_HEADSET;
  692.             main_mic_on = adev->devices & AUDIO_DEVICE_IN_BUILTIN_MIC;
  693.         }
  694.     }
  695. }
  696.  
  697. /* must be called with hw device and output stream mutexes locked */
  698. static int start_output_stream(struct sunxi_stream_out *out)
  699. {
  700.     F_LOG;
  701.     struct sunxi_audio_device *adev = out->dev;
  702.     unsigned int card = CARD_A1X_DEFAULT;
  703.     unsigned int port = PORT_CODEC;
  704.  
  705.     if (adev->mode == AUDIO_MODE_IN_CALL)
  706.     {
  707.         ALOGW("mode in call, do not start stream");
  708.         return 0;
  709.     }
  710.  
  711.     if (adev->raw_flag)
  712.     {
  713.         return 0;
  714.     }
  715.  
  716.     int device = adev->devices;
  717.     char prop_value[512];
  718.     int ret = property_get("audio.routing", prop_value, "");
  719.  
  720.     char prop_value2[16];
  721.     int ret2 = property_get("audio.hdmi_bypass", prop_value2, "");
  722.  
  723.     if (ret > 0)
  724.     {
  725.         if(atoi(prop_value) == AUDIO_DEVICE_OUT_SPEAKER)
  726.         {
  727.             ALOGD("start_output_stream, AUDIO_DEVICE_OUT_SPEAKER");
  728.             device = AUDIO_DEVICE_OUT_SPEAKER;
  729.         }
  730.         else if(atoi(prop_value) == AUDIO_DEVICE_OUT_AUX_DIGITAL)
  731.         {
  732.             if (atoi(prop_value2) == 0) {
  733.                 ALOGD("start_output_stream AUDIO_DEVICE_OUT_AUX_DIGITAL (HDMI)");
  734.                 device = AUDIO_DEVICE_OUT_AUX_DIGITAL;
  735.             }
  736.             else {
  737.  
  738.                 ALOGD("start_output_stream AUDIO_DEVICE_OUT_AUX_DIGITAL (HDMI Audio Bypass)");
  739.                 device = AUDIO_DEVICE_OUT_SPEAKER;
  740.             }
  741.         }
  742.         else if(atoi(prop_value) == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)
  743.         {
  744.             ALOGD("start_output_stream AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET");
  745.             device = AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
  746.         }
  747.         else
  748.         {
  749.             ALOGW("unknown audio.routing : %s", prop_value);
  750.         }
  751.     }
  752.     else
  753.     {
  754.         // ALOGW("get audio.routing failed");
  755.     }
  756.  
  757.     adev->devices = device;
  758.  
  759.     adev->active_output = out;
  760.  
  761.     if (adev->mode != AUDIO_MODE_IN_CALL) {
  762.         /* FIXME: only works if only one output can be active at a time */
  763.         select_output_device(adev);
  764.     }
  765.     /* S/PDIF takes priority over HDMI audio. In the case of multiple
  766.      * devices, this will cause use of S/PDIF or HDMI only */
  767.     out->config.rate = MM_SAMPLING_RATE;
  768.     if (adev->devices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) {
  769.         card = CARD_A1X_SPDIF;
  770.         port = PORT_SPDIF;
  771.     }
  772.     else if(adev->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
  773.         card = CARD_A1X_HDMI;
  774.         port = PORT_HDMI;
  775.         out->config.rate = MM_SAMPLING_RATE;
  776.     }
  777.     /* default to low power: will be corrected in out_write if necessary before first write to
  778.      * tinyalsa.
  779.      */
  780.     out->write_threshold = PLAYBACK_PERIOD_COUNT * LONG_PERIOD_SIZE;
  781.     out->config.start_threshold = SHORT_PERIOD_SIZE * 2;
  782.     out->config.avail_min = LONG_PERIOD_SIZE;
  783.  
  784.     ALOGD("start_output_stream: card:%d, port:%d, rate:%d", card, port, out->config.rate);
  785.  
  786.     out->pcm = pcm_open_req(card, port, PCM_OUT | PCM_MMAP | PCM_NOIRQ, &out->config, DEFAULT_OUT_SAMPLING_RATE);
  787.  
  788.     if (!pcm_is_ready(out->pcm)) {
  789.         ALOGE("cannot open pcm_out driver: %s", pcm_get_error(out->pcm));
  790.         pcm_close(out->pcm);
  791.         adev->active_output = NULL;
  792.         return -ENOMEM;
  793.     }
  794.  
  795.     if (adev->echo_reference != NULL)
  796.         out->echo_reference = adev->echo_reference;
  797.  
  798.     if (DEFAULT_OUT_SAMPLING_RATE != out->config.rate)
  799.     {
  800.         ret = create_resampler(DEFAULT_OUT_SAMPLING_RATE,
  801.                                out->config.rate,
  802.                                2,
  803.                                RESAMPLER_QUALITY_DEFAULT,
  804.                                NULL,
  805.                                &out->resampler);
  806.         if (ret != 0)
  807.         {
  808.             ALOGE("create out resampler failed, %d -> %d", DEFAULT_OUT_SAMPLING_RATE, out->config.rate);
  809.             return ret;
  810.         }
  811.  
  812.         ALOGV("create out resampler OK, %d -> %d", DEFAULT_OUT_SAMPLING_RATE, out->config.rate);
  813.     }
  814.     else
  815.     {
  816.         ALOGV("do not use out resampler");
  817.     }
  818.  
  819.     if (out->resampler)
  820.     {
  821.         out->resampler->reset(out->resampler);
  822.     }
  823.  
  824.     return 0;
  825. }
  826.  
  827. static int check_input_parameters(uint32_t sample_rate, int format, int channel_count)
  828. {
  829.     if (format != AUDIO_FORMAT_PCM_16_BIT)
  830.         return -EINVAL;
  831.  
  832.     if ((channel_count < 1) || (channel_count > 2))
  833.         return -EINVAL;
  834.  
  835.     switch(sample_rate) {
  836.     case 8000:
  837.     case 11025:
  838.     case 16000:
  839.     case 22050:
  840.     case 24000:
  841.     case 32000:
  842.     case 44100:
  843.     case 48000:
  844.         break;
  845.     default:
  846.         return -EINVAL;
  847.     }
  848.  
  849.     return 0;
  850. }
  851.  
  852. static size_t get_input_buffer_size(uint32_t sample_rate, int format, int channel_count)
  853. {
  854.     size_t size;
  855.     size_t device_rate;
  856.  
  857.     if (check_input_parameters(sample_rate, format, channel_count) != 0)
  858.         return 0;
  859.  
  860.     /* take resampling into account and return the closest majoring
  861.     multiple of 16 frames, as audioflinger expects audio buffers to
  862.     be a multiple of 16 frames */
  863.     size = (pcm_config_mm_in.period_size * sample_rate) / pcm_config_mm_in.rate;
  864.     size = ((size + 15) / 16) * 16;
  865.  
  866.     return size * channel_count * sizeof(short);
  867. }
  868.  
  869. static void add_echo_reference(struct sunxi_stream_out *out,
  870.                                struct echo_reference_itfe *reference)
  871. {
  872.     pthread_mutex_lock(&out->lock);
  873.     out->echo_reference = reference;
  874.     pthread_mutex_unlock(&out->lock);
  875. }
  876.  
  877. static void remove_echo_reference(struct sunxi_stream_out *out,
  878.                                   struct echo_reference_itfe *reference)
  879. {
  880.     pthread_mutex_lock(&out->lock);
  881.     if (out->echo_reference == reference) {
  882.         /* stop writing to echo reference */
  883.         reference->write(reference, NULL);
  884.         out->echo_reference = NULL;
  885.     }
  886.     pthread_mutex_unlock(&out->lock);
  887. }
  888.  
  889. static void put_echo_reference(struct sunxi_audio_device *adev,
  890.                           struct echo_reference_itfe *reference)
  891. {
  892.     if (adev->echo_reference != NULL &&
  893.             reference == adev->echo_reference) {
  894.         if (adev->active_output != NULL)
  895.             remove_echo_reference(adev->active_output, reference);
  896.         release_echo_reference(reference);
  897.         adev->echo_reference = NULL;
  898.     }
  899. }
  900.  
  901. static struct echo_reference_itfe *get_echo_reference(struct sunxi_audio_device *adev,
  902.                                                audio_format_t format,
  903.                                                uint32_t channel_count,
  904.                                                uint32_t sampling_rate)
  905. {
  906.     put_echo_reference(adev, adev->echo_reference);
  907.     if (adev->active_output != NULL) {
  908.         struct audio_stream *stream = &adev->active_output->stream.common;
  909.         uint32_t wr_channel_count = popcount(stream->get_channels(stream));
  910.         uint32_t wr_sampling_rate = stream->get_sample_rate(stream);
  911.  
  912.         int status = create_echo_reference(AUDIO_FORMAT_PCM_16_BIT,
  913.                                            channel_count,
  914.                                            sampling_rate,
  915.                                            AUDIO_FORMAT_PCM_16_BIT,
  916.                                            wr_channel_count,
  917.                                            wr_sampling_rate,
  918.                                            &adev->echo_reference);
  919.         if (status == 0)
  920.             add_echo_reference(adev->active_output, adev->echo_reference);
  921.     }
  922.     return adev->echo_reference;
  923. }
  924.  
  925. static int get_playback_delay(struct sunxi_stream_out *out,
  926.                        size_t frames,
  927.                        struct echo_reference_buffer *buffer)
  928. {
  929.     size_t kernel_frames;
  930.     int status;
  931.  
  932.     status = pcm_get_htimestamp(out->pcm, &kernel_frames, &buffer->time_stamp);
  933.     if (status < 0) {
  934.         buffer->time_stamp.tv_sec  = 0;
  935.         buffer->time_stamp.tv_nsec = 0;
  936.         buffer->delay_ns           = 0;
  937.         ALOGV("get_playback_delay(): pcm_get_htimestamp error,"
  938.                 "setting playbackTimestamp to 0");
  939.         return status;
  940.     }
  941.  
  942.     kernel_frames = pcm_get_buffer_size(out->pcm) - kernel_frames;
  943.  
  944.     /* adjust render time stamp with delay added by current driver buffer.
  945.      * Add the duration of current frame as we want the render time of the last
  946.      * sample being written. */
  947.     buffer->delay_ns = (long)(((int64_t)(kernel_frames + frames)* 1000000000)/
  948.                             MM_SAMPLING_RATE);
  949.  
  950.     return 0;
  951. }
  952.  
  953. static uint32_t out_get_sample_rate(const struct audio_stream *stream)
  954. {
  955.     return DEFAULT_OUT_SAMPLING_RATE;
  956. }
  957.  
  958. static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
  959. {
  960.     return 0;
  961. }
  962.  
  963. static size_t out_get_buffer_size(const struct audio_stream *stream)
  964. {
  965.     struct sunxi_stream_out *out = (struct sunxi_stream_out *)stream;
  966.  
  967.     /* take resampling into account and return the closest majoring
  968.     multiple of 16 frames, as audioflinger expects audio buffers to
  969.     be a multiple of 16 frames */
  970.     size_t size = (SHORT_PERIOD_SIZE * DEFAULT_OUT_SAMPLING_RATE) / out->config.rate;
  971.     size = ((size + 15) / 16) * 16;
  972.     return size * audio_stream_frame_size((struct audio_stream *)stream);
  973. }
  974.  
  975. static uint32_t out_get_channels(const struct audio_stream *stream)
  976. {
  977.     return AUDIO_CHANNEL_OUT_STEREO;
  978. }
  979.  
  980. static audio_format_t out_get_format(const struct audio_stream *stream)
  981. {
  982.     return AUDIO_FORMAT_PCM_16_BIT;
  983. }
  984.  
  985. static int out_set_format(struct audio_stream *stream, audio_format_t format)
  986. {
  987.     return 0;
  988. }
  989.  
  990. /* must be called with hw device and output stream mutexes locked */
  991. static int do_output_standby(struct sunxi_stream_out *out)
  992. {
  993.     struct sunxi_audio_device *adev = out->dev;
  994.  
  995.     if (!out->standby) {
  996.         pcm_close(out->pcm);
  997.         out->pcm = NULL;
  998.  
  999.         adev->active_output = 0;
  1000.  
  1001.         /* stop writing to echo reference */
  1002.         if (out->echo_reference != NULL) {
  1003.             out->echo_reference->write(out->echo_reference, NULL);
  1004.             out->echo_reference = NULL;
  1005.         }
  1006.  
  1007.         out->standby = 1;
  1008.     }
  1009.     return 0;
  1010. }
  1011.  
  1012. static int out_standby(struct audio_stream *stream)
  1013. {
  1014.     struct sunxi_stream_out *out = (struct sunxi_stream_out *)stream;
  1015.     int status;
  1016.  
  1017.     pthread_mutex_lock(&out->dev->lock);
  1018.     pthread_mutex_lock(&out->lock);
  1019.     status = do_output_standby(out);
  1020.     pthread_mutex_unlock(&out->lock);
  1021.     pthread_mutex_unlock(&out->dev->lock);
  1022.     return status;
  1023. }
  1024.  
  1025. static int out_dump(const struct audio_stream *stream, int fd)
  1026. {
  1027.     return 0;
  1028. }
  1029.  
  1030. static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
  1031. {
  1032.     struct sunxi_stream_out *out = (struct sunxi_stream_out *)stream;
  1033.     struct sunxi_audio_device *adev = out->dev;
  1034.     struct sunxi_stream_in *in;
  1035.     struct str_parms *parms;
  1036.     char *str;
  1037.     char value[32];
  1038.     int ret, val = 0;
  1039.     bool force_input_standby = false;
  1040.  
  1041.     parms = str_parms_create_str(kvpairs);
  1042.  
  1043.     ALOGV("out_set_parameters: %s", kvpairs);
  1044.  
  1045.     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
  1046.     if (ret >= 0) {
  1047.         val = atoi(value);
  1048.         pthread_mutex_lock(&adev->lock);
  1049.         pthread_mutex_lock(&out->lock);
  1050.         if (((adev->devices & AUDIO_DEVICE_OUT_ALL) != val) && (val != 0)) {
  1051.             if (out == adev->active_output) {
  1052.                 /* a change in output device may change the microphone selection */
  1053.                 if (adev->active_input &&
  1054.                         adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
  1055.                     force_input_standby = true;
  1056.                 }
  1057.                 /* force standby if moving to/from HDMI */
  1058.                 if (((val & AUDIO_DEVICE_OUT_AUX_DIGITAL) ^
  1059.                         (adev->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) ||
  1060.                         ((val & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) ^
  1061.                         (adev->devices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)))
  1062.                     do_output_standby(out);
  1063.             }
  1064.             adev->devices &= ~AUDIO_DEVICE_OUT_ALL;
  1065.             adev->devices |= val;
  1066.             select_output_device(adev);
  1067.         }
  1068.         pthread_mutex_unlock(&out->lock);
  1069.         if (force_input_standby) {
  1070.             in = adev->active_input;
  1071.             pthread_mutex_lock(&in->lock);
  1072.             do_input_standby(in);
  1073.             pthread_mutex_unlock(&in->lock);
  1074.         }
  1075.         pthread_mutex_unlock(&adev->lock);
  1076.     }
  1077.  
  1078.     ret = str_parms_get_str(parms, AUDIO_PARAMETER_RAW_DATA_OUT, value, sizeof(value));
  1079.     if (ret >= 0)
  1080.     {
  1081.         bool bval = (atoi(value) == 1) ? true : false;
  1082.         ALOGD("AUDIO_PARAMETER_RAW_DATA_OUT: %d", bval);
  1083.         pthread_mutex_lock(&adev->lock);
  1084.         pthread_mutex_lock(&out->lock);
  1085.  
  1086.         if (adev->raw_flag != bval)
  1087.         {
  1088.             adev->raw_flag = bval;
  1089.             do_output_standby(out);
  1090.         }
  1091.  
  1092.         pthread_mutex_unlock(&out->lock);
  1093.         pthread_mutex_unlock(&adev->lock);
  1094.     }
  1095.  
  1096.     str_parms_destroy(parms);
  1097.     return ret;
  1098. }
  1099.  
  1100. static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
  1101. {
  1102.     return strdup("");
  1103. }
  1104.  
  1105. static uint32_t out_get_latency(const struct audio_stream_out *stream)
  1106. {
  1107.     struct sunxi_stream_out *out = (struct sunxi_stream_out *)stream;
  1108.  
  1109.     return (SHORT_PERIOD_SIZE * PLAYBACK_PERIOD_COUNT * 1000) / out->config.rate;
  1110. }
  1111.  
  1112. static int out_set_volume(struct audio_stream_out *stream, float left,
  1113.                           float right)
  1114. {
  1115.     return -ENOSYS;
  1116. }
  1117.  
  1118. #if 0
  1119. static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
  1120.                          size_t bytes)
  1121. {
  1122.     struct sunxi_stream_out *out = (struct sunxi_stream_out *)stream;
  1123.     struct sunxi_audio_device *adev = out->dev;
  1124.     size_t frame_size = audio_stream_frame_size(&out->stream.common);
  1125.     size_t in_frames = bytes / frame_size;
  1126.     size_t out_frames = RESAMPLER_BUFFER_SIZE / frame_size;
  1127.     unsigned int card = CARD_A1X_DEFAULT;
  1128.     unsigned int port = PORT_CODEC;
  1129.      void *buf;
  1130.     int ret;
  1131.  
  1132.     if (adev->mode == AUDIO_MODE_IN_CALL)
  1133.     {
  1134.         //ALOGW("mode in call, do not out_write");
  1135.         return 0;
  1136.     }
  1137.  
  1138.  
  1139.     pthread_mutex_lock(&adev->lock);
  1140.     pthread_mutex_lock(&out->lock);
  1141.     if (out->standby)
  1142.     {
  1143.         out->config.channels = 2;//cda_dec->pDev.AudioBsInfo.chan;
  1144.         out->config.rate = 44100;//cda_dec->pDev.AudioBsInfo.Samplerate;
  1145.  
  1146.         out->config.period_size = 1024;
  1147.         out->config.period_count = 4;
  1148.         out->config.format = PCM_FORMAT_S16_LE;
  1149.         out->config.start_threshold = 0;
  1150.         out->config.stop_threshold = 0;
  1151.         out->config.silence_threshold = 0;
  1152.         ALOGV("***fs:%d,ch:%d",out->config.rate,out->config.channels);
  1153.         out->pcm = pcm_open(card, port, PCM_OUT, &(out->config));
  1154.          if (!out->pcm || !pcm_is_ready(out->pcm)) {
  1155.             ALOGW("Unable to open PCM device %u (%s)\n",
  1156.                     port, pcm_get_error(out->pcm));
  1157.             pthread_mutex_unlock(&adev->lock);
  1158.             goto exit;
  1159.         }
  1160. //       ret = start_output_stream(out);
  1161. //        if (ret != 0) {
  1162. //          goto exit;
  1163. //        }
  1164.  
  1165.         out->standby = 0;
  1166.  
  1167.  
  1168.     }
  1169.     pthread_mutex_unlock(&adev->lock);
  1170.     out_frames = in_frames;
  1171.      buf = (void *)buffer;
  1172.     //ALOGV("***line:%d,LEN:%d",__LINE__,out_frames * frame_size);
  1173.  
  1174.     ret = pcm_write(out->pcm, (void *)buf, out_frames * frame_size);
  1175.  
  1176.     exit:
  1177.  
  1178.         pthread_mutex_unlock(&out->lock);
  1179.         return bytes;
  1180.  
  1181. }
  1182.  
  1183. #else
  1184.  
  1185. static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
  1186.                          size_t bytes)
  1187. {
  1188.     int ret;
  1189.     struct sunxi_stream_out *out = (struct sunxi_stream_out *)stream;
  1190.     struct sunxi_audio_device *adev = out->dev;
  1191.     size_t frame_size = audio_stream_frame_size(&out->stream.common);
  1192.     size_t in_frames = bytes / frame_size;
  1193.     size_t out_frames = RESAMPLER_BUFFER_SIZE / frame_size;
  1194.     bool force_input_standby = false;
  1195.     struct sunxi_stream_in *in;
  1196.     int kernel_frames;
  1197.     void *buf;
  1198.    
  1199.     if (adev->mode == AUDIO_MODE_IN_CALL)
  1200.     {
  1201.         // ALOGW("mode in call, do not out_write");
  1202.         return 0;
  1203.     }
  1204.  
  1205.     if (adev->raw_flag)
  1206.     {
  1207.         return 0;
  1208.     }
  1209.  
  1210.     /* acquiring hw device mutex systematically is useful if a low priority thread is waiting
  1211.      * on the output stream mutex - e.g. executing select_mode() while holding the hw device
  1212.      * mutex
  1213.      */
  1214.     pthread_mutex_lock(&adev->lock);
  1215.     pthread_mutex_lock(&out->lock);
  1216.     if (out->standby) {
  1217.         ret = start_output_stream(out);
  1218.         if (ret != 0) {
  1219.             pthread_mutex_unlock(&adev->lock);
  1220.             goto exit;
  1221.         }
  1222.         out->standby = 0;
  1223.         /* a change in output device may change the microphone selection */
  1224.         if (adev->active_input &&
  1225.                 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION)
  1226.             force_input_standby = true;
  1227.     }
  1228.     pthread_mutex_unlock(&adev->lock);
  1229.  
  1230.     out->write_threshold = SHORT_PERIOD_SIZE * PLAYBACK_PERIOD_COUNT;
  1231.     out->config.avail_min = SHORT_PERIOD_SIZE;
  1232.     pcm_set_avail_min(out->pcm, out->config.avail_min);
  1233.  
  1234.     /* only use resampler if required */
  1235.     if (out->resampler) {
  1236.         out->resampler->resample_from_input(out->resampler,
  1237.                                             (int16_t *)buffer,
  1238.                                             &in_frames,
  1239.                                             (int16_t *)out->buffer,
  1240.                                             &out_frames);
  1241.         buf = out->buffer;
  1242.     } else {
  1243.         out_frames = in_frames;
  1244.         buf = (void *)buffer;
  1245.     }
  1246.     if (out->echo_reference != NULL) {
  1247.         struct echo_reference_buffer b;
  1248.         b.raw = (void *)buffer;
  1249.         b.frame_count = in_frames;
  1250.  
  1251.         get_playback_delay(out, out_frames, &b);
  1252.         out->echo_reference->write(out->echo_reference, &b);
  1253.     }
  1254.  
  1255.     /* do not allow more than out->write_threshold frames in kernel pcm driver buffer */
  1256.     do {
  1257.         struct timespec time_stamp;
  1258.  
  1259.         if (pcm_get_htimestamp(out->pcm, (unsigned int *)&kernel_frames, &time_stamp) < 0)
  1260.             break;
  1261.         kernel_frames = pcm_get_buffer_size(out->pcm) - kernel_frames;
  1262.  
  1263.         if (kernel_frames > out->write_threshold) {
  1264.             unsigned long time = (unsigned long)
  1265.                     (((int64_t)(kernel_frames - out->write_threshold) * 1000000) /
  1266.                             MM_SAMPLING_RATE);
  1267.             if (time < MIN_WRITE_SLEEP_US)
  1268.                 time = MIN_WRITE_SLEEP_US;
  1269.             usleep(time);
  1270.         }
  1271.     } while (kernel_frames > out->write_threshold);
  1272.  
  1273.     if (adev->af_capture_flag && adev->PcmManager.BufExist) {
  1274.         WritePcmData((void *)buf, out_frames * frame_size, &adev->PcmManager);
  1275.         memset(buf, 0, out_frames * frame_size); //mute
  1276.     }
  1277.  
  1278.     ret = pcm_mmap_write(out->pcm, (void *)buf, out_frames * frame_size);
  1279.     if(ret!=0)
  1280.     {
  1281.         do_output_standby(out);
  1282.     }
  1283.  
  1284. exit:
  1285.     pthread_mutex_unlock(&out->lock);
  1286.  
  1287.     if (ret != 0) {
  1288.         usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) /
  1289.                out_get_sample_rate(&stream->common));
  1290.     }
  1291.  
  1292.     if (force_input_standby) {
  1293.         pthread_mutex_lock(&adev->lock);
  1294.         if (adev->active_input) {
  1295.             in = adev->active_input;
  1296.             pthread_mutex_lock(&in->lock);
  1297.             do_input_standby(in);
  1298.             pthread_mutex_unlock(&in->lock);
  1299.         }
  1300.         pthread_mutex_unlock(&adev->lock);
  1301.     }
  1302.  
  1303.     return bytes;
  1304. }
  1305. #endif
  1306.  
  1307. static int out_get_render_position(const struct audio_stream_out *stream,
  1308.                                    uint32_t *dsp_frames)
  1309. {
  1310.     return -EINVAL;
  1311. }
  1312.  
  1313. static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
  1314. {
  1315.     return 0;
  1316. }
  1317.  
  1318. static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
  1319. {
  1320.     return 0;
  1321. }
  1322.  
  1323. static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
  1324.                                         int64_t *timestamp)
  1325. {
  1326.     return -EINVAL;
  1327. }
  1328.  
  1329. /** audio_stream_in implementation **/
  1330.  
  1331. static int get_next_buffer(struct resampler_buffer_provider *buffer_provider,
  1332.                                    struct resampler_buffer* buffer);
  1333. static void release_buffer(struct resampler_buffer_provider *buffer_provider,
  1334.                                   struct resampler_buffer* buffer);
  1335.  
  1336. /* must be called with hw device and input stream mutexes locked */
  1337. static int start_input_stream(struct sunxi_stream_in *in)
  1338. {
  1339.     F_LOG;
  1340.     int ret = 0;
  1341.     struct sunxi_audio_device *adev = in->dev;
  1342.  
  1343.     adev->active_input = in;
  1344.  
  1345.     if (adev->mode != AUDIO_MODE_IN_CALL) {
  1346.         adev->devices &= ~AUDIO_DEVICE_IN_ALL;
  1347.         adev->devices |= in->device;
  1348.         select_input_device(adev);
  1349.     }
  1350.  
  1351.     if (in->need_echo_reference && in->echo_reference == NULL)
  1352.         in->echo_reference = get_echo_reference(adev,
  1353.                                         AUDIO_FORMAT_PCM_16_BIT,
  1354.                                         in->config.channels,
  1355.                                         in->requested_rate);
  1356.  
  1357.     int in_ajust_rate = in->requested_rate;
  1358.     // out/in stream should be both 44.1K serial
  1359.     if (!(in->requested_rate % SAMPLING_RATE_11K))
  1360.     {
  1361.         // OK
  1362.         in_ajust_rate = in->requested_rate;
  1363.     }
  1364.     else
  1365.     {
  1366.         in_ajust_rate = SAMPLING_RATE_11K * in->requested_rate / SAMPLING_RATE_8K;
  1367.         if (in_ajust_rate > SAMPLING_RATE_44K)
  1368.         {
  1369.             in_ajust_rate = SAMPLING_RATE_44K;
  1370.         }
  1371.         ALOGV("out/in stream should be both 44.1K serial, force capture rate: %d", in_ajust_rate);
  1372.     }
  1373.  
  1374.     in->pcm = pcm_open_req(0, PORT_CODEC, PCM_IN, &in->config, in_ajust_rate);
  1375.  
  1376.     if (!pcm_is_ready(in->pcm)) {
  1377.         ALOGE("cannot open pcm_in driver: %s", pcm_get_error(in->pcm));
  1378.         pcm_close(in->pcm);
  1379.         adev->active_input = NULL;
  1380.         return -ENOMEM;
  1381.     }
  1382.  
  1383.     //
  1384.     //set_route_by_array(adev->mixer, mic1_rec_routing, 1);
  1385.  
  1386.     if (adev->mode == AUDIO_MODE_IN_CALL)
  1387.     {
  1388.         //set_route_by_array(adev->mixer, line_in_rec_routing, 1);  // must after mic1_rec_routing
  1389.     }
  1390.  
  1391.     if (in->requested_rate != in->config.rate) {
  1392.         in->buf_provider.get_next_buffer = get_next_buffer;
  1393.         in->buf_provider.release_buffer = release_buffer;
  1394.  
  1395.         ret = create_resampler(in->config.rate,
  1396.                                in->requested_rate,
  1397.                                in->config.channels,
  1398.                                RESAMPLER_QUALITY_DEFAULT,
  1399.                                &in->buf_provider,
  1400.                                &in->resampler);
  1401.         if (ret != 0) {
  1402.             ALOGE("create in resampler failed, %d -> %d", in->config.rate, in->requested_rate);
  1403.             ret = -EINVAL;
  1404.             goto err;
  1405.         }
  1406.  
  1407.         ALOGV("create in resampler OK, %d -> %d", in->config.rate, in->requested_rate);
  1408.     }
  1409.     else
  1410.     {
  1411.         ALOGV("do not use in resampler");
  1412.     }
  1413.  
  1414.     /* if no supported sample rate is available, use the resampler */
  1415.     if (in->resampler) {
  1416.         in->resampler->reset(in->resampler);
  1417.         in->frames_in = 0;
  1418.     }
  1419.     return 0;
  1420.  
  1421. err:
  1422.     if (in->resampler) {
  1423.         release_resampler(in->resampler);
  1424.     }
  1425.  
  1426.     return -1;
  1427. }
  1428.  
  1429. static uint32_t in_get_sample_rate(const struct audio_stream *stream)
  1430. {
  1431.     struct sunxi_stream_in *in = (struct sunxi_stream_in *)stream;
  1432.  
  1433.     return in->requested_rate;
  1434. }
  1435.  
  1436. static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
  1437. {
  1438.     return 0;
  1439. }
  1440.  
  1441. static size_t in_get_buffer_size(const struct audio_stream *stream)
  1442. {
  1443.     struct sunxi_stream_in *in = (struct sunxi_stream_in *)stream;
  1444.  
  1445.     return get_input_buffer_size(in->requested_rate,
  1446.                                  AUDIO_FORMAT_PCM_16_BIT,
  1447.                                  in->config.channels);
  1448. }
  1449.  
  1450. static uint32_t in_get_channels(const struct audio_stream *stream)
  1451. {
  1452.     struct sunxi_stream_in *in = (struct sunxi_stream_in *)stream;
  1453.  
  1454.     if (in->config.channels == 1) {
  1455.         return AUDIO_CHANNEL_IN_MONO;
  1456.     } else {
  1457.         return AUDIO_CHANNEL_IN_STEREO;
  1458.     }
  1459. }
  1460.  
  1461. static audio_format_t in_get_format(const struct audio_stream *stream)
  1462. {
  1463.     return AUDIO_FORMAT_PCM_16_BIT;
  1464. }
  1465.  
  1466. static int in_set_format(struct audio_stream *stream, audio_format_t format)
  1467. {
  1468.     return 0;
  1469. }
  1470.  
  1471. /* must be called with hw device and input stream mutexes locked */
  1472. static int do_input_standby(struct sunxi_stream_in *in)
  1473. {
  1474.     struct sunxi_audio_device *adev = in->dev;
  1475.  
  1476.     if (!in->standby) {
  1477.         pcm_close(in->pcm);
  1478.         in->pcm = NULL;
  1479.  
  1480.         adev->active_input = 0;
  1481.         if (adev->mode != AUDIO_MODE_IN_CALL) {
  1482.             adev->devices &= ~AUDIO_DEVICE_IN_ALL;
  1483.             select_input_device(adev);
  1484.         }
  1485.  
  1486.         if (in->echo_reference != NULL) {
  1487.             /* stop reading from echo reference */
  1488.             in->echo_reference->read(in->echo_reference, NULL);
  1489.             put_echo_reference(adev, in->echo_reference);
  1490.             in->echo_reference = NULL;
  1491.         }
  1492.  
  1493.         in->standby = 1;
  1494.  
  1495.         //
  1496.         // set_route_by_array(adev->mixer, line_in_rec_routing, 0);
  1497. //      set_route_by_array(adev->mixer, mic1_rec_routing, 0);
  1498.     }
  1499.     return 0;
  1500. }
  1501.  
  1502. static int in_standby(struct audio_stream *stream)
  1503. {
  1504.     struct sunxi_stream_in *in = (struct sunxi_stream_in *)stream;
  1505.     int status;
  1506.  
  1507.     pthread_mutex_lock(&in->dev->lock);
  1508.     pthread_mutex_lock(&in->lock);
  1509.     status = do_input_standby(in);
  1510.     pthread_mutex_unlock(&in->lock);
  1511.     pthread_mutex_unlock(&in->dev->lock);
  1512.     return status;
  1513. }
  1514.  
  1515. static int in_dump(const struct audio_stream *stream, int fd)
  1516. {
  1517.     return 0;
  1518. }
  1519.  
  1520. static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
  1521. {
  1522.     struct sunxi_stream_in *in = (struct sunxi_stream_in *)stream;
  1523.     struct sunxi_audio_device *adev = in->dev;
  1524.     struct str_parms *parms;
  1525.     char *str;
  1526.     char value[128];
  1527.     int ret, val = 0;
  1528.     bool do_standby = false;
  1529.  
  1530.     ALOGV("in_set_parameters: %s", kvpairs);
  1531.  
  1532.     parms = str_parms_create_str(kvpairs);
  1533.  
  1534.     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
  1535.  
  1536.     pthread_mutex_lock(&adev->lock);
  1537.     pthread_mutex_lock(&in->lock);
  1538.     if (ret >= 0) {
  1539.         val = atoi(value);
  1540.         /* no audio source uses val == 0 */
  1541.         if ((in->source != val) && (val != 0)) {
  1542.             in->source = val;
  1543.             do_standby = true;
  1544.         }
  1545.     }
  1546.  
  1547.     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
  1548.     if (ret >= 0) {
  1549.         val = atoi(value);
  1550.         if ((in->device != val) && (val != 0)) {
  1551.             in->device = val;
  1552.             do_standby = true;
  1553.         }
  1554.     }
  1555.  
  1556.     if (do_standby)
  1557.         do_input_standby(in);
  1558.     pthread_mutex_unlock(&in->lock);
  1559.     pthread_mutex_unlock(&adev->lock);
  1560.  
  1561.     str_parms_destroy(parms);
  1562.     return ret;
  1563. }
  1564.  
  1565. static char * in_get_parameters(const struct audio_stream *stream,
  1566.                                 const char *keys)
  1567. {
  1568.     return strdup("");
  1569. }
  1570.  
  1571. static int in_set_gain(struct audio_stream_in *stream, float gain)
  1572. {
  1573.     return 0;
  1574. }
  1575.  
  1576. static void get_capture_delay(struct sunxi_stream_in *in,
  1577.                        size_t frames,
  1578.                        struct echo_reference_buffer *buffer)
  1579. {
  1580.  
  1581.     /* read frames available in kernel driver buffer */
  1582.     size_t kernel_frames;
  1583.     struct timespec tstamp;
  1584.     long buf_delay;
  1585.     long rsmp_delay;
  1586.     long kernel_delay;
  1587.     long delay_ns;
  1588.  
  1589.     if (pcm_get_htimestamp(in->pcm, &kernel_frames, &tstamp) < 0) {
  1590.         buffer->time_stamp.tv_sec  = 0;
  1591.         buffer->time_stamp.tv_nsec = 0;
  1592.         buffer->delay_ns           = 0;
  1593.         ALOGW("read get_capture_delay(): pcm_htimestamp error");
  1594.         return;
  1595.     }
  1596.  
  1597.     /* read frames available in audio HAL input buffer
  1598.      * add number of frames being read as we want the capture time of first sample
  1599.      * in current buffer */
  1600.     buf_delay = (long)(((int64_t)(in->frames_in + in->proc_frames_in) * 1000000000)
  1601.                                     / in->config.rate);
  1602.     /* add delay introduced by resampler */
  1603.     rsmp_delay = 0;
  1604.     if (in->resampler) {
  1605.         rsmp_delay = in->resampler->delay_ns(in->resampler);
  1606.     }
  1607.  
  1608.     kernel_delay = (long)(((int64_t)kernel_frames * 1000000000) / in->config.rate);
  1609.  
  1610.     delay_ns = kernel_delay + buf_delay + rsmp_delay;
  1611.  
  1612.     buffer->time_stamp = tstamp;
  1613.     buffer->delay_ns   = delay_ns;
  1614.     ALOGV("get_capture_delay time_stamp = [%ld].[%ld], delay_ns: [%d],"
  1615.          " kernel_delay:[%ld], buf_delay:[%ld], rsmp_delay:[%ld], kernel_frames:[%d], "
  1616.          "in->frames_in:[%d], in->proc_frames_in:[%d], frames:[%d]",
  1617.          buffer->time_stamp.tv_sec , buffer->time_stamp.tv_nsec, buffer->delay_ns,
  1618.          kernel_delay, buf_delay, rsmp_delay, kernel_frames,
  1619.          in->frames_in, in->proc_frames_in, frames);
  1620. }
  1621.  
  1622. static int32_t update_echo_reference(struct sunxi_stream_in *in, size_t frames)
  1623. {
  1624.     struct echo_reference_buffer b;
  1625.     b.delay_ns = 0;
  1626.  
  1627.     ALOGV("update_echo_reference, frames = [%d], in->ref_frames_in = [%d],  "
  1628.           "b.frame_count = [%d]",
  1629.          frames, in->ref_frames_in, frames - in->ref_frames_in);
  1630.     if (in->ref_frames_in < frames) {
  1631.         if (in->ref_buf_size < frames) {
  1632.             in->ref_buf_size = frames;
  1633.             in->ref_buf = (int16_t *)realloc(in->ref_buf,
  1634.                                              in->ref_buf_size *
  1635.                                                  in->config.channels * sizeof(int16_t));
  1636.         }
  1637.  
  1638.         b.frame_count = frames - in->ref_frames_in;
  1639.         b.raw = (void *)(in->ref_buf + in->ref_frames_in * in->config.channels);
  1640.  
  1641.         get_capture_delay(in, frames, &b);
  1642.  
  1643.         if (in->echo_reference->read(in->echo_reference, &b) == 0)
  1644.         {
  1645.             in->ref_frames_in += b.frame_count;
  1646.             ALOGV("update_echo_reference: in->ref_frames_in:[%d], "
  1647.                     "in->ref_buf_size:[%d], frames:[%d], b.frame_count:[%d]",
  1648.                  in->ref_frames_in, in->ref_buf_size, frames, b.frame_count);
  1649.         }
  1650.     } else
  1651.         ALOGW("update_echo_reference: NOT enough frames to read ref buffer");
  1652.     return b.delay_ns;
  1653. }
  1654.  
  1655. static int set_preprocessor_param(effect_handle_t handle,
  1656.                            effect_param_t *param)
  1657. {
  1658.     uint32_t size = sizeof(int);
  1659.     uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
  1660.                         param->vsize;
  1661.  
  1662.     int status = (*handle)->command(handle,
  1663.                                    EFFECT_CMD_SET_PARAM,
  1664.                                    sizeof (effect_param_t) + psize,
  1665.                                    param,
  1666.                                    &size,
  1667.                                    &param->status);
  1668.     if (status == 0)
  1669.         status = param->status;
  1670.  
  1671.     return status;
  1672. }
  1673.  
  1674. static int set_preprocessor_echo_delay(effect_handle_t handle,
  1675.                                      int32_t delay_us)
  1676. {
  1677.     uint32_t buf[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
  1678.     effect_param_t *param = (effect_param_t *)buf;
  1679.  
  1680.     param->psize = sizeof(uint32_t);
  1681.     param->vsize = sizeof(uint32_t);
  1682.     *(uint32_t *)param->data = AEC_PARAM_ECHO_DELAY;
  1683.     *((int32_t *)param->data + 1) = delay_us;
  1684.  
  1685.     return set_preprocessor_param(handle, param);
  1686. }
  1687.  
  1688. static void push_echo_reference(struct sunxi_stream_in *in, size_t frames)
  1689. {
  1690.     /* read frames from echo reference buffer and update echo delay
  1691.      * in->ref_frames_in is updated with frames available in in->ref_buf */
  1692.     int32_t delay_us = update_echo_reference(in, frames)/1000;
  1693.     int i;
  1694.     audio_buffer_t buf;
  1695.  
  1696.     if (in->ref_frames_in < frames)
  1697.         frames = in->ref_frames_in;
  1698.  
  1699.     buf.frameCount = frames;
  1700.     buf.raw = in->ref_buf;
  1701.  
  1702.     for (i = 0; i < in->num_preprocessors; i++) {
  1703.         if ((*in->preprocessors[i])->process_reverse == NULL)
  1704.             continue;
  1705.  
  1706.         (*in->preprocessors[i])->process_reverse(in->preprocessors[i],
  1707.                                                &buf,
  1708.                                                NULL);
  1709.         set_preprocessor_echo_delay(in->preprocessors[i], delay_us);
  1710.     }
  1711.  
  1712.     in->ref_frames_in -= buf.frameCount;
  1713.     if (in->ref_frames_in) {
  1714.         memcpy(in->ref_buf,
  1715.                in->ref_buf + buf.frameCount * in->config.channels,
  1716.                in->ref_frames_in * in->config.channels * sizeof(int16_t));
  1717.     }
  1718. }
  1719.  
  1720. static int get_next_buffer(struct resampler_buffer_provider *buffer_provider,
  1721.                                    struct resampler_buffer* buffer)
  1722. {
  1723.     struct sunxi_stream_in *in;
  1724.  
  1725.     if (buffer_provider == NULL || buffer == NULL)
  1726.         return -EINVAL;
  1727.  
  1728.     in = (struct sunxi_stream_in *)((char *)buffer_provider -
  1729.                                    offsetof(struct sunxi_stream_in, buf_provider));
  1730.  
  1731.     if (in->pcm == NULL) {
  1732.         buffer->raw = NULL;
  1733.         buffer->frame_count = 0;
  1734.         in->read_status = -ENODEV;
  1735.         return -ENODEV;
  1736.     }
  1737.  
  1738. //  ALOGV("get_next_buffer: in->config.period_size: %d, audio_stream_frame_size: %d",
  1739. //      in->config.period_size, audio_stream_frame_size(&in->stream.common));
  1740.     if (in->frames_in == 0) {
  1741.         in->read_status = pcm_read(in->pcm,
  1742.                                    (void*)in->buffer,
  1743.                                    in->config.period_size *
  1744.                                        audio_stream_frame_size(&in->stream.common));
  1745.         if (in->read_status != 0) {
  1746.             ALOGE("get_next_buffer() pcm_read error %d, %s", in->read_status, strerror(errno));
  1747.             buffer->raw = NULL;
  1748.             buffer->frame_count = 0;
  1749.             return in->read_status;
  1750.         }
  1751.         in->frames_in = in->config.period_size;
  1752.     }
  1753.  
  1754.     buffer->frame_count = (buffer->frame_count > in->frames_in) ?
  1755.                                 in->frames_in : buffer->frame_count;
  1756.     buffer->i16 = in->buffer + (in->config.period_size - in->frames_in) *
  1757.                                                 in->config.channels;
  1758.  
  1759.     return in->read_status;
  1760.  
  1761. }
  1762.  
  1763. static void release_buffer(struct resampler_buffer_provider *buffer_provider,
  1764.                                   struct resampler_buffer* buffer)
  1765. {
  1766.     struct sunxi_stream_in *in;
  1767.  
  1768.     if (buffer_provider == NULL || buffer == NULL)
  1769.         return;
  1770.  
  1771.     in = (struct sunxi_stream_in *)((char *)buffer_provider -
  1772.                                    offsetof(struct sunxi_stream_in, buf_provider));
  1773.  
  1774.     in->frames_in -= buffer->frame_count;
  1775. }
  1776.  
  1777. /* read_frames() reads frames from kernel driver, down samples to capture rate
  1778.  * if necessary and output the number of frames requested to the buffer specified */
  1779. static ssize_t read_frames(struct sunxi_stream_in *in, void *buffer, ssize_t frames)
  1780. {
  1781.     // F_LOG;
  1782.     ssize_t frames_wr = 0;
  1783.  
  1784.     while (frames_wr < frames) {
  1785.         size_t frames_rd = frames - frames_wr;
  1786.         if (in->resampler != NULL) {
  1787.             in->resampler->resample_from_provider(in->resampler,
  1788.                     (int16_t *)((char *)buffer +
  1789.                             frames_wr * audio_stream_frame_size(&in->stream.common)),
  1790.                     &frames_rd);
  1791.         } else {
  1792.             struct resampler_buffer buf = {
  1793.                     { raw : NULL, },
  1794.                     frame_count : frames_rd,
  1795.             };
  1796.             get_next_buffer(&in->buf_provider, &buf);
  1797.             if (buf.raw != NULL) {
  1798.                 memcpy((char *)buffer +
  1799.                            frames_wr * audio_stream_frame_size(&in->stream.common),
  1800.                         buf.raw,
  1801.                         buf.frame_count * audio_stream_frame_size(&in->stream.common));
  1802.                 frames_rd = buf.frame_count;
  1803.             }
  1804.             release_buffer(&in->buf_provider, &buf);
  1805.         }
  1806.         /* in->read_status is updated by getNextBuffer() also called by
  1807.          * in->resampler->resample_from_provider() */
  1808.         if (in->read_status != 0)
  1809.             return in->read_status;
  1810.  
  1811.         frames_wr += frames_rd;
  1812.     }
  1813.     return frames_wr;
  1814. }
  1815.  
  1816. /* process_frames() reads frames from kernel driver (via read_frames()),
  1817.  * calls the active audio pre processings and output the number of frames requested
  1818.  * to the buffer specified */
  1819. static ssize_t process_frames(struct sunxi_stream_in *in, void* buffer, ssize_t frames)
  1820. {
  1821.     F_LOG;
  1822.     ssize_t frames_wr = 0;
  1823.     audio_buffer_t in_buf;
  1824.     audio_buffer_t out_buf;
  1825.     int i;
  1826.  
  1827.     while (frames_wr < frames) {
  1828.         /* first reload enough frames at the end of process input buffer */
  1829.         if (in->proc_frames_in < (size_t)frames) {
  1830.             ssize_t frames_rd;
  1831.  
  1832.             if (in->proc_buf_size < (size_t)frames) {
  1833.                 in->proc_buf_size = (size_t)frames;
  1834.                 in->proc_buf = (int16_t *)realloc(in->proc_buf,
  1835.                                          in->proc_buf_size *
  1836.                                              in->config.channels * sizeof(int16_t));
  1837.                 ALOGV("process_frames(): in->proc_buf %p size extended to %d frames",
  1838.                      in->proc_buf, in->proc_buf_size);
  1839.             }
  1840.             frames_rd = read_frames(in,
  1841.                                     in->proc_buf +
  1842.                                         in->proc_frames_in * in->config.channels,
  1843.                                     frames - in->proc_frames_in);
  1844.             if (frames_rd < 0) {
  1845.                 frames_wr = frames_rd;
  1846.                 break;
  1847.             }
  1848.             in->proc_frames_in += frames_rd;
  1849.         }
  1850.  
  1851.         if (in->echo_reference != NULL)
  1852.             push_echo_reference(in, in->proc_frames_in);
  1853.  
  1854.          /* in_buf.frameCount and out_buf.frameCount indicate respectively
  1855.           * the maximum number of frames to be consumed and produced by process() */
  1856.         in_buf.frameCount   = in->proc_frames_in;
  1857.         in_buf.s16          = in->proc_buf;
  1858.         out_buf.frameCount  = frames - frames_wr;
  1859.         out_buf.s16 = (int16_t *)buffer + frames_wr * in->config.channels;
  1860.  
  1861.         for (i = 0; i < in->num_preprocessors; i++)
  1862.             (*in->preprocessors[i])->process(in->preprocessors[i],
  1863.                                                &in_buf,
  1864.                                                &out_buf);
  1865.  
  1866.         /* process() has updated the number of frames consumed and produced in
  1867.          * in_buf.frameCount and out_buf.frameCount respectively
  1868.          * move remaining frames to the beginning of in->proc_buf */
  1869.         in->proc_frames_in -= in_buf.frameCount;
  1870.         if (in->proc_frames_in) {
  1871.             memcpy(in->proc_buf,
  1872.                    in->proc_buf + in_buf.frameCount * in->config.channels,
  1873.                    in->proc_frames_in * in->config.channels * sizeof(int16_t));
  1874.         }
  1875.  
  1876.         /* if not enough frames were passed to process(), read more and retry. */
  1877.         if (out_buf.frameCount == 0)
  1878.             continue;
  1879.  
  1880.         frames_wr += out_buf.frameCount;
  1881.     }
  1882.     return frames_wr;
  1883. }
  1884.  
  1885. static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
  1886.                        size_t bytes)
  1887. {
  1888.      F_LOG;
  1889.     int ret = 0;
  1890.     struct sunxi_stream_in *in      = (struct sunxi_stream_in *)stream;
  1891.     struct sunxi_audio_device *adev = in->dev;
  1892.     size_t frames_rq                = bytes / audio_stream_frame_size(&stream->common);
  1893.  
  1894.     /* acquiring hw device mutex systematically is useful if a low priority thread is waiting
  1895.      * on the input stream mutex - e.g. executing select_mode() while holding the hw device
  1896.      * mutex
  1897.      */
  1898.     if (adev->af_capture_flag && adev->PcmManager.BufExist) {
  1899.         pthread_mutex_lock(&adev->lock);
  1900.         pthread_mutex_lock(&in->lock);
  1901.         if (in->standby) {
  1902.             ret = start_input_stream(in);
  1903.             if (ret == 0)
  1904.                 in->standby = 0;
  1905.         }
  1906.         pthread_mutex_unlock(&adev->lock);
  1907.  
  1908.         if (ret < 0)
  1909.             goto exit;
  1910.  
  1911.         //if (bytes > adev->PcmManager.DataLen)
  1912.             //usleep(10000);
  1913.  
  1914.         ret = ReadPcmData(buffer, bytes, &adev->PcmManager);
  1915.  
  1916.         if (ret > 0)
  1917.             ret = 0;
  1918.  
  1919.         if (ret == 0 && adev->mic_mute)
  1920.             memset(buffer, 0, bytes);
  1921.  
  1922.         pthread_mutex_unlock(&in->lock);
  1923.         return bytes;
  1924.     }
  1925.  
  1926.     pthread_mutex_lock(&adev->lock);
  1927.     pthread_mutex_lock(&in->lock);
  1928.     if (in->standby) {
  1929.         ret = start_input_stream(in);
  1930.         if (ret == 0)
  1931.             in->standby = 0;
  1932.     }
  1933.     pthread_mutex_unlock(&adev->lock);
  1934.  
  1935.     if (ret < 0)
  1936.         goto exit;
  1937.  
  1938.     if (in->num_preprocessors != 0) {
  1939.         ret = process_frames(in, buffer, frames_rq);
  1940.     } else if (in->resampler != NULL) {
  1941.         ret = read_frames(in, buffer, frames_rq);
  1942.     } else {
  1943.         ret = pcm_read(in->pcm, buffer, bytes);
  1944.     }
  1945.  
  1946.     if (ret > 0)
  1947.         ret = 0;
  1948.  
  1949.     if (ret == 0 && adev->mic_mute)
  1950.         memset(buffer, 0, bytes);
  1951.  
  1952. exit:
  1953.     if (ret < 0)
  1954.         usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) /
  1955.                in_get_sample_rate(&stream->common));
  1956.  
  1957.     pthread_mutex_unlock(&in->lock);
  1958.     return bytes;
  1959. }
  1960.  
  1961. static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
  1962. {
  1963.     return 0;
  1964. }
  1965.  
  1966. static int in_add_audio_effect(const struct audio_stream *stream,
  1967.                                effect_handle_t effect)
  1968. {
  1969.     struct sunxi_stream_in *in = (struct sunxi_stream_in *)stream;
  1970.     int status;
  1971.     effect_descriptor_t desc;
  1972.  
  1973.     pthread_mutex_lock(&in->dev->lock);
  1974.     pthread_mutex_lock(&in->lock);
  1975.     if (in->num_preprocessors >= MAX_PREPROCESSORS) {
  1976.         status = -ENOSYS;
  1977.         goto exit;
  1978.     }
  1979.  
  1980.     status = (*effect)->get_descriptor(effect, &desc);
  1981.     if (status != 0)
  1982.         goto exit;
  1983.  
  1984.     in->preprocessors[in->num_preprocessors++] = effect;
  1985.  
  1986.     if (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) {
  1987.         in->need_echo_reference = true;
  1988.         do_input_standby(in);
  1989.     }
  1990.  
  1991. exit:
  1992.  
  1993.     pthread_mutex_unlock(&in->lock);
  1994.     pthread_mutex_unlock(&in->dev->lock);
  1995.     return status;
  1996. }
  1997.  
  1998. static int in_remove_audio_effect(const struct audio_stream *stream,
  1999.                                   effect_handle_t effect)
  2000. {
  2001.     struct sunxi_stream_in *in = (struct sunxi_stream_in *)stream;
  2002.     int i;
  2003.     int status = -EINVAL;
  2004.     bool found = false;
  2005.     effect_descriptor_t desc;
  2006.  
  2007.     pthread_mutex_lock(&in->dev->lock);
  2008.     pthread_mutex_lock(&in->lock);
  2009.     if (in->num_preprocessors <= 0) {
  2010.         status = -ENOSYS;
  2011.         goto exit;
  2012.     }
  2013.  
  2014.     for (i = 0; i < in->num_preprocessors; i++) {
  2015.         if (found) {
  2016.             in->preprocessors[i - 1] = in->preprocessors[i];
  2017.             continue;
  2018.         }
  2019.         if (in->preprocessors[i] == effect) {
  2020.             in->preprocessors[i] = NULL;
  2021.             status = 0;
  2022.             found = true;
  2023.         }
  2024.     }
  2025.  
  2026.     if (status != 0)
  2027.         goto exit;
  2028.  
  2029.     in->num_preprocessors--;
  2030.  
  2031.     status = (*effect)->get_descriptor(effect, &desc);
  2032.     if (status != 0)
  2033.         goto exit;
  2034.     if (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) {
  2035.         in->need_echo_reference = false;
  2036.         do_input_standby(in);
  2037.     }
  2038.  
  2039. exit:
  2040.  
  2041.     pthread_mutex_unlock(&in->lock);
  2042.     pthread_mutex_unlock(&in->dev->lock);
  2043.     return status;
  2044. }
  2045.  
  2046. static int adev_open_output_stream(struct audio_hw_device *dev,
  2047.                                    audio_io_handle_t handle,
  2048.                                    audio_devices_t devices,
  2049.                                    audio_output_flags_t flags,
  2050.                                    struct audio_config *config,
  2051.                                    struct audio_stream_out **stream_out)
  2052. {
  2053.     struct sunxi_audio_device *ladev = (struct sunxi_audio_device *)dev;
  2054.     struct sunxi_stream_out *out;
  2055.     int ret;
  2056.  
  2057.     ALOGV("adev_open_output_stream, flags: %x", flags);
  2058.  
  2059.     out = (struct sunxi_stream_out *)calloc(1, sizeof(struct sunxi_stream_out));
  2060.     if (!out)
  2061.         return -ENOMEM;
  2062.  
  2063.     out->buffer = malloc(RESAMPLER_BUFFER_SIZE); /* todo: allow for reallocing */
  2064.  
  2065.     out->stream.common.get_sample_rate  = out_get_sample_rate;
  2066.     out->stream.common.set_sample_rate  = out_set_sample_rate;
  2067.     out->stream.common.get_buffer_size  = out_get_buffer_size;
  2068.     out->stream.common.get_channels     = out_get_channels;
  2069.     out->stream.common.get_format       = out_get_format;
  2070.     out->stream.common.set_format       = out_set_format;
  2071.     out->stream.common.standby          = out_standby;
  2072.     out->stream.common.dump             = out_dump;
  2073.     out->stream.common.set_parameters   = out_set_parameters;
  2074.     out->stream.common.get_parameters   = out_get_parameters;
  2075.     out->stream.common.add_audio_effect = out_add_audio_effect;
  2076.     out->stream.common.remove_audio_effect = out_remove_audio_effect;
  2077.     out->stream.get_latency             = out_get_latency;
  2078.     out->stream.set_volume              = out_set_volume;
  2079.     out->stream.write                   = out_write;
  2080.     out->stream.get_render_position     = out_get_render_position;
  2081.     out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
  2082.  
  2083.     out->config                         = pcm_config_mm_out;
  2084.  
  2085.     out->dev        = ladev;
  2086.     out->standby    = 1;
  2087.  
  2088.     /* FIXME: when we support multiple output devices, we will want to
  2089.      * do the following:
  2090.      * adev->devices &= ~AUDIO_DEVICE_OUT_ALL;
  2091.      * adev->devices |= out->device;
  2092.      * select_output_device(adev);
  2093.      * This is because out_set_parameters() with a route is not
  2094.      * guaranteed to be called after an output stream is opened. */
  2095.     config->format          = out_get_format(&out->stream.common);
  2096.     config->channel_mask    = out_get_channels(&out->stream.common);
  2097.     config->sample_rate     = out_get_sample_rate(&out->stream.common);
  2098.  
  2099.     ALOGV("+++++++++++++++ adev_open_output_stream: req_sample_rate: %d, fmt: %x, channel_count: %d",
  2100.         config->sample_rate, config->format, config->channel_mask);
  2101.  
  2102.     *stream_out = &out->stream;
  2103.     return 0;
  2104.  
  2105. err_open:
  2106.     free(out);
  2107.     *stream_out = NULL;
  2108.     return ret;
  2109. }
  2110.  
  2111. static void adev_close_output_stream(struct audio_hw_device *dev,
  2112.                                      struct audio_stream_out *stream)
  2113. {
  2114.     struct sunxi_stream_out *out = (struct sunxi_stream_out *)stream;
  2115.  
  2116.     out_standby(&stream->common);
  2117.     if (out->buffer)
  2118.         free(out->buffer);
  2119.     if (out->resampler)
  2120.         release_resampler(out->resampler);
  2121.     free(stream);
  2122. }
  2123.  
  2124. static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
  2125. {
  2126.     struct sunxi_audio_device *adev = (struct sunxi_audio_device *)dev;
  2127.     struct str_parms *parms;
  2128.     char *str;
  2129.     char value[32];
  2130.     int ret;
  2131.  
  2132.     ALOGV("adev_set_parameters, %s", kvpairs);
  2133.  
  2134.     parms   = str_parms_create_str(kvpairs);
  2135.     ret     = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
  2136.     if (ret >= 0) {
  2137.         int tty_mode;
  2138.  
  2139.         if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
  2140.             tty_mode = TTY_MODE_OFF;
  2141.         else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
  2142.             tty_mode = TTY_MODE_VCO;
  2143.         else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
  2144.             tty_mode = TTY_MODE_HCO;
  2145.         else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
  2146.             tty_mode = TTY_MODE_FULL;
  2147.         else
  2148.             return -EINVAL;
  2149.  
  2150.         pthread_mutex_lock(&adev->lock);
  2151.         if (tty_mode != adev->tty_mode) {
  2152.             adev->tty_mode = tty_mode;
  2153.             if (adev->mode == AUDIO_MODE_IN_CALL)
  2154.                 select_output_device(adev);
  2155.         }
  2156.         pthread_mutex_unlock(&adev->lock);
  2157.     }
  2158.  
  2159.     ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
  2160.     if (ret >= 0) {
  2161.         if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
  2162.             adev->bluetooth_nrec = true;
  2163.         else
  2164.             adev->bluetooth_nrec = false;
  2165.     }
  2166.  
  2167.     str_parms_destroy(parms);
  2168.     return ret;
  2169. }
  2170.  
  2171. static char * adev_get_parameters(const struct audio_hw_device *dev,
  2172.                                   const char *keys)
  2173. {
  2174.     if (!strcmp(keys, "routing"))
  2175.     {
  2176.         char prop_value[512];
  2177.         int ret = property_get("audio.routing", prop_value, "");
  2178.         if (ret > 0)
  2179.         {
  2180.             return strdup(prop_value);
  2181.         }
  2182.     }
  2183.     return strdup("");
  2184. }
  2185.  
  2186. static int adev_init_check(const struct audio_hw_device *dev)
  2187. {
  2188.     return 0;
  2189. }
  2190.  
  2191. static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
  2192. {
  2193.     struct sunxi_audio_device *adev = (struct sunxi_audio_device *)dev;
  2194.  
  2195.     if (adev->mode == AUDIO_MODE_IN_CALL)
  2196.     {
  2197.         adev->voice_volume = volume;
  2198.  
  2199.         // 59, 47, 35, 23, 11, 0
  2200.         int vol = 59;
  2201.         if (volume >= 1.0f)
  2202.         {
  2203.             vol = 59;
  2204.         }
  2205.         else if (volume >= 0.8f)
  2206.         {
  2207.             vol = 55;
  2208.         }
  2209.         else if (volume >= 0.6f)
  2210.         {
  2211.             vol = 50;
  2212.         }
  2213.         else if (volume >= 0.4f)
  2214.         {
  2215.             vol = 43;
  2216.         }
  2217.         else if (volume >= 0.2f)
  2218.         {
  2219.             vol = 30;
  2220.         }
  2221.         else
  2222.         {
  2223.             vol = 0;
  2224.         }
  2225.  
  2226.         ALOGV("adev_set_voice_volume, volume: %f, vol: %d", volume, vol);
  2227.  
  2228.         call_volume = vol;
  2229.         mixer_ctl_set_value(adev->mixer_ctls.master_playback_volume, 0, vol);
  2230.     }
  2231.  
  2232.     return 0;
  2233. }
  2234.  
  2235. static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
  2236. {
  2237.     F_LOG;
  2238.     return -ENOSYS;
  2239. }
  2240.  
  2241. static int adev_get_master_volume(struct audio_hw_device *dev, float *volume)
  2242. {
  2243.     F_LOG;
  2244.     return -ENOSYS;
  2245. }
  2246.  
  2247. static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
  2248. {
  2249.     struct sunxi_audio_device *adev = (struct sunxi_audio_device *)dev;
  2250.  
  2251.     pthread_mutex_lock(&adev->lock);
  2252.     if (adev->mode != mode) {
  2253.         adev->mode = mode;
  2254.         select_mode(adev);
  2255.     }
  2256.     pthread_mutex_unlock(&adev->lock);
  2257.  
  2258.     return 0;
  2259. }
  2260.  
  2261. static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
  2262. {
  2263.     struct sunxi_audio_device *adev = (struct sunxi_audio_device *)dev;
  2264.  
  2265.     adev->mic_mute = state;
  2266.  
  2267.     return 0;
  2268. }
  2269.  
  2270. static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
  2271. {
  2272.     struct sunxi_audio_device *adev = (struct sunxi_audio_device *)dev;
  2273.  
  2274.     *state = adev->mic_mute;
  2275.  
  2276.     return 0;
  2277. }
  2278.  
  2279. static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
  2280.                                          const struct audio_config *config)
  2281. {
  2282.     size_t size;
  2283.     int channel_count = popcount(config->channel_mask);
  2284.     if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
  2285.         return 0;
  2286.  
  2287.     return get_input_buffer_size(config->sample_rate, config->format, channel_count);
  2288. }
  2289.  
  2290. static int adev_open_input_stream(struct audio_hw_device *dev,
  2291.                                   audio_io_handle_t handle,
  2292.                                   audio_devices_t devices,
  2293.                                   struct audio_config *config,
  2294.                                   struct audio_stream_in **stream_in)
  2295. {
  2296.     struct sunxi_audio_device *ladev = (struct sunxi_audio_device *)dev;
  2297.     struct sunxi_stream_in *in;
  2298.     int ret;
  2299.     int channel_count = popcount(config->channel_mask);
  2300.  
  2301.     *stream_in = NULL;
  2302.  
  2303.     if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
  2304.         return -EINVAL;
  2305.  
  2306.     in = (struct sunxi_stream_in *)calloc(1, sizeof(struct sunxi_stream_in));
  2307.     if (!in)
  2308.         return -ENOMEM;
  2309.  
  2310.     in->stream.common.get_sample_rate   = in_get_sample_rate;
  2311.     in->stream.common.set_sample_rate   = in_set_sample_rate;
  2312.     in->stream.common.get_buffer_size   = in_get_buffer_size;
  2313.     in->stream.common.get_channels      = in_get_channels;
  2314.     in->stream.common.get_format        = in_get_format;
  2315.     in->stream.common.set_format        = in_set_format;
  2316.     in->stream.common.standby           = in_standby;
  2317.     in->stream.common.dump              = in_dump;
  2318.     in->stream.common.set_parameters    = in_set_parameters;
  2319.     in->stream.common.get_parameters    = in_get_parameters;
  2320.     in->stream.common.add_audio_effect  = in_add_audio_effect;
  2321.     in->stream.common.remove_audio_effect = in_remove_audio_effect;
  2322.     in->stream.set_gain                 = in_set_gain;
  2323.     in->stream.read                     = in_read;
  2324.     in->stream.get_input_frames_lost    = in_get_input_frames_lost;
  2325.  
  2326.     in->requested_rate  = config->sample_rate;
  2327.  
  2328.     // default config
  2329.     memcpy(&in->config, &pcm_config_mm_in, sizeof(pcm_config_mm_in));
  2330.     in->config.channels = channel_count;
  2331.     in->config.in_init_channels = channel_count;
  2332.  
  2333.     ALOGV("to malloc in-buffer: period_size: %d, frame_size: %d",
  2334.         in->config.period_size, audio_stream_frame_size(&in->stream.common));
  2335.     in->buffer = malloc(in->config.period_size *
  2336.                         audio_stream_frame_size(&in->stream.common) * 8);
  2337.  
  2338.     if (!in->buffer) {
  2339.         ret = -ENOMEM;
  2340.         goto err;
  2341.     }
  2342.  
  2343.     ladev->af_capture_flag = false;
  2344.     //devices = AUDIO_DEVICE_IN_WIFI_DISPLAY;//for test
  2345.  
  2346.     if (devices == AUDIO_DEVICE_IN_AF) {
  2347.         ALOGV("to malloc PcmManagerBuffer: Buffer_size: %d", AF_BUFFER_SIZE);
  2348.         ladev->PcmManager.BufStart= (unsigned char *)malloc(AF_BUFFER_SIZE);
  2349.  
  2350.         if(!ladev->PcmManager.BufStart) {
  2351.             ret = -ENOMEM;
  2352.             goto err;
  2353.         }
  2354.  
  2355.         ladev->PcmManager.BufExist      = true;
  2356.         ladev->PcmManager.BufTotalLen   = AF_BUFFER_SIZE;
  2357.         ladev->PcmManager.BufWritPtr    = ladev->PcmManager.BufStart;
  2358.         ladev->PcmManager.BufReadPtr    = ladev->PcmManager.BufStart;
  2359.         ladev->PcmManager.BufValideLen  = ladev->PcmManager.BufTotalLen;
  2360.         ladev->PcmManager.DataLen       = 0;
  2361.         ladev->PcmManager.SampleRate    = config->sample_rate;
  2362.         ladev->PcmManager.Channel       = 2;
  2363.         ladev->af_capture_flag          = true;
  2364.     }
  2365.  
  2366.     in->dev     = ladev;
  2367.     in->standby = 1;
  2368.     in->device  = devices;
  2369.  
  2370.     *stream_in  = &in->stream;
  2371.     return 0;
  2372.  
  2373. err:
  2374.     if (in->resampler)
  2375.         release_resampler(in->resampler);
  2376.  
  2377.     free(in);
  2378.     return ret;
  2379. }
  2380.  
  2381. static void adev_close_input_stream(struct audio_hw_device *dev,
  2382.                                    struct audio_stream_in *stream)
  2383. {
  2384.     struct sunxi_stream_in *in          = (struct sunxi_stream_in *)stream;
  2385.     struct sunxi_audio_device *ladev    = (struct sunxi_audio_device *)dev;
  2386.  
  2387.     in_standby(&stream->common);
  2388.  
  2389.     if (in->buffer) {
  2390.         free(in->buffer);
  2391.         in->buffer = 0;
  2392.     }
  2393.     if (in->resampler) {
  2394.         release_resampler(in->resampler);
  2395.     }
  2396.     if (ladev->af_capture_flag) {
  2397.         ladev->af_capture_flag = false;
  2398.     }
  2399.     if (ladev->PcmManager.BufStart) {
  2400.         ladev->PcmManager.BufExist = false;
  2401.         free(ladev->PcmManager.BufStart);
  2402.         ladev->PcmManager.BufStart = 0;
  2403.     }
  2404.     free(stream);
  2405.     return;
  2406. }
  2407.  
  2408. static int adev_dump(const audio_hw_device_t *device, int fd)
  2409. {
  2410.     return 0;
  2411. }
  2412.  
  2413. static int adev_close(hw_device_t *device)
  2414. {
  2415.     struct sunxi_audio_device *adev = (struct sunxi_audio_device *)device;
  2416.  
  2417.     mixer_close(adev->mixer);
  2418.     free(device);
  2419.     return 0;
  2420. }
  2421.  
  2422. static uint32_t adev_get_supported_devices(const struct audio_hw_device *dev)
  2423. {
  2424.     return (/* OUT */
  2425.             AUDIO_DEVICE_OUT_EARPIECE |
  2426.             AUDIO_DEVICE_OUT_SPEAKER |
  2427.             AUDIO_DEVICE_OUT_WIRED_HEADSET |
  2428.             AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
  2429.             AUDIO_DEVICE_OUT_AUX_DIGITAL |
  2430.             AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET |
  2431.             AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET |
  2432.             AUDIO_DEVICE_OUT_ALL_SCO |
  2433.             AUDIO_DEVICE_OUT_DEFAULT |
  2434.             /* IN */
  2435.             AUDIO_DEVICE_IN_COMMUNICATION |
  2436.             AUDIO_DEVICE_IN_AMBIENT |
  2437.             AUDIO_DEVICE_IN_BUILTIN_MIC |
  2438.             AUDIO_DEVICE_IN_WIRED_HEADSET |
  2439.             AUDIO_DEVICE_IN_AUX_DIGITAL |
  2440.             AUDIO_DEVICE_IN_BACK_MIC |
  2441.             AUDIO_DEVICE_IN_AF |
  2442.             AUDIO_DEVICE_IN_ALL_SCO |
  2443.             AUDIO_DEVICE_IN_DEFAULT);
  2444. }
  2445.  
  2446. static int adev_open(const hw_module_t* module, const char* name,
  2447.                      hw_device_t** device)
  2448. {
  2449.     struct sunxi_audio_device *adev;
  2450.     int ret;
  2451.  
  2452.     if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
  2453.         return -EINVAL;
  2454.  
  2455.     adev = calloc(1, sizeof(struct sunxi_audio_device));
  2456.     if (!adev)
  2457.         return -ENOMEM;
  2458.  
  2459.     adev->hw_device.common.tag      = HARDWARE_DEVICE_TAG;
  2460.     adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
  2461.     adev->hw_device.common.module   = (struct hw_module_t *) module;
  2462.     adev->hw_device.common.close    = adev_close;
  2463.  
  2464.     adev->hw_device.get_supported_devices   = adev_get_supported_devices;
  2465.     adev->hw_device.init_check              = adev_init_check;
  2466.     adev->hw_device.set_voice_volume        = adev_set_voice_volume;
  2467.     adev->hw_device.set_master_volume       = adev_set_master_volume;
  2468.     adev->hw_device.get_master_volume       = adev_get_master_volume;
  2469.     adev->hw_device.set_mode                = adev_set_mode;
  2470.     adev->hw_device.set_mic_mute            = adev_set_mic_mute;
  2471.     adev->hw_device.get_mic_mute            = adev_get_mic_mute;
  2472.     adev->hw_device.set_parameters          = adev_set_parameters;
  2473.     adev->hw_device.get_parameters          = adev_get_parameters;
  2474.     adev->hw_device.get_input_buffer_size   = adev_get_input_buffer_size;
  2475.     adev->hw_device.open_output_stream      = adev_open_output_stream;
  2476.     adev->hw_device.close_output_stream     = adev_close_output_stream;
  2477.     adev->hw_device.open_input_stream       = adev_open_input_stream;
  2478.     adev->hw_device.close_input_stream      = adev_close_input_stream;
  2479.     adev->hw_device.dump                    = adev_dump;
  2480.  
  2481.     adev->raw_flag                          = false;
  2482.     adev->mixer                             = mixer_open(0);
  2483.  
  2484.     if (!adev->mixer) {
  2485.         free(adev);
  2486.         ALOGE("Unable to open the mixer, aborting.");
  2487.         return -EINVAL;
  2488.     }
  2489.  
  2490. #if !LOG_NDEBUG
  2491.     // dump list of mixer controls
  2492.     tinymix_list_controls(adev->mixer);
  2493. #endif
  2494.  
  2495.     adev->mixer_ctls.audio_speaker_out = mixer_get_ctl_by_name(adev->mixer,
  2496.                                        MIXER_AUDIO_SPEAKER_OUT);
  2497.     if (!adev->mixer_ctls.audio_speaker_out) {
  2498.         ALOGE("Unable to find '%s' mixer control",MIXER_AUDIO_SPEAKER_OUT);
  2499.         goto error_out;
  2500.     }
  2501.  
  2502.  
  2503.     /* Set the default route before the PCM stream is opened */
  2504.     pthread_mutex_lock(&adev->lock);
  2505.  
  2506. //    set_route_by_array(adev->mixer, defaults, 1);
  2507.     adev->mode              = AUDIO_MODE_NORMAL;
  2508.     adev->devices           = AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_IN_BUILTIN_MIC;
  2509.     select_output_device(adev);
  2510.  
  2511.     adev->pcm_modem_dl      = NULL;
  2512.     adev->pcm_modem_ul      = NULL;
  2513.     adev->voice_volume      = 1.0f;
  2514.     adev->tty_mode          = TTY_MODE_OFF;
  2515.     adev->bluetooth_nrec    = true;
  2516.     adev->wb_amr = 0;
  2517.    
  2518.     pthread_mutex_unlock(&adev->lock);
  2519.  
  2520.     *device = &adev->hw_device.common;
  2521.  
  2522.     return 0;
  2523.  
  2524. error_out:    
  2525.  
  2526. #if !LOG_NDEBUG
  2527.     /* To aid debugging, dump all mixer controls */
  2528.     {
  2529.             unsigned int cnt = mixer_get_num_ctls(adev->mixer);
  2530.             unsigned int i;
  2531.             ALOGD("Mixer dump: Nr of controls: %d",cnt);
  2532.             for (i = 0; i < cnt; i++) {
  2533.                     struct mixer_ctl* x = mixer_get_ctl(adev->mixer,i);
  2534.                     if (x != NULL) {
  2535.                             char * name;
  2536.                             const char* type;
  2537.                             name = mixer_ctl_get_name(x);
  2538.                             type = mixer_ctl_get_type_string(x);
  2539.                             ALOGD("#%d: '%s' [%s]",i,name,type);
  2540.                     }
  2541.             }
  2542.     }
  2543. #endif
  2544.  
  2545.     mixer_close(adev->mixer);
  2546.     free(adev);
  2547.     return -EINVAL;
  2548. }
  2549.  
  2550. static struct hw_module_methods_t hal_module_methods = {
  2551.     .open = adev_open,
  2552. };
  2553.  
  2554. struct audio_module HAL_MODULE_INFO_SYM = {
  2555.     .common = {
  2556.         .tag                = HARDWARE_MODULE_TAG,
  2557.         .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
  2558.         .hal_api_version    = HARDWARE_HAL_API_VERSION,
  2559.         .id                 = AUDIO_HARDWARE_MODULE_ID,
  2560.         .name               = "sunxi audio HW HAL",
  2561.         .author             = "author",
  2562.         .methods            = &hal_module_methods,
  2563.     },
  2564. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement