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 | #ifndef ANDROID_AUDIO_PIPE_H |
| 18 | #define ANDROID_AUDIO_PIPE_H |
| 19 | |
Glenn Kasten | ed99c2b | 2016-12-12 08:31:24 -0800 | [diff] [blame] | 20 | #include <audio_utils/fifo.h> |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 21 | #include "NBAIO.h" |
| 22 | |
| 23 | namespace android { |
| 24 | |
| 25 | // Pipe is multi-thread safe for readers (see PipeReader), but safe for only a single writer thread. |
| 26 | // It cannot UNDERRUN on write, unless we allow designation of a master reader that provides the |
| 27 | // time-base. Readers can be added and removed dynamically, and it's OK to have no readers. |
| 28 | class Pipe : public NBAIO_Sink { |
| 29 | |
| 30 | friend class PipeReader; |
| 31 | |
| 32 | public: |
| 33 | // maxFrames will be rounded up to a power of 2, and all slots are available. Must be >= 2. |
Glenn Kasten | c26d923 | 2014-05-09 13:37:29 -0700 | [diff] [blame] | 34 | // buffer is an optional parameter specifying the virtual address of the pipe buffer, |
| 35 | // which must be of size roundup(maxFrames) * Format_frameSize(format) bytes. |
| 36 | Pipe(size_t maxFrames, const NBAIO_Format& format, void *buffer = NULL); |
| 37 | |
| 38 | // If a buffer was specified in the constructor, it is not automatically freed by destructor. |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 39 | virtual ~Pipe(); |
| 40 | |
| 41 | // NBAIO_Port interface |
| 42 | |
| 43 | //virtual ssize_t negotiate(const NBAIO_Format offers[], size_t numOffers, |
| 44 | // NBAIO_Format counterOffers[], size_t& numCounterOffers); |
| 45 | //virtual NBAIO_Format format() const; |
| 46 | |
| 47 | // NBAIO_Sink interface |
| 48 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 49 | //virtual int64_t framesWritten() const; |
| 50 | //virtual int64_t framesUnderrun() const; |
| 51 | //virtual int64_t underruns() const; |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 52 | |
| 53 | // The write side of a pipe permits overruns; flow control is the caller's responsibility. |
| 54 | // It doesn't return +infinity because that would guarantee an overrun. |
Glenn Kasten | ed99c2b | 2016-12-12 08:31:24 -0800 | [diff] [blame] | 55 | virtual ssize_t availableToWrite() { return mMaxFrames; } |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 56 | |
| 57 | virtual ssize_t write(const void *buffer, size_t count); |
| 58 | //virtual ssize_t writeVia(writeVia_t via, size_t total, void *user, size_t block); |
| 59 | |
| 60 | private: |
| 61 | const size_t mMaxFrames; // always a power of 2 |
| 62 | void * const mBuffer; |
Glenn Kasten | ed99c2b | 2016-12-12 08:31:24 -0800 | [diff] [blame] | 63 | audio_utils_fifo mFifo; |
| 64 | audio_utils_fifo_writer mFifoWriter; |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 65 | volatile int32_t mReaders; // number of PipeReader clients currently attached to this Pipe |
Glenn Kasten | c26d923 | 2014-05-09 13:37:29 -0700 | [diff] [blame] | 66 | const bool mFreeBufferInDestructor; |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | } // namespace android |
| 70 | |
| 71 | #endif // ANDROID_AUDIO_PIPE_H |