summaryrefslogtreecommitdiffstats
blob: 9889c7ae8deec685d8570bfd1aeee550d84c79e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * Copyright (C) 2013 Texas Instruments
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// #define LOG_NDEBUG 0
// #define VERY_VERBOSE_LOGGING

#include <media/nbaio/NBAIO.h>
#include <media/nbaio/MonoPipe.h>
#include <media/nbaio/MonoPipeReader.h>
#include <media/AudioBufferProvider.h>

#include <tiaudioutils/Log.h>
#include <tiaudioutils/MonoPipe.h>

namespace tiaudioutils {

using android::NBAIO_Format;
using android::Format_from_SR_C;

MonoPipe::MonoPipe(const PcmParams &params, uint32_t frames)
    : mParams(params)
{
    if (frames < (kPipeSizeFactor * mParams.frameCount))
        frames = kPipeSizeFactor * mParams.frameCount;

    ALOGV("MonoPipe: Create pipe for %u frames (%u bytes)",
          frames, params.framesToBytes(frames));

    NBAIO_Format format = Format_from_SR_C(mParams.sampleRate, mParams.channels);

    NBAIO_Format offers[1] = {format};
    size_t numCounterOffers = 0;

    mSink = new android::MonoPipe(frames, format, true);
    if (mSink) {
        ssize_t index = mSink->negotiate(offers, 1, NULL, numCounterOffers);
        ALOG_ASSERT(index == 0);

        mSource = new android::MonoPipeReader(mSink);
        if (mSource) {
            numCounterOffers = 0;
            index = mSource->negotiate(offers, 1, NULL, numCounterOffers);
            ALOG_ASSERT(index == 0);
        }
    }
}

MonoPipe::~MonoPipe()
{
    if (mSink) {
        mSink->shutdown(true);
        delete mSink;
    }

    if (mSource)
        delete mSource;
}

bool MonoPipe::initCheck() const
{
    return ((mSink != NULL) && (mSource != NULL));
}

int MonoPipe::read(void *buffer, uint32_t frames)
{
    ssize_t read = availableToRead();

    /* Wait the time equivalent to the pending frames */
    if ((read >= 0) && (read < (ssize_t)frames))
        usleep(((frames - read) * 1000 * 1000) / mParams.sampleRate);

    /* NBAIO pipe reader doesn't block on read() */
    read = mSource->read(buffer, frames,
                         android::AudioBufferProvider::kInvalidPTS);
    if (read < 0)
        ALOGE("MonoPipe: failed to read from pipe %d", read);

    return read;
}

int MonoPipe::write(const void *buffer, uint32_t frames)
{
    ssize_t written = mSink->write(buffer, frames);
    if (written < 0)
        ALOGE("MonoPipe: failed to write to pipe %d", written);

    return written;
}

int MonoPipe::availableToRead() const
{
    return mSource->availableToRead();
}

int MonoPipe::availableToWrite() const
{
    return mSink->availableToWrite();
}

/* ---------------------------------------------------------------------------------------- */

PipeReader::PipeReader(MonoPipe *pipe)
    : mPipe(pipe)
{
}

PipeReader::~PipeReader()
{
    if (mBuffer.i8)
        delete [] mBuffer.i8;
}

int PipeReader::getNextBuffer(BufferProvider::Buffer *buffer)
{
    uint32_t frameSize = mPipe->getParams().frameSize();
    int ret = 0;

    /* resize our internal buffer if needed */
    if (mBuffer.frameCount < buffer->frameCount) {
        if (mBuffer.i8)
            delete [] mBuffer.i8;
        mBuffer.i8 = new int8_t[buffer->frameCount * frameSize];
        if (mBuffer.i8 == NULL) {
            ALOGE("PipeReader: failed to resize internal buffer");
            buffer->frameCount = 0;
            return -ENOMEM;
        }
        mBuffer.frameCount = buffer->frameCount;
    }

    int8_t *buf = mBuffer.i8;
    int pending = buffer->frameCount;

    while (pending > 0) {
        int read = mPipe->read(buf, pending);
        if (read < 0) {
            ALOGE("PipeReader: failed to read from pipe %d", read);
            buffer->frameCount = 0;
            return read;
        } else if (read == 0) {
            ALOGW("PipeReader: underrun!");
            break;
        } else {
            pending -= read;
            buf += read * frameSize;
        }
    }

    ALOGW_IF(pending, "PipeReader: unexpected %u pending frames", pending);

    buffer->frameCount -= pending;
    buffer->raw = mBuffer.raw;

    if (!buffer->frameCount)
        ret = -EAGAIN;

    return ret;
}

void PipeReader::releaseBuffer(BufferProvider::Buffer *buffer)
{
    /* Nothing to do to release the buffer, but must be implemented */
}

/* ---------------------------------------------------------------------------------------- */

PipeWriter::PipeWriter(MonoPipe *pipe)
    : mPipe(pipe)
{
}

PipeWriter::~PipeWriter()
{
    if (mBuffer.i8)
        delete [] mBuffer.i8;
}

int PipeWriter::getNextBuffer(BufferProvider::Buffer *buffer)
{
    uint32_t frameSize = mPipe->getParams().frameSize();

    /* resize our internal buffer if needed */
    if (mBuffer.frameCount < buffer->frameCount) {
        if (mBuffer.i8)
            delete [] mBuffer.i8;
        mBuffer.i8 = new int8_t[buffer->frameCount * frameSize];
        if (mBuffer.i8 == NULL) {
            ALOGE("PipeWriter: failed to resize internal buffer");
            buffer->frameCount = 0;
            return -ENOMEM;
        }
        mBuffer.frameCount = buffer->frameCount;
    }

    buffer->raw = mBuffer.raw;
    buffer->frameCount = mBuffer.frameCount;

    return 0;
}

void PipeWriter::releaseBuffer(BufferProvider::Buffer *buffer)
{
    int8_t *buf = buffer->i8;
    int pending = buffer->frameCount;
    uint32_t frameSize = mPipe->getParams().frameSize();

    while (pending > 0) {
        int written = mPipe->write(buf, pending);
        if (written < 0) {
            ALOGE("PipeWriter: failed to write to pipe %d", written);
            return;
        }
        pending -= written;
        buf += written * frameSize;
    }

    ALOGW_IF(pending, "PipeWriter: unexpected %u pending frames", pending);
}

} /* namespace tiaudioutils */