MonoPipe.h revision 767094dd98b01baf21de2ad09c27b3c98776cf73
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 <utils/LinearTransform.h> 22#include "NBAIO.h" 23 24namespace android { 25 26// MonoPipe is similar to Pipe except: 27// - supports only a single reader, called MonoPipeReader 28// - write() cannot overrun; instead it will return a short actual count if insufficient space 29// - write() can optionally block if the pipe is full 30// Like Pipe, it is not multi-thread safe for either writer or reader 31// but writer and reader can be different threads. 32class MonoPipe : public NBAIO_Sink { 33 34 friend class MonoPipeReader; 35 36public: 37 // reqFrames will be rounded up to a power of 2, and all slots are available. Must be >= 2. 38 // Note: whatever shares this object with another thread needs to do so in an SMP-safe way (like 39 // creating it the object before creating the other thread, or storing the object with a 40 // release_store). Otherwise the other thread could see a partially-constructed object. 41 MonoPipe(size_t reqFrames, NBAIO_Format format, bool writeCanBlock = false); 42 virtual ~MonoPipe(); 43 44 // NBAIO_Port interface 45 46 //virtual ssize_t negotiate(const NBAIO_Format offers[], size_t numOffers, 47 // NBAIO_Format counterOffers[], size_t& numCounterOffers); 48 //virtual NBAIO_Format format() const; 49 50 // NBAIO_Sink interface 51 52 //virtual size_t framesWritten() const; 53 //virtual size_t framesUnderrun() const; 54 //virtual size_t underruns() const; 55 56 virtual ssize_t availableToWrite() const; 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 // MonoPipe's implementation of getNextWriteTimestamp works in conjunction 61 // with MonoPipeReader. Every time a MonoPipeReader reads from the pipe, it 62 // receives a "readPTS" indicating the point in time for which the reader 63 // would like to read data. This "last read PTS" is offset by the amt of 64 // data the reader is currently mixing and then cached cached along with the 65 // updated read pointer. This cached value is the local time for which the 66 // reader is going to request data next time it reads data (assuming we are 67 // in steady state and operating with no underflows). Writers to the 68 // MonoPipe who would like to know when their next write operation will hit 69 // the speakers can call getNextWriteTimestamp which will return the value 70 // of the last read PTS plus the duration of the amt of data waiting to be 71 // read in the MonoPipe. 72 virtual status_t getNextWriteTimestamp(int64_t *timestamp); 73 74 // average number of frames present in the pipe under normal conditions. 75 // See throttling mechanism in MonoPipe::write() 76 size_t getAvgFrames() const { return mSetpoint; } 77 void setAvgFrames(size_t setpoint); 78 size_t maxFrames() const { return mMaxFrames; } 79 80 // Set the shutdown state for the write side of a pipe. 81 // This may be called by an unrelated thread. When shutdown state is 'true', 82 // a write that would otherwise block instead returns a short transfer count. 83 // There is no guarantee how long it will take for the shutdown to be recognized, 84 // but it will not be an unbounded amount of time. 85 // The state can be restored to normal by calling shutdown(false). 86 void shutdown(bool newState = true); 87 88 // Return true if the write side of a pipe is currently shutdown. 89 bool isShutdown(); 90 91 // Return NO_ERROR if there is a timestamp available 92 status_t getTimestamp(AudioTimestamp& timestamp); 93 94private: 95 // A pair of methods and a helper variable which allows the reader and the 96 // writer to update and observe the values of mFront and mNextRdPTS in an 97 // atomic lock-less fashion. 98 // 99 // :: Important :: 100 // Two assumptions must be true in order for this lock-less approach to 101 // function properly on all systems. First, there may only be one updater 102 // thread in the system. Second, the updater thread must be running at a 103 // strictly higher priority than the observer threads. Currently, both of 104 // these assumptions are true. The only updater is always a single 105 // FastMixer thread (which runs with SCHED_FIFO/RT priority while the only 106 // observer is always an AudioFlinger::PlaybackThread running with 107 // traditional (non-RT) audio priority. 108 void updateFrontAndNRPTS(int32_t newFront, int64_t newNextRdPTS); 109 void observeFrontAndNRPTS(int32_t *outFront, int64_t *outNextRdPTS); 110 volatile int32_t mUpdateSeq; 111 112 const size_t mReqFrames; // as requested in constructor, unrounded 113 const size_t mMaxFrames; // always a power of 2 114 void * const mBuffer; 115 // mFront and mRear will never be separated by more than mMaxFrames. 116 // 32-bit overflow is possible if the pipe is active for a long time, but if that happens it's 117 // safe because we "&" with (mMaxFrames-1) at end of computations to calculate a buffer index. 118 volatile int32_t mFront; // written by the reader with updateFrontAndNRPTS, observed by 119 // the writer with observeFrontAndNRPTS 120 volatile int32_t mRear; // written by writer with android_atomic_release_store, 121 // read by reader with android_atomic_acquire_load 122 volatile int64_t mNextRdPTS; // written by the reader with updateFrontAndNRPTS, observed by 123 // the writer with observeFrontAndNRPTS 124 bool mWriteTsValid; // whether mWriteTs is valid 125 struct timespec mWriteTs; // time that the previous write() completed 126 size_t mSetpoint; // target value for pipe fill depth 127 const bool mWriteCanBlock; // whether write() should block if the pipe is full 128 129 int64_t offsetTimestampByAudioFrames(int64_t ts, size_t audFrames); 130 LinearTransform mSamplesToLocalTime; 131 132 bool mIsShutdown; // whether shutdown(true) was called, no barriers are needed 133}; 134 135} // namespace android 136 137#endif // ANDROID_AUDIO_MONO_PIPE_H 138