echo_reference.c revision d0e472977acbfd5c8d56d506f40039f451f84f8f
1b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent/* 2b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** Copyright 2011, The Android Open-Source Project 3b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** 4b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** Licensed under the Apache License, Version 2.0 (the "License"); 5b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** you may not use this file except in compliance with the License. 6b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** You may obtain a copy of the License at 7b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** 8b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** http://www.apache.org/licenses/LICENSE-2.0 9b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** 10b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** Unless required by applicable law or agreed to in writing, software 11b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** distributed under the License is distributed on an "AS IS" BASIS, 12b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** See the License for the specific language governing permissions and 14b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** limitations under the License. 15b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent*/ 16b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 17b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent//#define LOG_NDEBUG 0 18b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent#define LOG_TAG "echo_reference" 19b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 20b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent#include <errno.h> 21b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent#include <stdlib.h> 22b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent#include <pthread.h> 23b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent#include <cutils/log.h> 24b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent#include <system/audio.h> 25b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent#include <audio_utils/resampler.h> 26b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent#include <audio_utils/echo_reference.h> 27b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 28b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent// echo reference state: bit field indicating if read, write or both are active. 29b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentenum state { 30b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent ECHOREF_IDLE = 0x00, // idle 31b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent ECHOREF_READING = 0x01, // reading is active 32b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent ECHOREF_WRITING = 0x02 // writing is active 33b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent}; 34b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 35b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentstruct echo_reference { 36b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent struct echo_reference_itfe itfe; 37b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent int status; // init status 38b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent uint32_t state; // active state: reading, writing or both 39b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent audio_format_t rd_format; // read sample format 40b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent uint32_t rd_channel_count; // read number of channels 41b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent uint32_t rd_sampling_rate; // read sampling rate in Hz 42b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent size_t rd_frame_size; // read frame size (bytes per sample) 43b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent audio_format_t wr_format; // write sample format 44b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent uint32_t wr_channel_count; // write number of channels 45b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent uint32_t wr_sampling_rate; // write sampling rate in Hz 46b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent size_t wr_frame_size; // write frame size (bytes per sample) 47b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent void *buffer; // main buffer 48b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent size_t buf_size; // main buffer size in frames 49b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent size_t frames_in; // number of frames in main buffer 50b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent void *wr_buf; // buffer for input conversions 51b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent size_t wr_buf_size; // size of conversion buffer in frames 52b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent size_t wr_frames_in; // number of frames in conversion buffer 53b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent void *wr_src_buf; // resampler input buf (either wr_buf or buffer used by write()) 54b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent struct timespec wr_render_time; // latest render time indicated by write() 55b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent // default ALSA gettimeofday() format 56b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent int32_t playback_delay; // playback buffer delay indicated by last write() 57b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent pthread_mutex_t lock; // mutex protecting read/write concurrency 58b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent pthread_cond_t cond; // condition signaled when data is ready to read 59b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent struct resampler_itfe *down_sampler; // input resampler 60b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent struct resampler_buffer_provider provider; // resampler buffer provider 61b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent}; 62b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 63b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 64b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentint echo_reference_get_next_buffer(struct resampler_buffer_provider *buffer_provider, 65b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent struct resampler_buffer* buffer) 66b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent{ 67b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent struct echo_reference *er; 68b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 69b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (buffer_provider == NULL) { 70b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent return -EINVAL; 71b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 72b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 73c9d5dff9789312449c936913a577fa799565bf1fEric Laurent er = (struct echo_reference *)((char *)buffer_provider - 74c9d5dff9789312449c936913a577fa799565bf1fEric Laurent offsetof(struct echo_reference, provider)); 75b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 76b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (er->wr_src_buf == NULL || er->wr_frames_in == 0) { 77b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent buffer->raw = NULL; 78b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent buffer->frame_count = 0; 79b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent return -ENODATA; 80b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 81b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 82b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent buffer->frame_count = (buffer->frame_count > er->wr_frames_in) ? er->wr_frames_in : buffer->frame_count; 83b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent // this is er->rd_channel_count here as we resample after stereo to mono conversion if any 84b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent buffer->i16 = (int16_t *)er->wr_src_buf + (er->wr_buf_size - er->wr_frames_in) * er->rd_channel_count; 85b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 86b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent return 0; 87b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent} 88b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 89b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentvoid echo_reference_release_buffer(struct resampler_buffer_provider *buffer_provider, 90b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent struct resampler_buffer* buffer) 91b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent{ 92b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent struct echo_reference *er; 93b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 94b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (buffer_provider == NULL) { 95b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent return; 96b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 97b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 98c9d5dff9789312449c936913a577fa799565bf1fEric Laurent er = (struct echo_reference *)((char *)buffer_provider - 99c9d5dff9789312449c936913a577fa799565bf1fEric Laurent offsetof(struct echo_reference, provider)); 100b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 101b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->wr_frames_in -= buffer->frame_count; 102b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent} 103b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 104b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentstatic void echo_reference_reset_l(struct echo_reference *er) 105b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent{ 106473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("echo_reference_reset_l()"); 107b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent free(er->buffer); 108b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->buffer = NULL; 109b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->buf_size = 0; 110b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->frames_in = 0; 111b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent free(er->wr_buf); 112b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->wr_buf = NULL; 113b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->wr_buf_size = 0; 114b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->wr_render_time.tv_sec = 0; 115b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->wr_render_time.tv_nsec = 0; 116b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent} 117b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 118b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentstatic int echo_reference_write(struct echo_reference_itfe *echo_reference, 119b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent struct echo_reference_buffer *buffer) 120b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent{ 121b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent struct echo_reference *er = (struct echo_reference *)echo_reference; 122b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent int status = 0; 123b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 124b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (er == NULL) { 125b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent return -EINVAL; 126b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 127b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 128b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent pthread_mutex_lock(&er->lock); 129b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 130b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (buffer == NULL) { 131473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("echo_reference_write() stop write"); 132b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->state &= ~ECHOREF_WRITING; 133b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent echo_reference_reset_l(er); 134b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent goto exit; 135b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 136b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 137473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("echo_reference_write() START trying to write %d frames", buffer->frame_count); 138473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("echo_reference_write() playbackTimestamp:[%d].[%d], er->playback_delay:[%d]", 139b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent (int)buffer->time_stamp.tv_sec, 140b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent (int)buffer->time_stamp.tv_nsec, er->playback_delay); 141b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 142473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block //ALOGV("echo_reference_write() %d frames", buffer->frame_count); 143b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent // discard writes until a valid time stamp is provided. 144b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 145b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if ((buffer->time_stamp.tv_sec == 0) && (buffer->time_stamp.tv_nsec == 0) && 146b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent (er->wr_render_time.tv_sec == 0) && (er->wr_render_time.tv_nsec == 0)) { 147b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent goto exit; 148b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 149b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 150b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if ((er->state & ECHOREF_WRITING) == 0) { 151473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("echo_reference_write() start write"); 152b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (er->down_sampler != NULL) { 153b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->down_sampler->reset(er->down_sampler); 154b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 155b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->state |= ECHOREF_WRITING; 156b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 157b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 158b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if ((er->state & ECHOREF_READING) == 0) { 159b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent goto exit; 160b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 161b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 162b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->wr_render_time.tv_sec = buffer->time_stamp.tv_sec; 163b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->wr_render_time.tv_nsec = buffer->time_stamp.tv_nsec; 164b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 165b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->playback_delay = buffer->delay_ns; 166b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 167b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent void *srcBuf; 168b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent size_t inFrames; 169b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent // do stereo to mono and down sampling if necessary 170b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (er->rd_channel_count != er->wr_channel_count || 171b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->rd_sampling_rate != er->wr_sampling_rate) { 172b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (er->wr_buf_size < buffer->frame_count) { 173b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->wr_buf_size = buffer->frame_count; 174b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent //max buffer size is normally function of read sampling rate but as write sampling rate 175b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent //is always more than read sampling rate this works 176b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->wr_buf = realloc(er->wr_buf, er->wr_buf_size * er->rd_frame_size); 177b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 178b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 179b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent inFrames = buffer->frame_count; 180b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (er->rd_channel_count != er->wr_channel_count) { 181b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent // must be stereo to mono 182b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent int16_t *src16 = (int16_t *)buffer->raw; 183b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent int16_t *dst16 = (int16_t *)er->wr_buf; 184b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent size_t frames = buffer->frame_count; 185b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent while (frames--) { 186b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent *dst16++ = (int16_t)(((int32_t)*src16 + (int32_t)*(src16 + 1)) >> 1); 187b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent src16 += 2; 188b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 189b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 190b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (er->wr_sampling_rate != er->rd_sampling_rate) { 191b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (er->down_sampler == NULL) { 192b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent int rc; 193473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("echo_reference_write() new ReSampler(%d, %d)", 194b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->wr_sampling_rate, er->rd_sampling_rate); 195b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->provider.get_next_buffer = echo_reference_get_next_buffer; 196b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->provider.release_buffer = echo_reference_release_buffer; 197b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent rc = create_resampler(er->wr_sampling_rate, 198b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->rd_sampling_rate, 199b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->rd_channel_count, 200b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent RESAMPLER_QUALITY_VOIP, 201b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent &er->provider, 202b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent &er->down_sampler); 203b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (rc != 0) { 204b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->down_sampler = NULL; 205473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("echo_reference_write() failure to create resampler %d", rc); 206b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent status = -ENODEV; 207b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent goto exit; 208b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 209b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 210b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent // er->wr_src_buf and er->wr_frames_in are used by getNexBuffer() called by the resampler 211b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent // to get new frames 212b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (er->rd_channel_count != er->wr_channel_count) { 213b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->wr_src_buf = er->wr_buf; 214b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } else { 215b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->wr_src_buf = buffer->raw; 216b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 217b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->wr_frames_in = buffer->frame_count; 218b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent // inFrames is always more than we need here to get frames remaining from previous runs 219b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent // inFrames is updated by resample() with the number of frames produced 220473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("echo_reference_write() ReSampling(%d, %d)", 221b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->wr_sampling_rate, er->rd_sampling_rate); 222b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->down_sampler->resample_from_provider(er->down_sampler, 223b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent (int16_t *)er->wr_buf, &inFrames); 224473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV_IF(er->wr_frames_in != 0, 225b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent "echo_reference_write() er->wr_frames_in not 0 (%d) after resampler", 226b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->wr_frames_in); 227b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 228b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent srcBuf = er->wr_buf; 229b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } else { 230b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent inFrames = buffer->frame_count; 231b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent srcBuf = buffer->raw; 232b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 233b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 234b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (er->frames_in + inFrames > er->buf_size) { 235473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("echo_reference_write() increasing buffer size from %d to %d", 236b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->buf_size, er->frames_in + inFrames); 237b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->buf_size = er->frames_in + inFrames; 238b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->buffer = realloc(er->buffer, er->buf_size * er->rd_frame_size); 239b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 240b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent memcpy((char *)er->buffer + er->frames_in * er->rd_frame_size, 241b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent srcBuf, 242b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent inFrames * er->rd_frame_size); 243b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->frames_in += inFrames; 244b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 245473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("EchoReference::write_log() inFrames:[%d], mFramesInOld:[%d], "\ 246b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent "mFramesInNew:[%d], er->buf_size:[%d], er->wr_render_time:[%d].[%d]," 247b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent "er->playback_delay:[%d]", 248b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent inFrames, er->frames_in - inFrames, er->frames_in, er->buf_size, 249b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent (int)er->wr_render_time.tv_sec, 250b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent (int)er->wr_render_time.tv_nsec, er->playback_delay); 251b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 252b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent pthread_cond_signal(&er->cond); 253b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentexit: 254b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent pthread_mutex_unlock(&er->lock); 255473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("echo_reference_write() END"); 256b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent return status; 257b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent} 258b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 259b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent#define MIN_DELAY_UPDATE_NS 62500 // delay jump threshold to update ref buffer 260b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent // 0.5 samples at 8kHz in nsecs 261b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 262b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 263b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentstatic int echo_reference_read(struct echo_reference_itfe *echo_reference, 264b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent struct echo_reference_buffer *buffer) 265b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent{ 266b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent struct echo_reference *er = (struct echo_reference *)echo_reference; 267b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 268b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (er == NULL) { 269b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent return -EINVAL; 270b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 271b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 272b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent pthread_mutex_lock(&er->lock); 273b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 274b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (buffer == NULL) { 275473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("EchoReference::read() stop read"); 276b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->state &= ~ECHOREF_READING; 277b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent goto exit; 278b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 279b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 280473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("EchoReference::read() START, delayCapture:[%d],er->frames_in:[%d],buffer->frame_count:[%d]", 281b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent buffer->delay_ns, er->frames_in, buffer->frame_count); 282b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 283b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if ((er->state & ECHOREF_READING) == 0) { 284473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("EchoReference::read() start read"); 285b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent echo_reference_reset_l(er); 286b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->state |= ECHOREF_READING; 287b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 288b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 289b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if ((er->state & ECHOREF_WRITING) == 0) { 290b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent memset(buffer->raw, 0, er->rd_frame_size * buffer->frame_count); 291b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent buffer->delay_ns = 0; 292b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent goto exit; 293b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 294b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 295473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block// ALOGV("EchoReference::read() %d frames", buffer->frame_count); 296b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 297b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent // allow some time for new frames to arrive if not enough frames are ready for read 298b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (er->frames_in < buffer->frame_count) { 299b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent uint32_t timeoutMs = (uint32_t)((1000 * buffer->frame_count) / er->rd_sampling_rate / 2); 300b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent struct timespec ts; 301b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 302b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent ts.tv_sec = timeoutMs/1000; 303b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent ts.tv_nsec = timeoutMs%1000; 304b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent pthread_cond_timedwait_relative_np(&er->cond, &er->lock, &ts); 305b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 306b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (er->frames_in < buffer->frame_count) { 307473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("EchoReference::read() waited %d ms but still not enough frames"\ 308b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent " er->frames_in: %d, buffer->frame_count = %d", 309b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent timeoutMs, er->frames_in, buffer->frame_count); 310b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent buffer->frame_count = er->frames_in; 311b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 312b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 313b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 314b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent int64_t timeDiff; 315b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent struct timespec tmp; 316b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 317b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if ((er->wr_render_time.tv_sec == 0 && er->wr_render_time.tv_nsec == 0) || 318b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent (buffer->time_stamp.tv_sec == 0 && buffer->time_stamp.tv_nsec == 0)) { 319473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("read: NEW:timestamp is zero---------setting timeDiff = 0, "\ 320b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent "not updating delay this time"); 321b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent timeDiff = 0; 322b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } else { 323b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (buffer->time_stamp.tv_nsec < er->wr_render_time.tv_nsec) { 324b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent tmp.tv_sec = buffer->time_stamp.tv_sec - er->wr_render_time.tv_sec - 1; 325b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent tmp.tv_nsec = 1000000000 + buffer->time_stamp.tv_nsec - er->wr_render_time.tv_nsec; 326b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } else { 327b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent tmp.tv_sec = buffer->time_stamp.tv_sec - er->wr_render_time.tv_sec; 328b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent tmp.tv_nsec = buffer->time_stamp.tv_nsec - er->wr_render_time.tv_nsec; 329b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 330b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent timeDiff = (((int64_t)tmp.tv_sec * 1000000000 + tmp.tv_nsec)); 331b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 332b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent int64_t expectedDelayNs = er->playback_delay + buffer->delay_ns - timeDiff; 333b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 334473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("expectedDelayNs[%lld] = er->playback_delay[%d] + delayCapture[%d] - timeDiff[%lld]", 335b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent expectedDelayNs, er->playback_delay, buffer->delay_ns, timeDiff); 336b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 337b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (expectedDelayNs > 0) { 338b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent int64_t delayNs = ((int64_t)er->frames_in * 1000000000) / er->rd_sampling_rate; 339b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 340b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent delayNs -= expectedDelayNs; 341b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 342b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (abs(delayNs) >= MIN_DELAY_UPDATE_NS) { 343b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (delayNs < 0) { 344b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent size_t previousFrameIn = er->frames_in; 345b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->frames_in = (expectedDelayNs * er->rd_sampling_rate)/1000000000; 346b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent int offset = er->frames_in - previousFrameIn; 347473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("EchoReference::readlog: delayNs = NEGATIVE and ENOUGH : "\ 348b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent "setting %d frames to zero er->frames_in: %d, previousFrameIn = %d", 349b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent offset, er->frames_in, previousFrameIn); 350b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 351b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (er->frames_in > er->buf_size) { 352b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->buf_size = er->frames_in; 353b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->buffer = realloc(er->buffer, er->frames_in * er->rd_frame_size); 354473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("EchoReference::read: increasing buffer size to %d", er->buf_size); 355b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 356b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 357b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (offset > 0) 358b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent memset((char *)er->buffer + previousFrameIn * er->rd_frame_size, 359b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 0, offset * er->rd_frame_size); 360b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } else { 361b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent size_t previousFrameIn = er->frames_in; 362b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent int framesInInt = (int)(((int64_t)expectedDelayNs * 363b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent (int64_t)er->rd_sampling_rate)/1000000000); 364b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent int offset = previousFrameIn - framesInInt; 365b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 366473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("EchoReference::readlog: delayNs = POSITIVE/ENOUGH :previousFrameIn: %d,"\ 367b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent "framesInInt: [%d], offset:[%d], buffer->frame_count:[%d]", 368b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent previousFrameIn, framesInInt, offset, buffer->frame_count); 369b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 370b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (framesInInt < (int)buffer->frame_count) { 371b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (framesInInt > 0) { 372b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent memset((char *)er->buffer + framesInInt * er->rd_frame_size, 373b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 0, (buffer->frame_count-framesInInt) * er->rd_frame_size); 374473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("EchoReference::read: pushing [%d] zeros into ref buffer", 375b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent (buffer->frame_count-framesInInt)); 376b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } else { 377473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("framesInInt = %d", framesInInt); 378b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 379b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent framesInInt = buffer->frame_count; 380b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } else { 381b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (offset > 0) { 382b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent memcpy(er->buffer, (char *)er->buffer + (offset * er->rd_frame_size), 383b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent framesInInt * er->rd_frame_size); 384473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("EchoReference::read: shifting ref buffer by [%d]",framesInInt); 385b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 386b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 387b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->frames_in = (size_t)framesInInt; 388b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 389b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } else { 390473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("EchoReference::read: NOT ENOUGH samples to update %lld", delayNs); 391b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 392b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } else { 393473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("NEGATIVE expectedDelayNs[%lld] = "\ 394b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent "er->playback_delay[%d] + delayCapture[%d] - timeDiff[%lld]", 395b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent expectedDelayNs, er->playback_delay, buffer->delay_ns, timeDiff); 396b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 397b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 398b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 399b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent memcpy(buffer->raw, 400b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent (char *)er->buffer, 401b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent buffer->frame_count * er->rd_frame_size); 402b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 403b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->frames_in -= buffer->frame_count; 404b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent memcpy(er->buffer, 405b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent (char *)er->buffer + buffer->frame_count * er->rd_frame_size, 406b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->frames_in * er->rd_frame_size); 407b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 408b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent // As the reference buffer is now time aligned to the microphone signal there is a zero delay 409b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent buffer->delay_ns = 0; 410b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 411473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("EchoReference::read() END %d frames, total frames in %d", 412b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent buffer->frame_count, er->frames_in); 413b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 414b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent pthread_cond_signal(&er->cond); 415b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 416b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentexit: 417b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent pthread_mutex_unlock(&er->lock); 418b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent return 0; 419b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent} 420b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 421b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 422b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentint create_echo_reference(audio_format_t rdFormat, 423b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent uint32_t rdChannelCount, 424b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent uint32_t rdSamplingRate, 425b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent audio_format_t wrFormat, 426b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent uint32_t wrChannelCount, 427b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent uint32_t wrSamplingRate, 428b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent struct echo_reference_itfe **echo_reference) 429b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent{ 430b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent struct echo_reference *er; 431b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 432473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("create_echo_reference()"); 433b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 434b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (echo_reference == NULL) { 435b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent return -EINVAL; 436b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 437b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 438b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent *echo_reference = NULL; 439b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 440b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (rdFormat != AUDIO_FORMAT_PCM_16_BIT || 441b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent rdFormat != wrFormat) { 442d0e472977acbfd5c8d56d506f40039f451f84f8fSteve Block ALOGW("create_echo_reference bad format rd %d, wr %d", rdFormat, wrFormat); 443b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent return -EINVAL; 444b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 445b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if ((rdChannelCount != 1 && rdChannelCount != 2) || 446b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent wrChannelCount != 2) { 447d0e472977acbfd5c8d56d506f40039f451f84f8fSteve Block ALOGW("create_echo_reference bad channel count rd %d, wr %d", rdChannelCount, wrChannelCount); 448b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent return -EINVAL; 449b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 450b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 451b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (wrSamplingRate < rdSamplingRate) { 452d0e472977acbfd5c8d56d506f40039f451f84f8fSteve Block ALOGW("create_echo_reference bad smp rate rd %d, wr %d", rdSamplingRate, wrSamplingRate); 453b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent return -EINVAL; 454b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 455b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 456b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er = (struct echo_reference *)calloc(1, sizeof(struct echo_reference)); 457b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 458b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->itfe.read = echo_reference_read; 459b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->itfe.write = echo_reference_write; 460b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 461b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->state = ECHOREF_IDLE; 462b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->rd_format = rdFormat; 463b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->rd_channel_count = rdChannelCount; 464b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->rd_sampling_rate = rdSamplingRate; 465b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->wr_format = wrFormat; 466b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->wr_channel_count = wrChannelCount; 467b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->wr_sampling_rate = wrSamplingRate; 468b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->rd_frame_size = audio_bytes_per_sample(rdFormat) * rdChannelCount; 469b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent er->wr_frame_size = audio_bytes_per_sample(wrFormat) * wrChannelCount; 470b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent *echo_reference = &er->itfe; 471b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent return 0; 472b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent} 473b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 474b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentvoid release_echo_reference(struct echo_reference_itfe *echo_reference) { 475b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent struct echo_reference *er = (struct echo_reference *)echo_reference; 476b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 477b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (er == NULL) { 478b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent return; 479b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 480b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 481473d4a5c5c66fd8a2e99791d61fa16076558ab54Steve Block ALOGV("EchoReference dstor"); 482b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent echo_reference_reset_l(er); 483b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent if (er->down_sampler != NULL) { 484b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent release_resampler(er->down_sampler); 485b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent } 486b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent free(er); 487b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent} 488b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 489