summaryrefslogtreecommitdiff
path: root/audio/utils/src/SimpleStream.cpp
blob: 446eb841d56b4967ace6931253b5a6f1a693e97f (plain)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/*
 * 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 <tiaudioutils/Log.h>
#include <tiaudioutils/SimpleStream.h>
#include <tiaudioutils/Resampler.h>

namespace tiaudioutils {

SimpleInStream::SimpleInStream(const PcmParams &params)
    : mParams(params), mReader(NULL), mStarted(false)
{
}

SimpleInStream::~SimpleInStream()
{
    if (mReader) {
        ALOGW("SimpleInStream: automatically unregistered");
        mReader->unregisterStream(this);
    }
}

bool SimpleInStream::initCheck() const
{
    if (!mParams.isValid()) {
        ALOGE("SimpleInStream: params are invalid");
        return false;
    }

    if (!mParams.frameCount) {
        ALOGE("SimpleInStream: params.frameCount is invalid");
        return false;
    }

    return true;
}

int SimpleInStream::start()
{
    if (!mReader) {
        ALOGE("SimpleInStream: not registered to reader, cannot start");
        return -EINVAL;
    }

    AutoMutex lock(mLock);
    if (mStarted) {
        ALOGE("SimpleInStream: stream is already started");
        return -EBUSY;
    }

    int ret = mReader->open();
    if (!ret)
        mStarted = true;
    else
        ALOGE("SimpleInStream: failed to open %d", ret);

    return ret;
}

void SimpleInStream::stop()
{
    if (!mReader) {
        ALOGE("SimpleInStream: not registered to reader, cannot stop");
        return;
    }

    AutoMutex lock(mLock);
    if (!mStarted) {
        ALOGE("SimpleInStream: stream is already stopped");
        return;
    }

    mStarted = false;
    mReader->close();
}

bool SimpleInStream::isStarted() const
{
    AutoMutex lock(mLock);
    return mStarted;
}

int SimpleInStream::read(void *buffer, size_t frames)
{
    if (!mReader) {
        ALOGE("SimpleInStream: not registered to reader, cannot read");
        return -EINVAL;
    }

    AutoMutex lock(mLock);
    if (!mStarted) {
        ALOGE("SimpleInStream: stream is not started, cannot read");
        return -EPERM;
    }

    return mReader->read(buffer, frames);
}

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

SimpleOutStream::SimpleOutStream(const PcmParams &params)
    : mParams(params), mWriter(NULL), mStarted(false)
{
}

SimpleOutStream::~SimpleOutStream()
{
    if (mWriter) {
        ALOGW("SimpleOutStream: automatically unregistered");
        mWriter->unregisterStream(this);
    }
}

bool SimpleOutStream::initCheck() const
{
    if (!mParams.isValid()) {
        ALOGE("SimpleOutStream: params are invalid");
        return false;
    }

    if (!mParams.frameCount) {
        ALOGE("SimpleOutStream: params.frameCount is invalid");
        return false;
    }

    return true;
}

int SimpleOutStream::start()
{
    if (!mWriter) {
        ALOGE("SimpleOutStream: not registered to writer, cannot start");
        return -EINVAL;
    }

    AutoMutex lock(mLock);
    if (mStarted) {
        ALOGE("SimpleOutStream: stream is already started");
        return -EBUSY;
    }

    int ret = mWriter->open();
    if (!ret)
        mStarted = true;
    else
        ALOGE("SimpleOutStream: failed to open %d", ret);

    return ret;
}

void SimpleOutStream::stop()
{
    if (!mWriter) {
        ALOGE("SimpleOutStream: not registered to writer, cannot stop");
        return;
    }

    AutoMutex lock(mLock);
    if (!mStarted) {
        ALOGE("SimpleOutStream: stream is already stopped");
        return;
    }

    mStarted = false;
    mWriter->close();
}

bool SimpleOutStream::isStarted() const
{
    AutoMutex lock(mLock);
    return mStarted;
}

int SimpleOutStream::write(const void *buffer, size_t frames)
{
    if (!mWriter) {
        ALOGE("SimpleOutStream: not registered to writer, cannot write");
        return -EINVAL;
    }

    AutoMutex lock(mLock);
    if (!mStarted) {
        ALOGE("SimpleOutStream: stream is not started, cannot write");
        return -EPERM;
    }

    return mWriter->write(buffer, frames);
}

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

SimpleReader::SimpleReader(PcmInPort *port, const PcmParams &params)
    : mPort(port),
      mParams(params),
      mStream(NULL),
      mResampler(NULL),
      mBufferProvider(*this)
{
    /* Intermediate buffer for the frames to be resampled */
    mBuffer.frameCount = mParams.frameCount;
    mBuffer.i8 = new int8_t[mParams.bufferSize()];
}

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

