| Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 1 | /* | 
 | 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 Kasten | 2dd4bdd | 2012-08-29 11:10:32 -0700 | [diff] [blame] | 23 | #include <media/nbaio/Pipe.h> | 
| Glenn Kasten | 53dbe77 | 2015-01-06 10:46:38 -0800 | [diff] [blame] | 24 | #include <audio_utils/roundup.h> | 
| Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 25 |  | 
 | 26 | namespace android { | 
 | 27 |  | 
| Glenn Kasten | c26d923 | 2014-05-09 13:37:29 -0700 | [diff] [blame] | 28 | Pipe::Pipe(size_t maxFrames, const NBAIO_Format& format, void *buffer) : | 
| Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 29 |         NBAIO_Sink(format), | 
| Glenn Kasten | ed99c2b | 2016-12-12 08:31:24 -0800 | [diff] [blame] | 30 |         // TODO fifo now supports non-power-of-2 buffer sizes, so could remove the roundup | 
| Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 31 |         mMaxFrames(roundup(maxFrames)), | 
| Glenn Kasten | c26d923 | 2014-05-09 13:37:29 -0700 | [diff] [blame] | 32 |         mBuffer(buffer == NULL ? malloc(mMaxFrames * Format_frameSize(format)) : buffer), | 
| Glenn Kasten | ed99c2b | 2016-12-12 08:31:24 -0800 | [diff] [blame] | 33 |         mFifo(mMaxFrames, Format_frameSize(format), mBuffer, false /*throttlesWriter*/), | 
 | 34 |         mFifoWriter(mFifo), | 
| Glenn Kasten | c26d923 | 2014-05-09 13:37:29 -0700 | [diff] [blame] | 35 |         mReaders(0), | 
 | 36 |         mFreeBufferInDestructor(buffer == NULL) | 
| Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 37 | { | 
 | 38 | } | 
 | 39 |  | 
 | 40 | Pipe::~Pipe() | 
 | 41 | { | 
 | 42 |     ALOG_ASSERT(android_atomic_acquire_load(&mReaders) == 0); | 
| Glenn Kasten | c26d923 | 2014-05-09 13:37:29 -0700 | [diff] [blame] | 43 |     if (mFreeBufferInDestructor) { | 
 | 44 |         free(mBuffer); | 
 | 45 |     } | 
| Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 46 | } | 
 | 47 |  | 
 | 48 | ssize_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 Kasten | ed99c2b | 2016-12-12 08:31:24 -0800 | [diff] [blame] | 54 |     ssize_t actual = mFifoWriter.write(buffer, count); | 
 | 55 |     ALOG_ASSERT(actual <= count); | 
 | 56 |     if (actual <= 0) { | 
 | 57 |         return actual; | 
| Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 58 |     } | 
| Glenn Kasten | ed99c2b | 2016-12-12 08:31:24 -0800 | [diff] [blame] | 59 |     mFramesWritten += (size_t) actual; | 
 | 60 |     return actual; | 
| Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 61 | } | 
 | 62 |  | 
 | 63 | }   // namespace android |