blob: 39df3f4ebc941d432485602c98e87002587a9234 [file] [log] [blame]
Glenn Kasten01066232012-02-27 11:50:44 -08001/*
2 * Copyright (C) 2012 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_TAG "Pipe"
18//#define LOG_NDEBUG 0
19
20#include <cutils/atomic.h>
21#include <cutils/compiler.h>
22#include <utils/Log.h>
Glenn Kasten2dd4bdd2012-08-29 11:10:32 -070023#include <media/nbaio/Pipe.h>
Glenn Kasten53dbe772015-01-06 10:46:38 -080024#include <audio_utils/roundup.h>
Glenn Kasten01066232012-02-27 11:50:44 -080025
26namespace android {
27
Glenn Kastenc26d9232014-05-09 13:37:29 -070028Pipe::Pipe(size_t maxFrames, const NBAIO_Format& format, void *buffer) :
Glenn Kasten01066232012-02-27 11:50:44 -080029 NBAIO_Sink(format),
Glenn Kastened99c2b2016-12-12 08:31:24 -080030 // TODO fifo now supports non-power-of-2 buffer sizes, so could remove the roundup
Glenn Kasten01066232012-02-27 11:50:44 -080031 mMaxFrames(roundup(maxFrames)),
Glenn Kastenc26d9232014-05-09 13:37:29 -070032 mBuffer(buffer == NULL ? malloc(mMaxFrames * Format_frameSize(format)) : buffer),
Glenn Kastened99c2b2016-12-12 08:31:24 -080033 mFifo(mMaxFrames, Format_frameSize(format), mBuffer, false /*throttlesWriter*/),
34 mFifoWriter(mFifo),
Glenn Kastenc26d9232014-05-09 13:37:29 -070035 mReaders(0),
36 mFreeBufferInDestructor(buffer == NULL)
Glenn Kasten01066232012-02-27 11:50:44 -080037{
38}
39
40Pipe::~Pipe()
41{
42 ALOG_ASSERT(android_atomic_acquire_load(&mReaders) == 0);
Glenn Kastenc26d9232014-05-09 13:37:29 -070043 if (mFreeBufferInDestructor) {
44 free(mBuffer);
45 }
Glenn Kasten01066232012-02-27 11:50:44 -080046}
47
48ssize_t Pipe::write(const void *buffer, size_t count)
49{
50 // count == 0 is unlikely and not worth checking for
51 if (CC_UNLIKELY(!mNegotiated)) {
52 return NEGOTIATE;
53 }
Glenn Kastened99c2b2016-12-12 08:31:24 -080054 ssize_t actual = mFifoWriter.write(buffer, count);
55 ALOG_ASSERT(actual <= count);
56 if (actual <= 0) {
57 return actual;
Glenn Kasten01066232012-02-27 11:50:44 -080058 }
Glenn Kastened99c2b2016-12-12 08:31:24 -080059 mFramesWritten += (size_t) actual;
60 return actual;
Glenn Kasten01066232012-02-27 11:50:44 -080061}
62
63} // namespace android