bool SimpleReader::initCheck() const
{
    if (mPort == NULL) {
        ALOGE("SimpleReader: invalid PCM input port");
        return false;
    }

    if (!mParams.isValid() || !mParams.frameCount) {
        ALOGE("SimpleReader: params are not valid");
        return false;
    }

    if (mBuffer.raw == NULL) {
        ALOGE("SimpleReader: intermediate buffer allocation failed");
        return false;
    }

    return true;
}

int SimpleReader::registerStream(SimpleInStream *stream)
{
    if (!stream) {
        ALOGE("SimpleReader: stream is invalid, cannot register");
        return -EINVAL;
    }

    const PcmParams &outParams = stream->getParams();
    if (!outParams.isValid() || !outParams.frameCount) {
        ALOGE("SimpleReader: stream has invalid params");
        return -EINVAL;
    }

    ALOGI("SimpleReader: register stream %p", stream);

    AutoMutex lock(mLock);
    if (mStream) {
        ALOGE("SimpleReader: reader allows only one stream");
        return -ENOTSUP;
    }

    /*
     * Hardware parameters are not known when the stream is created. It's until
     * stream registration that we have enough information to determine if the
     * stream needs resampling, and if so, what the resampling parameters are.
     * Speex-based resampler is supported only for 16-bits/sample.
     */
    if ((mParams.sampleRate != outParams.sampleRate) && (outParams.sampleBits == 16)) {
        mResampler = new Resampler(mParams, outParams);
        if (!mResampler) {
            ALOGE("SimpleReader: failed to create resampler");
            return -ENODEV;
        }
        if (!mResampler->initCheck()) {
            ALOGE("SimpleReader: failed to initialize resampler");
            delete mResampler;
            mResampler = NULL;
            return -EINVAL;
        }
    } else if ((mParams.sampleBits != outParams.sampleBits) ||
               (mParams.channels != outParams.channels) ||
               (mParams.sampleRate != outParams.sampleRate)) {
        ALOGE("SimpleReader: reader doesn't support stream's params");
        return -EINVAL;
    }

    stream->mReader = this;
    mStream = stream;

    return 0;
}

void SimpleReader::unregisterStream(SimpleInStream *stream)
{
    if (!stream) {
        ALOGE("SimpleReader: stream is invalid, cannot unregister");
        return;
    }

    if (stream->isStarted()) {
        ALOGE("SimpleReader: stream %p is not stopped, cannot unregister", stream);
        return;
    }

    ALOGI("SimpleReader: unregister stream %p", stream);

    AutoMutex lock(mLock);
    if (mStream != stream) {
        ALOGE("SimpleReader: stream is already unregistered");
        return;
    }

    if (mResampler) {
        delete mResampler;
        mResampler = NULL;
    }

    stream->mReader = NULL;
    mStream = NULL;
}

bool SimpleReader::isStreamRegistered(SimpleInStream *stream)
{
    AutoMutex lock(mLock);
    return (mStream == stream);
}

int SimpleReader::open()
{
    ALOGI("SimpleReader: open PCM port");

    AutoMutex lock(mLock);
    if (mPort->isOpen()) {
        ALOGW("SimpleReader: port is already open");
        return 0;
    }

    int ret = mPort->open(mParams);
    if (ret)
        ALOGE("SimpleReader: failed to open PCM port %d", ret);

    return ret;
}

void SimpleReader::close()
{
    ALOGI("SimpleReader: close PCM port");

    AutoMutex lock(mLock);
    if (mPort->isOpen())
        mPort->close();
    else
        ALOGW("SimpleReader: PCM port is already closed");
}


int SimpleReader::read(void *buffer, size_t frames)
{
    AutoMutex lock(mLock);
    int ret;

    if (!mPort->isOpen()) {
        ALOGE("SimpleReader: PCM port is not open");
        return -ENODEV;
    }

    if (mResampler) {
        ret = mResampler->resample(mBufferProvider,
                                   buffer, frames);
        if (ret) {
            ALOGE("SimpleReader: failed to resample %d", ret);
            return ret;
        }
        ret = frames;
    } else {
        ret = mPort->read(buffer, frames);
        if (ret < 0)
            ALOGE("SimpleReader: failed to read PCM data %d", ret);
    }

    return ret;
}

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

int SimpleReader::ReadProvider::getNextBuffer(BufferProvider::Buffer *buffer)
{
    int ret = 0;

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

    int8_t *curBuffer = mReader.mBuffer.i8;
    uint32_t pending = buffer->frameCount;
    while (pending > 0) {
        int read = mReader.mPort->read(curBuffer, pending);
        if (read < 0) {
            ALOGE("SimpleReader: failed to read PCM data %d", read);
            break;
        }
        pending -= read;
        curBuffer += read * mReader.mParams.frameSize();
    }

    ALOGW_IF(pending, "SimpleReader: could not read %d frames", pending);

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

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

    return ret;
}

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

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

