echo_reference.c revision b3184d71bc6cee9fcbb36343e379143329be00ce
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 Laurent// container_of is used to retrieve the containing struct echo_reference of
65b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent// a struct buffer_provider passed to echo_reference_get_next_buffer() or
66b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent// echo_reference_release_buffer()
67b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent// - ptr: pointer to the contained member
68b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent// - type: type of the containing struct
69b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent// - member: name of the member in the containing struct
70b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent#ifndef container_of
71b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent#define container_of(ptr, type, member) ({                      \
72b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
73b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            (type *)( (char *)__mptr - offsetof(type,member) );})
74b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent#endif
75b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
76b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentint echo_reference_get_next_buffer(struct resampler_buffer_provider *buffer_provider,
77b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                                   struct resampler_buffer* buffer)
78b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent{
79b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    struct echo_reference *er;
80b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
81b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    if (buffer_provider == NULL) {
82b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        return -EINVAL;
83b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    }
84b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
85b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er = container_of(buffer_provider, struct echo_reference, provider);
86b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
87b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    if (er->wr_src_buf == NULL || er->wr_frames_in == 0) {
88b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        buffer->raw = NULL;
89b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        buffer->frame_count = 0;
90b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        return -ENODATA;
91b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    }
92b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
93b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    buffer->frame_count = (buffer->frame_count > er->wr_frames_in) ? er->wr_frames_in : buffer->frame_count;
94b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    // this is er->rd_channel_count here as we resample after stereo to mono conversion if any
95b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    buffer->i16 = (int16_t *)er->wr_src_buf + (er->wr_buf_size - er->wr_frames_in) * er->rd_channel_count;
96b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
97b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    return 0;
98b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent}
99b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
100b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentvoid echo_reference_release_buffer(struct resampler_buffer_provider *buffer_provider,
101b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                                  struct resampler_buffer* buffer)
102b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent{
103b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    struct echo_reference *er;
104b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
105b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    if (buffer_provider == NULL) {
106b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        return;
107b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    }
108b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
109b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er = container_of(buffer_provider, struct echo_reference, provider);
110b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
111b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->wr_frames_in -= buffer->frame_count;
112b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent}
113b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
114b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentstatic void echo_reference_reset_l(struct echo_reference *er)
115b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent{
116b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    LOGV("echo_reference_reset_l()");
117b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    free(er->buffer);
118b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->buffer = NULL;
119b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->buf_size = 0;
120b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->frames_in = 0;
121b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    free(er->wr_buf);
122b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->wr_buf = NULL;
123b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->wr_buf_size = 0;
124b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->wr_render_time.tv_sec = 0;
125b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->wr_render_time.tv_nsec = 0;
126b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent}
127b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
128b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentstatic int echo_reference_write(struct echo_reference_itfe *echo_reference,
129b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                         struct echo_reference_buffer *buffer)
130b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent{
131b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    struct echo_reference *er = (struct echo_reference *)echo_reference;
132b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    int status = 0;
133b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
134b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    if (er == NULL) {
135b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        return -EINVAL;
136b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    }
137b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
138b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    pthread_mutex_lock(&er->lock);
139b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
140b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    if (buffer == NULL) {
141b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        LOGV("echo_reference_write() stop write");
142b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        er->state &= ~ECHOREF_WRITING;
143b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        echo_reference_reset_l(er);
144b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        goto exit;
145b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    }
146b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
147b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    LOGV("echo_reference_write() START trying to write %d frames", buffer->frame_count);
148b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    LOGV("echo_reference_write() playbackTimestamp:[%d].[%d], er->playback_delay:[%d]",
149b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            (int)buffer->time_stamp.tv_sec,
150b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            (int)buffer->time_stamp.tv_nsec, er->playback_delay);
151b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
152b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    //LOGV("echo_reference_write() %d frames", buffer->frame_count);
153b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    // discard writes until a valid time stamp is provided.
154b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
155b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    if ((buffer->time_stamp.tv_sec == 0) && (buffer->time_stamp.tv_nsec == 0) &&
156b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        (er->wr_render_time.tv_sec == 0) && (er->wr_render_time.tv_nsec == 0)) {
157b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        goto exit;
158b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    }
159b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
160b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    if ((er->state & ECHOREF_WRITING) == 0) {
161b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        LOGV("echo_reference_write() start write");
162b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        if (er->down_sampler != NULL) {
163b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            er->down_sampler->reset(er->down_sampler);
164b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        }
165b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        er->state |= ECHOREF_WRITING;
166b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    }
167b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
168b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    if ((er->state & ECHOREF_READING) == 0) {
169b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        goto exit;
170b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    }
171b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
172b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->wr_render_time.tv_sec  = buffer->time_stamp.tv_sec;
173b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->wr_render_time.tv_nsec = buffer->time_stamp.tv_nsec;
174b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
175b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->playback_delay = buffer->delay_ns;
176b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
177b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    void *srcBuf;
178b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    size_t inFrames;
179b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    // do stereo to mono and down sampling if necessary
180b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    if (er->rd_channel_count != er->wr_channel_count ||
181b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            er->rd_sampling_rate != er->wr_sampling_rate) {
182b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        if (er->wr_buf_size < buffer->frame_count) {
183b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            er->wr_buf_size = buffer->frame_count;
184b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            //max buffer size is normally function of read sampling rate but as write sampling rate
185b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            //is always more than read sampling rate this works
186b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            er->wr_buf = realloc(er->wr_buf, er->wr_buf_size * er->rd_frame_size);
187b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        }
188b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
189b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        inFrames = buffer->frame_count;
190b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        if (er->rd_channel_count != er->wr_channel_count) {
191b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            // must be stereo to mono
192b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            int16_t *src16 = (int16_t *)buffer->raw;
193b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            int16_t *dst16 = (int16_t *)er->wr_buf;
194b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            size_t frames = buffer->frame_count;
195b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            while (frames--) {
196b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                *dst16++ = (int16_t)(((int32_t)*src16 + (int32_t)*(src16 + 1)) >> 1);
197b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                src16 += 2;
198b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            }
199b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        }
200b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        if (er->wr_sampling_rate != er->rd_sampling_rate) {
201b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            if (er->down_sampler == NULL) {
202b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                int rc;
203b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                LOGV("echo_reference_write() new ReSampler(%d, %d)",
204b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                      er->wr_sampling_rate, er->rd_sampling_rate);
205b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                er->provider.get_next_buffer = echo_reference_get_next_buffer;
206b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                er->provider.release_buffer = echo_reference_release_buffer;
207b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                rc = create_resampler(er->wr_sampling_rate,
208b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                                 er->rd_sampling_rate,
209b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                                 er->rd_channel_count,
210b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                                 RESAMPLER_QUALITY_VOIP,
211b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                                 &er->provider,
212b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                                 &er->down_sampler);
213b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                if (rc != 0) {
214b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                    er->down_sampler = NULL;
215b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                    LOGV("echo_reference_write() failure to create resampler %d", rc);
216b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                    status = -ENODEV;
217b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                    goto exit;
218b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                }
219b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            }
220b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            // er->wr_src_buf and er->wr_frames_in are used by getNexBuffer() called by the resampler
221b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            // to get new frames
222b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            if (er->rd_channel_count != er->wr_channel_count) {
223b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                er->wr_src_buf = er->wr_buf;
224b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            } else {
225b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                er->wr_src_buf = buffer->raw;
226b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            }
227b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            er->wr_frames_in = buffer->frame_count;
228b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            // inFrames is always more than we need here to get frames remaining from previous runs
229b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            // inFrames is updated by resample() with the number of frames produced
230b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            LOGV("echo_reference_write() ReSampling(%d, %d)",
231b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                  er->wr_sampling_rate, er->rd_sampling_rate);
232b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            er->down_sampler->resample_from_provider(er->down_sampler,
233b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                                                     (int16_t *)er->wr_buf, &inFrames);
234b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            LOGV_IF(er->wr_frames_in != 0,
235b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                    "echo_reference_write() er->wr_frames_in not 0 (%d) after resampler",
236b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                    er->wr_frames_in);
237b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        }
238b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        srcBuf = er->wr_buf;
239b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    } else {
240b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        inFrames = buffer->frame_count;
241b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        srcBuf = buffer->raw;
242b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    }
243b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
244b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    if (er->frames_in + inFrames > er->buf_size) {
245b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        LOGV("echo_reference_write() increasing buffer size from %d to %d",
246b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                er->buf_size, er->frames_in + inFrames);
247b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                er->buf_size = er->frames_in + inFrames;
248b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                er->buffer = realloc(er->buffer, er->buf_size * er->rd_frame_size);
249b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    }
250b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    memcpy((char *)er->buffer + er->frames_in * er->rd_frame_size,
251b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent           srcBuf,
252b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent           inFrames * er->rd_frame_size);
253b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->frames_in += inFrames;
254b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
255b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    LOGV("EchoReference::write_log() inFrames:[%d], mFramesInOld:[%d], "\
256b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent         "mFramesInNew:[%d], er->buf_size:[%d], er->wr_render_time:[%d].[%d],"
257b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent         "er->playback_delay:[%d]",
258b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent         inFrames, er->frames_in - inFrames, er->frames_in, er->buf_size,
259b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent         (int)er->wr_render_time.tv_sec,
260b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent         (int)er->wr_render_time.tv_nsec, er->playback_delay);
261b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
262b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    pthread_cond_signal(&er->cond);
263b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentexit:
264b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    pthread_mutex_unlock(&er->lock);
265b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    LOGV("echo_reference_write() END");
266b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    return status;
267b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent}
268b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
269b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent#define MIN_DELAY_UPDATE_NS 62500 // delay jump threshold to update ref buffer
270b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                                  // 0.5 samples at 8kHz in nsecs
271b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
272b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
273b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentstatic int echo_reference_read(struct echo_reference_itfe *echo_reference,
274b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                         struct echo_reference_buffer *buffer)
275b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent{
276b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    struct echo_reference *er = (struct echo_reference *)echo_reference;
277b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
278b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    if (er == NULL) {
279b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        return -EINVAL;
280b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    }
281b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
282b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    pthread_mutex_lock(&er->lock);
283b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
284b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    if (buffer == NULL) {
285b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        LOGV("EchoReference::read() stop read");
286b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        er->state &= ~ECHOREF_READING;
287b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        goto exit;
288b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    }
289b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
290b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    LOGV("EchoReference::read() START, delayCapture:[%d],er->frames_in:[%d],buffer->frame_count:[%d]",
291b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    buffer->delay_ns, er->frames_in, buffer->frame_count);
292b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
293b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    if ((er->state & ECHOREF_READING) == 0) {
294b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        LOGV("EchoReference::read() start read");
295b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        echo_reference_reset_l(er);
296b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        er->state |= ECHOREF_READING;
297b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    }
298b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
299b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    if ((er->state & ECHOREF_WRITING) == 0) {
300b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        memset(buffer->raw, 0, er->rd_frame_size * buffer->frame_count);
301b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        buffer->delay_ns = 0;
302b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        goto exit;
303b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    }
304b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
305b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent//    LOGV("EchoReference::read() %d frames", buffer->frame_count);
306b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
307b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    // allow some time for new frames to arrive if not enough frames are ready for read
308b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    if (er->frames_in < buffer->frame_count) {
309b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        uint32_t timeoutMs = (uint32_t)((1000 * buffer->frame_count) / er->rd_sampling_rate / 2);
310b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        struct timespec ts;
311b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
312b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        ts.tv_sec  = timeoutMs/1000;
313b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        ts.tv_nsec = timeoutMs%1000;
314b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        pthread_cond_timedwait_relative_np(&er->cond, &er->lock, &ts);
315b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
316b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        if (er->frames_in < buffer->frame_count) {
317b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            LOGV("EchoReference::read() waited %d ms but still not enough frames"\
318b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                 " er->frames_in: %d, buffer->frame_count = %d",
319b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                timeoutMs, er->frames_in, buffer->frame_count);
320b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            buffer->frame_count = er->frames_in;
321b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        }
322b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    }
323b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
324b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    int64_t timeDiff;
325b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    struct timespec tmp;
326b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
327b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    if ((er->wr_render_time.tv_sec == 0 && er->wr_render_time.tv_nsec == 0) ||
328b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        (buffer->time_stamp.tv_sec == 0 && buffer->time_stamp.tv_nsec == 0)) {
329b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        LOGV("read: NEW:timestamp is zero---------setting timeDiff = 0, "\
330b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent             "not updating delay this time");
331b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        timeDiff = 0;
332b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    } else {
333b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        if (buffer->time_stamp.tv_nsec < er->wr_render_time.tv_nsec) {
334b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            tmp.tv_sec = buffer->time_stamp.tv_sec - er->wr_render_time.tv_sec - 1;
335b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            tmp.tv_nsec = 1000000000 + buffer->time_stamp.tv_nsec - er->wr_render_time.tv_nsec;
336b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        } else {
337b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            tmp.tv_sec = buffer->time_stamp.tv_sec - er->wr_render_time.tv_sec;
338b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            tmp.tv_nsec = buffer->time_stamp.tv_nsec - er->wr_render_time.tv_nsec;
339b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        }
340b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        timeDiff = (((int64_t)tmp.tv_sec * 1000000000 + tmp.tv_nsec));
341b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
342b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        int64_t expectedDelayNs =  er->playback_delay + buffer->delay_ns - timeDiff;
343b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
344b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        LOGV("expectedDelayNs[%lld] =  er->playback_delay[%d] + delayCapture[%d] - timeDiff[%lld]",
345b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        expectedDelayNs, er->playback_delay, buffer->delay_ns, timeDiff);
346b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
347b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        if (expectedDelayNs > 0) {
348b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            int64_t delayNs = ((int64_t)er->frames_in * 1000000000) / er->rd_sampling_rate;
349b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
350b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            delayNs -= expectedDelayNs;
351b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
352b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            if (abs(delayNs) >= MIN_DELAY_UPDATE_NS) {
353b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                if (delayNs < 0) {
354b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                    size_t previousFrameIn = er->frames_in;
355b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                    er->frames_in = (expectedDelayNs * er->rd_sampling_rate)/1000000000;
356b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                    int    offset = er->frames_in - previousFrameIn;
357b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                    LOGV("EchoReference::readlog: delayNs = NEGATIVE and ENOUGH : "\
358b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                         "setting %d frames to zero er->frames_in: %d, previousFrameIn = %d",
359b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                         offset, er->frames_in, previousFrameIn);
360b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
361b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                    if (er->frames_in > er->buf_size) {
362b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                        er->buf_size = er->frames_in;
363b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                        er->buffer  = realloc(er->buffer, er->frames_in * er->rd_frame_size);
364b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                        LOGV("EchoReference::read: increasing buffer size to %d", er->buf_size);
365b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                    }
366b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
367b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                    if (offset > 0)
368b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                        memset((char *)er->buffer + previousFrameIn * er->rd_frame_size,
369b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                               0, offset * er->rd_frame_size);
370b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                } else {
371b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                    size_t  previousFrameIn = er->frames_in;
372b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                    int     framesInInt = (int)(((int64_t)expectedDelayNs *
373b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                                           (int64_t)er->rd_sampling_rate)/1000000000);
374b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                    int     offset = previousFrameIn - framesInInt;
375b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
376b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                    LOGV("EchoReference::readlog: delayNs = POSITIVE/ENOUGH :previousFrameIn: %d,"\
377b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                         "framesInInt: [%d], offset:[%d], buffer->frame_count:[%d]",
378b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                         previousFrameIn, framesInInt, offset, buffer->frame_count);
379b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
380b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                    if (framesInInt < (int)buffer->frame_count) {
381b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                        if (framesInInt > 0) {
382b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                            memset((char *)er->buffer + framesInInt * er->rd_frame_size,
383b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                                   0, (buffer->frame_count-framesInInt) * er->rd_frame_size);
384b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                            LOGV("EchoReference::read: pushing [%d] zeros into ref buffer",
385b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                                 (buffer->frame_count-framesInInt));
386b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                        } else {
387b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                            LOGV("framesInInt = %d", framesInInt);
388b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                        }
389b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                        framesInInt = buffer->frame_count;
390b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                    } else {
391b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                        if (offset > 0) {
392b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                            memcpy(er->buffer, (char *)er->buffer + (offset * er->rd_frame_size),
393b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                                   framesInInt * er->rd_frame_size);
394b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                            LOGV("EchoReference::read: shifting ref buffer by [%d]",framesInInt);
395b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                        }
396b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                    }
397b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                    er->frames_in = (size_t)framesInInt;
398b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                }
399b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            } else {
400b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                LOGV("EchoReference::read: NOT ENOUGH samples to update %lld", delayNs);
401b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            }
402b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        } else {
403b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            LOGV("NEGATIVE expectedDelayNs[%lld] =  "\
404b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                 "er->playback_delay[%d] + delayCapture[%d] - timeDiff[%lld]",
405b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                 expectedDelayNs, er->playback_delay, buffer->delay_ns, timeDiff);
406b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        }
407b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    }
408b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
409b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    memcpy(buffer->raw,
410b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent           (char *)er->buffer,
411b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent           buffer->frame_count * er->rd_frame_size);
412b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
413b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->frames_in -= buffer->frame_count;
414b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    memcpy(er->buffer,
415b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent           (char *)er->buffer + buffer->frame_count * er->rd_frame_size,
416b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent           er->frames_in * er->rd_frame_size);
417b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
418b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    // As the reference buffer is now time aligned to the microphone signal there is a zero delay
419b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    buffer->delay_ns = 0;
420b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
421b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    LOGV("EchoReference::read() END %d frames, total frames in %d",
422b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent          buffer->frame_count, er->frames_in);
423b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
424b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    pthread_cond_signal(&er->cond);
425b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
426b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentexit:
427b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    pthread_mutex_unlock(&er->lock);
428b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    return 0;
429b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent}
430b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
431b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
432b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentint create_echo_reference(audio_format_t rdFormat,
433b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                            uint32_t rdChannelCount,
434b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                            uint32_t rdSamplingRate,
435b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                            audio_format_t wrFormat,
436b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                            uint32_t wrChannelCount,
437b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                            uint32_t wrSamplingRate,
438b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent                            struct echo_reference_itfe **echo_reference)
439b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent{
440b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    struct echo_reference *er;
441b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
442b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    LOGV("create_echo_reference()");
443b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
444b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    if (echo_reference == NULL) {
445b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        return -EINVAL;
446b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    }
447b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
448b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    *echo_reference = NULL;
449b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
450b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    if (rdFormat != AUDIO_FORMAT_PCM_16_BIT ||
451b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            rdFormat != wrFormat) {
452b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        LOGW("create_echo_reference bad format rd %d, wr %d", rdFormat, wrFormat);
453b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        return -EINVAL;
454b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    }
455b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    if ((rdChannelCount != 1 && rdChannelCount != 2) ||
456b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent            wrChannelCount != 2) {
457b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        LOGW("create_echo_reference bad channel count rd %d, wr %d", rdChannelCount, wrChannelCount);
458b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        return -EINVAL;
459b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    }
460b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
461b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    if (wrSamplingRate < rdSamplingRate) {
462b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        LOGW("create_echo_reference bad smp rate rd %d, wr %d", rdSamplingRate, wrSamplingRate);
463b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        return -EINVAL;
464b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    }
465b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
466b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er = (struct echo_reference *)calloc(1, sizeof(struct echo_reference));
467b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
468b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->itfe.read = echo_reference_read;
469b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->itfe.write = echo_reference_write;
470b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
471b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->state = ECHOREF_IDLE;
472b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->rd_format = rdFormat;
473b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->rd_channel_count = rdChannelCount;
474b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->rd_sampling_rate = rdSamplingRate;
475b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->wr_format = wrFormat;
476b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->wr_channel_count = wrChannelCount;
477b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->wr_sampling_rate = wrSamplingRate;
478b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->rd_frame_size = audio_bytes_per_sample(rdFormat) * rdChannelCount;
479b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    er->wr_frame_size = audio_bytes_per_sample(wrFormat) * wrChannelCount;
480b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    *echo_reference = &er->itfe;
481b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    return 0;
482b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent}
483b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
484b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentvoid release_echo_reference(struct echo_reference_itfe *echo_reference) {
485b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    struct echo_reference *er = (struct echo_reference *)echo_reference;
486b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
487b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    if (er == NULL) {
488b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        return;
489b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    }
490b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
491b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    LOGV("EchoReference dstor");
492b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    echo_reference_reset_l(er);
493b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    if (er->down_sampler != NULL) {
494b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent        release_resampler(er->down_sampler);
495b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    }
496b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent    free(er);
497b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent}
498b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent
499