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