SimpleWriter::SimpleWriter(PcmOutPort *port, const PcmParams &params)
    : mPort(port),
      mParams(params),
      mStream(NULL),
      mResampler(NULL)
{
    /* Intermediate buffer for the resampled frames */
    mBuffer.frameCount = mParams.frameCount;
    mBuffer.i8 = new int8_t[mParams.bufferSize()];
}

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

bool SimpleWriter::initCheck() const
{
    if (mPort == NULL) {
        ALOGE("SimpleWriter: invalid PCM output port");
        return false;
    }

    if (!mParams.isValid() || !mParams.frameCount) {
        ALOGE("SimpleWriter: params are not valid");
        return false;
    }

    if (mBuffer.raw == NULL) {
        ALOGE("SimpleWriter: intermediate buffer allocation failed");
        return false;
    }

    return true;
}

int SimpleWriter::registerStream(SimpleOutStream *stream)
{
    if (!stream) {
        ALOGE("SimpleWriter: stream is invalid, cannot register");
        return -EINVAL;
    }

    const PcmParams &inParams = stream->getParams();
    if (!inParams.isValid() || !inParams.frameCount) {
        ALOGE("SimpleWriter: stream has invalid params");
        return -EINVAL;
    }

    ALOGI("SimpleWriter: register stream %p", stream);

    AutoMutex lock(mLock);
    if (mStream) {
        ALOGE("SimpleWriter: writer allows only one stream");
        return -ENOTSUP;
    }

    /*
     * Hardware parameters are not known when the stream is created. It's until
     * stream registration that we have enough information to determine if the
     * stream needs resampling, and if so, what the resampling parameters are.
     * Speex-based resampler is supported only for 16-bits/sample.
     */
    if ((mParams.sampleRate != inParams.sampleRate) && (inParams.sampleBits == 16)) {
        mResampler = new Resampler(inParams, mParams);
        if (!mResampler) {
            ALOGE("SimpleWriter: failed to create resampler");
            return -ENODEV;
        }
        if (!mResampler->initCheck()) {
            ALOGE("SimpleWriter: failed to initialize resampler");
            delete mResampler;
            mResampler = NULL;
            return -EINVAL;
        }
    } else if ((mParams.sampleBits != inParams.sampleBits) ||
               (mParams.channels != inParams.channels) ||
               (mParams.sampleRate != inParams.sampleRate)) {
        ALOGE("SimpleWriter: writer doesn't support stream's params");
        return -EINVAL;
    }

    stream->mWriter = this;
    mStream = stream;

    return 0;
}

void SimpleWriter::unregisterStream(SimpleOutStream *stream)
{
    if (!stream) {
        ALOGE("SimpleWriter: stream is invalid, cannot unregister");
        return;
    }

    if (stream->isStarted()) {
        ALOGE("SimpleWriter: stream %p is not stopped, cannot unregister", stream);
        return;
    }

    ALOGI("SimpleWriter: unregister stream %p", stream);

    AutoMutex lock(mLock);
    if (mStream != stream) {
        ALOGE("SimpleWriter: stream is already unregistered");
        return;
    }

    if (mResampler) {
        delete mResampler;
        mResampler = NULL;
    }

    stream->mWriter = NULL;
    mStream = NULL;
}

bool SimpleWriter::isStreamRegistered(SimpleOutStream *stream)
{
    AutoMutex lock(mLock);
    return (mStream == stream);
}

int SimpleWriter::open()
{
    ALOGI("SimpleWriter: open PCM port");

    AutoMutex lock(mLock);
    if (mPort->isOpen()) {
        ALOGW("SimpleWriter: port is already open");
        return 0;
    }

    int ret = mPort->open(mParams);
    if (ret)
        ALOGE("SimpleWriter: failed to open PCM port %d", ret);

    return ret;
}

void SimpleWriter::close()
{
    ALOGI("SimpleWriter: close PCM port");

    AutoMutex lock(mLock);
    if (mPort->isOpen())
        mPort->close();
    else
        ALOGW("SimpleWriter: PCM port is already closed");
}


int SimpleWriter::write(const void *buffer, size_t frames)
{
    AutoMutex lock(mLock);

    if (!mPort->isOpen()) {
        ALOGE("SimpleWriter: PCM port is not open");
        return -ENODEV;
    }

    const void *outBuffer;
    uint32_t outFrames;
    int ret;

    if (mResampler) {
        outBuffer = mBuffer.raw;
        outFrames = mBuffer.frameCount;
        ret = mResampler->resample(buffer, frames, mBuffer.raw, outFrames);
        if (ret) {
            ALOGE("SimpleWriter: failed to resample %d", ret);
            return ret;
        }
    } else {
        outBuffer = buffer;
        outFrames = frames;
    }

    ret = mPort->write(outBuffer, outFrames);
    if (ret < 0) {
        ALOGE("SimpleWriter: failed to write PCM data %d", ret);
        return ret;
    }

    return frames;
}

} /* namespace tiaudioutils */