Home | History | Annotate | Download | only in audio_utils
      1 /*
      2 ** Copyright 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_NDEBUG 0
     18 #define LOG_TAG "echo_reference"
     19 
     20 #include <errno.h>
     21 #include <stdlib.h>
     22 #include <pthread.h>
     23 #include <cutils/log.h>
     24 #include <system/audio.h>
     25 #include <audio_utils/resampler.h>
     26 #include <audio_utils/echo_reference.h>
     27 
     28 // echo reference state: bit field indicating if read, write or both are active.
     29 enum state {
     30     ECHOREF_IDLE = 0x00,        // idle
     31     ECHOREF_READING = 0x01,     // reading is active
     32     ECHOREF_WRITING = 0x02      // writing is active
     33 };
     34 
     35 struct echo_reference {
     36     struct echo_reference_itfe itfe;
     37     int status;                     // init status
     38     uint32_t state;                 // active state: reading, writing or both
     39     audio_format_t rd_format;       // read sample format
     40     uint32_t rd_channel_count;      // read number of channels
     41     uint32_t rd_sampling_rate;      // read sampling rate in Hz
     42     size_t rd_frame_size;           // read frame size (bytes per sample)
     43     audio_format_t wr_format;       // write sample format
     44     uint32_t wr_channel_count;      // write number of channels
     45     uint32_t wr_sampling_rate;      // write sampling rate in Hz
     46     size_t wr_frame_size;           // write frame size (bytes per sample)
     47     void *buffer;                   // main buffer
     48     size_t buf_size;                // main buffer size in frames
     49     size_t frames_in;               // number of frames in main buffer
     50     void *wr_buf;                   // buffer for input conversions
     51     size_t wr_buf_size;             // size of conversion buffer in frames
     52     size_t wr_frames_in;            // number of frames in conversion buffer
     53     size_t wr_curr_frame_size;      // number of frames given to current write() function
     54     void *wr_src_buf;               // resampler input buf (either wr_buf or buffer used by write())
     55     struct timespec wr_render_time; // latest render time indicated by write()
     56                                     // default ALSA gettimeofday() format
     57     int32_t  playback_delay;        // playback buffer delay indicated by last write()
     58     int16_t prev_delta_sign;        // sign of previous delay difference:
     59                                     //  1: positive, -1: negative, 0: unknown
     60     uint16_t delta_count;           // number of consecutive delay differences with same sign
     61     pthread_mutex_t lock;                      // mutex protecting read/write concurrency
     62     pthread_cond_t cond;                       // condition signaled when data is ready to read
     63     struct resampler_itfe *resampler;          // input resampler
     64     struct resampler_buffer_provider provider; // resampler buffer provider
     65 };
     66 
     67 
     68 int echo_reference_get_next_buffer(struct resampler_buffer_provider *buffer_provider,
     69                                    struct resampler_buffer* buffer)
     70 {
     71     struct echo_reference *er;
     72 
     73     if (buffer_provider == NULL) {
     74         return -EINVAL;
     75     }
     76 
     77     er = (struct echo_reference *)((char *)buffer_provider -
     78                                       offsetof(struct echo_reference, provider));
     79 
     80     if (er->wr_src_buf == NULL || er->wr_frames_in == 0) {
     81         buffer->raw = NULL;
     82         buffer->frame_count = 0;
     83         return -ENODATA;
     84     }
     85 
     86     buffer->frame_count = (buffer->frame_count > er->wr_frames_in) ? er->wr_frames_in : buffer->frame_count;
     87     // this is er->rd_channel_count here as we resample after stereo to mono conversion if any
     88     buffer->i16 = (int16_t *)er->wr_src_buf + (er->wr_curr_frame_size - er->wr_frames_in) * er->rd_channel_count;
     89 
     90     return 0;
     91 }
     92 
     93 void echo_reference_release_buffer(struct resampler_buffer_provider *buffer_provider,
     94                                   struct resampler_buffer* buffer)
     95 {
     96     struct echo_reference *er;
     97 
     98     if (buffer_provider == NULL) {
     99         return;
    100     }
    101 
    102     er = (struct echo_reference *)((char *)buffer_provider -
    103                                       offsetof(struct echo_reference, provider));
    104 
    105     er->wr_frames_in -= buffer->frame_count;
    106 }
    107 
    108 static void echo_reference_reset_l(struct echo_reference *er)
    109 {
    110     ALOGV("echo_reference_reset_l()");
    111     free(er->buffer);
    112     er->buffer = NULL;
    113     er->buf_size = 0;
    114     er->frames_in = 0;
    115     free(er->wr_buf);
    116     er->wr_buf = NULL;
    117     er->wr_buf_size = 0;
    118     er->wr_render_time.tv_sec = 0;
    119     er->wr_render_time.tv_nsec = 0;
    120     er->delta_count = 0;
    121     er->prev_delta_sign = 0;
    122 }
    123 
    124 /* additional space in resampler buffer allowing for extra samples to be returned
    125  * by speex resampler when sample rates ratio is not an integer.
    126  */
    127 #define RESAMPLER_HEADROOM_SAMPLES   10
    128 
    129 static int echo_reference_write(struct echo_reference_itfe *echo_reference,
    130                          struct echo_reference_buffer *buffer)
    131 {
    132     struct echo_reference *er = (struct echo_reference *)echo_reference;
    133     int status = 0;
    134 
    135     if (er == NULL) {
    136         return -EINVAL;
    137     }
    138 
    139     pthread_mutex_lock(&er->lock);
    140 
    141     if (buffer == NULL) {
    142         ALOGV("echo_reference_write() stop write");
    143         er->state &= ~ECHOREF_WRITING;
    144         echo_reference_reset_l(er);
    145         goto exit;
    146     }
    147 
    148     ALOGV("echo_reference_write() START trying to write %d frames", buffer->frame_count);
    149     ALOGV("echo_reference_write() playbackTimestamp:[%d].[%d], er->playback_delay:[%d]",
    150             (int)buffer->time_stamp.tv_sec,
    151             (int)buffer->time_stamp.tv_nsec, er->playback_delay);
    152 
    153     //ALOGV("echo_reference_write() %d frames", buffer->frame_count);
    154     // discard writes until a valid time stamp is provided.
    155 
    156     if ((buffer->time_stamp.tv_sec == 0) && (buffer->time_stamp.tv_nsec == 0) &&
    157         (er->wr_render_time.tv_sec == 0) && (er->wr_render_time.tv_nsec == 0)) {
    158         goto exit;
    159     }
    160 
    161     if ((er->state & ECHOREF_WRITING) == 0) {
    162         ALOGV("echo_reference_write() start write");
    163         if (er->resampler != NULL) {
    164             er->resampler->reset(er->resampler);
    165         }
    166         er->state |= ECHOREF_WRITING;
    167     }
    168 
    169     if ((er->state & ECHOREF_READING) == 0) {
    170         goto exit;
    171     }
    172 
    173     er->wr_render_time.tv_sec  = buffer->time_stamp.tv_sec;
    174     er->wr_render_time.tv_nsec = buffer->time_stamp.tv_nsec;
    175 
    176     er->playback_delay = buffer->delay_ns;
    177 
    178     // this will be used in the get_next_buffer, to support variable input buffer sizes
    179     er->wr_curr_frame_size = buffer->frame_count;
    180 
    181     void *srcBuf;
    182     size_t inFrames;
    183     // do stereo to mono and down sampling if necessary
    184     if (er->rd_channel_count != er->wr_channel_count ||
    185             er->rd_sampling_rate != er->wr_sampling_rate) {
    186         size_t wrBufSize = buffer->frame_count;
    187 
    188         inFrames = buffer->frame_count;
    189 
    190         if (er->rd_sampling_rate != er->wr_sampling_rate) {
    191             inFrames = (buffer->frame_count * er->rd_sampling_rate) / er->wr_sampling_rate +
    192                                                     RESAMPLER_HEADROOM_SAMPLES;
    193             // wr_buf is not only used as resampler output but also for stereo to mono conversion
    194             // output so buffer size is driven by both write and read sample rates
    195             if (inFrames > wrBufSize) {
    196                 wrBufSize = inFrames;
    197             }
    198         }
    199 
    200         if (er->wr_buf_size < wrBufSize) {
    201             ALOGV("echo_reference_write() increasing write buffer size from %d to %d",
    202                     er->wr_buf_size, wrBufSize);
    203             er->wr_buf_size = wrBufSize;
    204             er->wr_buf = realloc(er->wr_buf, er->wr_buf_size * er->rd_frame_size);
    205         }
    206 
    207         if (er->rd_channel_count != er->wr_channel_count) {
    208             // must be stereo to mono
    209             int16_t *src16 = (int16_t *)buffer->raw;
    210             int16_t *dst16 = (int16_t *)er->wr_buf;
    211             size_t frames = buffer->frame_count;
    212             while (frames--) {
    213                 *dst16++ = (int16_t)(((int32_t)*src16 + (int32_t)*(src16 + 1)) >> 1);
    214                 src16 += 2;
    215             }
    216         }
    217         if (er->wr_sampling_rate != er->rd_sampling_rate) {
    218             if (er->resampler == NULL) {
    219                 int rc;
    220                 ALOGV("echo_reference_write() new ReSampler(%d, %d)",
    221                       er->wr_sampling_rate, er->rd_sampling_rate);
    222                 er->provider.get_next_buffer = echo_reference_get_next_buffer;
    223                 er->provider.release_buffer = echo_reference_release_buffer;
    224                 rc = create_resampler(er->wr_sampling_rate,
    225                                  er->rd_sampling_rate,
    226                                  er->rd_channel_count,
    227                                  RESAMPLER_QUALITY_DEFAULT,
    228                                  &er->provider,
    229                                  &er->resampler);
    230                 if (rc != 0) {
    231                     er->resampler = NULL;
    232                     ALOGV("echo_reference_write() failure to create resampler %d", rc);
    233                     status = -ENODEV;
    234                     goto exit;
    235                 }
    236             }
    237             // er->wr_src_buf and er->wr_frames_in are used by getNexBuffer() called by the resampler
    238             // to get new frames
    239             if (er->rd_channel_count != er->wr_channel_count) {
    240                 er->wr_src_buf = er->wr_buf;
    241             } else {
    242                 er->wr_src_buf = buffer->raw;
    243             }
    244             er->wr_frames_in = buffer->frame_count;
    245             // inFrames is always more than we need here to get frames remaining from previous runs
    246             // inFrames is updated by resample() with the number of frames produced
    247             ALOGV("echo_reference_write() ReSampling(%d, %d)",
    248                   er->wr_sampling_rate, er->rd_sampling_rate);
    249             er->resampler->resample_from_provider(er->resampler,
    250                                                      (int16_t *)er->wr_buf, &inFrames);
    251             ALOGV_IF(er->wr_frames_in != 0,
    252                     "echo_reference_write() er->wr_frames_in not 0 (%d) after resampler",
    253                     er->wr_frames_in);
    254         }
    255         srcBuf = er->wr_buf;
    256     } else {
    257         inFrames = buffer->frame_count;
    258         srcBuf = buffer->raw;
    259     }
    260 
    261     if (er->frames_in + inFrames > er->buf_size) {
    262         ALOGV("echo_reference_write() increasing buffer size from %d to %d",
    263                 er->buf_size, er->frames_in + inFrames);
    264                 er->buf_size = er->frames_in + inFrames;
    265                 er->buffer = realloc(er->buffer, er->buf_size * er->rd_frame_size);
    266     }
    267     memcpy((char *)er->buffer + er->frames_in * er->rd_frame_size,
    268            srcBuf,
    269            inFrames * er->rd_frame_size);
    270     er->frames_in += inFrames;
    271 
    272     ALOGV("echo_reference_write() frames written:[%d], frames total:[%d] buffer size:[%d]\n"
    273           "                       er->wr_render_time:[%d].[%d], er->playback_delay:[%d]",
    274           inFrames, er->frames_in, er->buf_size,
    275           (int)er->wr_render_time.tv_sec, (int)er->wr_render_time.tv_nsec, er->playback_delay);
    276 
    277     pthread_cond_signal(&er->cond);
    278 exit:
    279     pthread_mutex_unlock(&er->lock);
    280     ALOGV("echo_reference_write() END");
    281     return status;
    282 }
    283 
    284 // delay jump threshold to update ref buffer: 6 samples at 8kHz in nsecs
    285 #define MIN_DELAY_DELTA_NS (375000*2)
    286 // number of consecutive delta with same sign between expected and actual delay before adjusting
    287 // the buffer
    288 #define MIN_DELTA_NUM 4
    289 
    290 
    291 static int echo_reference_read(struct echo_reference_itfe *echo_reference,
    292                          struct echo_reference_buffer *buffer)
    293 {
    294     struct echo_reference *er = (struct echo_reference *)echo_reference;
    295 
    296     if (er == NULL) {
    297         return -EINVAL;
    298     }
    299 
    300     pthread_mutex_lock(&er->lock);
    301 
    302     if (buffer == NULL) {
    303         ALOGV("echo_reference_read() stop read");
    304         er->state &= ~ECHOREF_READING;
    305         goto exit;
    306     }
    307 
    308     ALOGV("echo_reference_read() START, delayCapture:[%d], "
    309             "er->frames_in:[%d],buffer->frame_count:[%d]",
    310     buffer->delay_ns, er->frames_in, buffer->frame_count);
    311 
    312     if ((er->state & ECHOREF_READING) == 0) {
    313         ALOGV("echo_reference_read() start read");
    314         echo_reference_reset_l(er);
    315         er->state |= ECHOREF_READING;
    316     }
    317 
    318     if ((er->state & ECHOREF_WRITING) == 0) {
    319         memset(buffer->raw, 0, er->rd_frame_size * buffer->frame_count);
    320         buffer->delay_ns = 0;
    321         goto exit;
    322     }
    323 
    324 //    ALOGV("echo_reference_read() %d frames", buffer->frame_count);
    325 
    326     // allow some time for new frames to arrive if not enough frames are ready for read
    327     if (er->frames_in < buffer->frame_count) {
    328         uint32_t timeoutMs = (uint32_t)((1000 * buffer->frame_count) / er->rd_sampling_rate / 2);
    329         struct timespec ts;
    330 
    331         ts.tv_sec  = timeoutMs/1000;
    332         ts.tv_nsec = timeoutMs%1000;
    333         pthread_cond_timedwait_relative_np(&er->cond, &er->lock, &ts);
    334 
    335         ALOGV_IF((er->frames_in < buffer->frame_count),
    336                  "echo_reference_read() waited %d ms but still not enough frames"\
    337                  " er->frames_in: %d, buffer->frame_count = %d",
    338                  timeoutMs, er->frames_in, buffer->frame_count);
    339     }
    340 
    341     int64_t timeDiff;
    342     struct timespec tmp;
    343 
    344     if ((er->wr_render_time.tv_sec == 0 && er->wr_render_time.tv_nsec == 0) ||
    345         (buffer->time_stamp.tv_sec == 0 && buffer->time_stamp.tv_nsec == 0)) {
    346         ALOGV("echo_reference_read(): NEW:timestamp is zero---------setting timeDiff = 0, "\
    347              "not updating delay this time");
    348         timeDiff = 0;
    349     } else {
    350         if (buffer->time_stamp.tv_nsec < er->wr_render_time.tv_nsec) {
    351             tmp.tv_sec = buffer->time_stamp.tv_sec - er->wr_render_time.tv_sec - 1;
    352             tmp.tv_nsec = 1000000000 + buffer->time_stamp.tv_nsec - er->wr_render_time.tv_nsec;
    353         } else {
    354             tmp.tv_sec = buffer->time_stamp.tv_sec - er->wr_render_time.tv_sec;
    355             tmp.tv_nsec = buffer->time_stamp.tv_nsec - er->wr_render_time.tv_nsec;
    356         }
    357         timeDiff = (((int64_t)tmp.tv_sec * 1000000000 + tmp.tv_nsec));
    358 
    359         int64_t expectedDelayNs =  er->playback_delay + buffer->delay_ns - timeDiff;
    360 
    361         if (er->resampler != NULL) {
    362             // Resampler already compensates part of the delay
    363             int32_t rsmp_delay = er->resampler->delay_ns(er->resampler);
    364             expectedDelayNs -= rsmp_delay;
    365         }
    366 
    367         ALOGV("echo_reference_read(): expectedDelayNs[%lld] = "
    368                 "er->playback_delay[%d] + delayCapture[%d] - timeDiff[%lld]",
    369                 expectedDelayNs, er->playback_delay, buffer->delay_ns, timeDiff);
    370 
    371         if (expectedDelayNs > 0) {
    372             int64_t delayNs = ((int64_t)er->frames_in * 1000000000) / er->rd_sampling_rate;
    373 
    374             int64_t  deltaNs = delayNs - expectedDelayNs;
    375 
    376             ALOGV("echo_reference_read(): EchoPathDelayDeviation between reference and DMA [%lld]", deltaNs);
    377             if (abs(deltaNs) >= MIN_DELAY_DELTA_NS) {
    378                 // smooth the variation and update the reference buffer only
    379                 // if a deviation in the same direction is observed for more than MIN_DELTA_NUM
    380                 // consecutive reads.
    381                 int16_t delay_sign = (deltaNs >= 0) ? 1 : -1;
    382                 if (delay_sign == er->prev_delta_sign) {
    383                     er->delta_count++;
    384                 } else {
    385                     er->delta_count = 1;
    386                 }
    387                 er->prev_delta_sign = delay_sign;
    388 
    389                 if (er->delta_count > MIN_DELTA_NUM) {
    390                     size_t previousFrameIn = er->frames_in;
    391                     er->frames_in = (size_t)((expectedDelayNs * er->rd_sampling_rate)/1000000000);
    392                     int offset = er->frames_in - previousFrameIn;
    393 
    394                     ALOGV("echo_reference_read(): deltaNs ENOUGH and %s: "
    395                             "er->frames_in: %d, previousFrameIn = %d",
    396                          delay_sign ? "positive" : "negative", er->frames_in, previousFrameIn);
    397 
    398                     if (deltaNs < 0) {
    399                         // Less data available in the reference buffer than expected
    400                         if (er->frames_in > er->buf_size) {
    401                             er->buf_size = er->frames_in;
    402                             er->buffer  = realloc(er->buffer, er->buf_size * er->rd_frame_size);
    403                             ALOGV("echo_reference_read(): increasing buffer size to %d",
    404                                   er->buf_size);
    405                         }
    406 
    407                         if (offset > 0) {
    408                             memset((char *)er->buffer + previousFrameIn * er->rd_frame_size,
    409                                    0, offset * er->rd_frame_size);
    410                             ALOGV("echo_reference_read(): pushing ref buffer by [%d]", offset);
    411                         }
    412                     } else {
    413                         // More data available in the reference buffer than expected
    414                         offset = -offset;
    415                         if (offset > 0) {
    416                             memcpy(er->buffer, (char *)er->buffer + (offset * er->rd_frame_size),
    417                                    er->frames_in * er->rd_frame_size);
    418                             ALOGV("echo_reference_read(): shifting ref buffer by [%d]",
    419                                   er->frames_in);
    420                         }
    421                     }
    422                 }
    423             } else {
    424                 er->delta_count = 0;
    425                 er->prev_delta_sign = 0;
    426                 ALOGV("echo_reference_read(): Constant EchoPathDelay - difference "
    427                         "between reference and DMA %lld", deltaNs);
    428             }
    429         } else {
    430             ALOGV("echo_reference_read(): NEGATIVE expectedDelayNs[%lld] =  "\
    431                  "er->playback_delay[%d] + delayCapture[%d] - timeDiff[%lld]",
    432                  expectedDelayNs, er->playback_delay, buffer->delay_ns, timeDiff);
    433         }
    434     }
    435 
    436     if (er->frames_in < buffer->frame_count) {
    437         if (buffer->frame_count > er->buf_size) {
    438             er->buf_size = buffer->frame_count;
    439             er->buffer  = realloc(er->buffer, er->buf_size * er->rd_frame_size);
    440             ALOGV("echo_reference_read(): increasing buffer size to %d", er->buf_size);
    441         }
    442         // filling up the reference buffer with 0s to match the expected delay.
    443         memset((char *)er->buffer + er->frames_in * er->rd_frame_size,
    444             0, (buffer->frame_count - er->frames_in) * er->rd_frame_size);
    445         er->frames_in = buffer->frame_count;
    446     }
    447 
    448     memcpy(buffer->raw,
    449            (char *)er->buffer,
    450            buffer->frame_count * er->rd_frame_size);
    451 
    452     er->frames_in -= buffer->frame_count;
    453     memcpy(er->buffer,
    454            (char *)er->buffer + buffer->frame_count * er->rd_frame_size,
    455            er->frames_in * er->rd_frame_size);
    456 
    457     // As the reference buffer is now time aligned to the microphone signal there is a zero delay
    458     buffer->delay_ns = 0;
    459 
    460     ALOGV("echo_reference_read() END %d frames, total frames in %d",
    461           buffer->frame_count, er->frames_in);
    462 
    463     pthread_cond_signal(&er->cond);
    464 
    465 exit:
    466     pthread_mutex_unlock(&er->lock);
    467     return 0;
    468 }
    469 
    470 
    471 int create_echo_reference(audio_format_t rdFormat,
    472                             uint32_t rdChannelCount,
    473                             uint32_t rdSamplingRate,
    474                             audio_format_t wrFormat,
    475                             uint32_t wrChannelCount,
    476                             uint32_t wrSamplingRate,
    477                             struct echo_reference_itfe **echo_reference)
    478 {
    479     struct echo_reference *er;
    480 
    481     ALOGV("create_echo_reference()");
    482 
    483     if (echo_reference == NULL) {
    484         return -EINVAL;
    485     }
    486 
    487     *echo_reference = NULL;
    488 
    489     if (rdFormat != AUDIO_FORMAT_PCM_16_BIT ||
    490             rdFormat != wrFormat) {
    491         ALOGW("create_echo_reference bad format rd %d, wr %d", rdFormat, wrFormat);
    492         return -EINVAL;
    493     }
    494     if ((rdChannelCount != 1 && rdChannelCount != 2) ||
    495             wrChannelCount != 2) {
    496         ALOGW("create_echo_reference bad channel count rd %d, wr %d", rdChannelCount, wrChannelCount);
    497         return -EINVAL;
    498     }
    499 
    500     er = (struct echo_reference *)calloc(1, sizeof(struct echo_reference));
    501 
    502     er->itfe.read = echo_reference_read;
    503     er->itfe.write = echo_reference_write;
    504 
    505     er->state = ECHOREF_IDLE;
    506     er->rd_format = rdFormat;
    507     er->rd_channel_count = rdChannelCount;
    508     er->rd_sampling_rate = rdSamplingRate;
    509     er->wr_format = wrFormat;
    510     er->wr_channel_count = wrChannelCount;
    511     er->wr_sampling_rate = wrSamplingRate;
    512     er->rd_frame_size = audio_bytes_per_sample(rdFormat) * rdChannelCount;
    513     er->wr_frame_size = audio_bytes_per_sample(wrFormat) * wrChannelCount;
    514     *echo_reference = &er->itfe;
    515     return 0;
    516 }
    517 
    518 void release_echo_reference(struct echo_reference_itfe *echo_reference) {
    519     struct echo_reference *er = (struct echo_reference *)echo_reference;
    520 
    521     if (er == NULL) {
    522         return;
    523     }
    524 
    525     ALOGV("EchoReference dstor");
    526     echo_reference_reset_l(er);
    527     if (er->resampler != NULL) {
    528         release_resampler(er->resampler);
    529     }
    530     free(er);
    531 }
    532 
    533