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_MONO_PIPE_H |
| 18 | #define ANDROID_AUDIO_MONO_PIPE_H |
| 19 | |
| 20 | #include <time.h> |
| 21 | #include "NBAIO.h" |
Glenn Kasten | a07a1c2 | 2013-08-23 10:54:35 -0700 | [diff] [blame] | 22 | #include <media/SingleStateQueue.h> |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 26 | typedef SingleStateQueue<ExtendedTimestamp> ExtendedTimestampSingleStateQueue; |
Glenn Kasten | a07a1c2 | 2013-08-23 10:54:35 -0700 | [diff] [blame] | 27 | |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 28 | // MonoPipe is similar to Pipe except: |
| 29 | // - supports only a single reader, called MonoPipeReader |
| 30 | // - write() cannot overrun; instead it will return a short actual count if insufficient space |
| 31 | // - write() can optionally block if the pipe is full |
| 32 | // Like Pipe, it is not multi-thread safe for either writer or reader |
| 33 | // but writer and reader can be different threads. |
| 34 | class MonoPipe : public NBAIO_Sink { |
| 35 | |
| 36 | friend class MonoPipeReader; |
| 37 | |
| 38 | public: |
Glenn Kasten | 820ba70 | 2012-05-30 16:34:17 -0700 | [diff] [blame] | 39 | // reqFrames will be rounded up to a power of 2, and all slots are available. Must be >= 2. |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 40 | // Note: whatever shares this object with another thread needs to do so in an SMP-safe way (like |
| 41 | // creating it the object before creating the other thread, or storing the object with a |
| 42 | // release_store). Otherwise the other thread could see a partially-constructed object. |
Glenn Kasten | 72e54af | 2014-01-31 09:37:35 -0800 | [diff] [blame] | 43 | MonoPipe(size_t reqFrames, const NBAIO_Format& format, bool writeCanBlock = false); |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 44 | virtual ~MonoPipe(); |
| 45 | |
| 46 | // NBAIO_Port interface |
| 47 | |
| 48 | //virtual ssize_t negotiate(const NBAIO_Format offers[], size_t numOffers, |
| 49 | // NBAIO_Format counterOffers[], size_t& numCounterOffers); |
| 50 | //virtual NBAIO_Format format() const; |
| 51 | |
| 52 | // NBAIO_Sink interface |
| 53 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 54 | //virtual int64_t framesWritten() const; |
| 55 | //virtual int64_t framesUnderrun() const; |
| 56 | //virtual int64_t underruns() const; |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 57 | |
| 58 | virtual ssize_t availableToWrite() const; |
| 59 | virtual ssize_t write(const void *buffer, size_t count); |
| 60 | //virtual ssize_t writeVia(writeVia_t via, size_t total, void *user, size_t block); |
| 61 | |
Eric Laurent | e737cda | 2012-05-22 18:55:44 -0700 | [diff] [blame] | 62 | // average number of frames present in the pipe under normal conditions. |
| 63 | // See throttling mechanism in MonoPipe::write() |
Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 64 | size_t getAvgFrames() const { return mSetpoint; } |
| 65 | void setAvgFrames(size_t setpoint); |
| 66 | size_t maxFrames() const { return mMaxFrames; } |
Eric Laurent | e737cda | 2012-05-22 18:55:44 -0700 | [diff] [blame] | 67 | |
Glenn Kasten | 003d9f7 | 2012-09-28 12:22:52 -0700 | [diff] [blame] | 68 | // Set the shutdown state for the write side of a pipe. |
| 69 | // This may be called by an unrelated thread. When shutdown state is 'true', |
| 70 | // a write that would otherwise block instead returns a short transfer count. |
| 71 | // There is no guarantee how long it will take for the shutdown to be recognized, |
| 72 | // but it will not be an unbounded amount of time. |
| 73 | // The state can be restored to normal by calling shutdown(false). |
| 74 | void shutdown(bool newState = true); |
| 75 | |
| 76 | // Return true if the write side of a pipe is currently shutdown. |
| 77 | bool isShutdown(); |
| 78 | |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 79 | // Return NO_ERROR if there is a timestamp available |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 80 | status_t getTimestamp(ExtendedTimestamp ×tamp); |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 81 | |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 82 | private: |
Glenn Kasten | 820ba70 | 2012-05-30 16:34:17 -0700 | [diff] [blame] | 83 | const size_t mReqFrames; // as requested in constructor, unrounded |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 84 | const size_t mMaxFrames; // always a power of 2 |
| 85 | void * const mBuffer; |
| 86 | // mFront and mRear will never be separated by more than mMaxFrames. |
| 87 | // 32-bit overflow is possible if the pipe is active for a long time, but if that happens it's |
| 88 | // safe because we "&" with (mMaxFrames-1) at end of computations to calculate a buffer index. |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 89 | volatile int32_t mFront; // written by reader with android_atomic_release_store, |
| 90 | // read by writer with android_atomic_acquire_load |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 91 | volatile int32_t mRear; // written by writer with android_atomic_release_store, |
| 92 | // read by reader with android_atomic_acquire_load |
Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 93 | bool mWriteTsValid; // whether mWriteTs is valid |
| 94 | struct timespec mWriteTs; // time that the previous write() completed |
| 95 | size_t mSetpoint; // target value for pipe fill depth |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 96 | const bool mWriteCanBlock; // whether write() should block if the pipe is full |
John Grossman | 2c3b2da | 2012-08-02 17:08:54 -0700 | [diff] [blame] | 97 | |
Glenn Kasten | 003d9f7 | 2012-09-28 12:22:52 -0700 | [diff] [blame] | 98 | bool mIsShutdown; // whether shutdown(true) was called, no barriers are needed |
Glenn Kasten | a07a1c2 | 2013-08-23 10:54:35 -0700 | [diff] [blame] | 99 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 100 | ExtendedTimestampSingleStateQueue::Shared mTimestampShared; |
| 101 | ExtendedTimestampSingleStateQueue::Mutator mTimestampMutator; |
| 102 | ExtendedTimestampSingleStateQueue::Observer mTimestampObserver; |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | } // namespace android |
| 106 | |
| 107 | #endif // ANDROID_AUDIO_MONO_PIPE_H |