MonoPipe.h revision 2dd4bdd715f586d4d30cf90cc6fc2bbfbce60fe0
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 80private: 81 // A pair of methods and a helper variable which allows the reader and the 82 // writer to update and observe the values of mFront and mNextRdPTS in an 83 // atomic lock-less fashion. 84 // 85 // :: Important :: 86 // Two assumptions must be true in order for this lock-less approach to 87 // function properly on all systems. First, there may only be one updater 88 // thread in the system. Second, the updater thread must be running at a 89 // strictly higher priority than the observer threads. Currently, both of 90 // these assumptions are true. The only updater is always a single 91 // FastMixer thread (which runs with SCHED_FIFO/RT priority while the only 92 // observer is always an AudioFlinger::PlaybackThread running with 93 // traditional (non-RT) audio priority. 94 void updateFrontAndNRPTS(int32_t newFront, int64_t newNextRdPTS); 95 void observeFrontAndNRPTS(int32_t *outFront, int64_t *outNextRdPTS); 96 volatile int32_t mUpdateSeq; 97 98 const size_t mReqFrames; // as requested in constructor, unrounded 99 const size_t mMaxFrames; // always a power of 2 100 void * const mBuffer; 101 // mFront and mRear will never be separated by more than mMaxFrames. 102 // 32-bit overflow is possible if the pipe is active for a long time, but if that happens it's 103 // safe because we "&" with (mMaxFrames-1) at end of computations to calculate a buffer index. 104 volatile int32_t mFront; // written by the reader with updateFrontAndNRPTS, observed by 105 // the writer with observeFrontAndNRPTS 106 volatile int32_t mRear; // written by writer with android_atomic_release_store, 107 // read by reader with android_atomic_acquire_load 108 volatile int64_t mNextRdPTS; // written by the reader with updateFrontAndNRPTS, observed by 109 // the writer with observeFrontAndNRPTS 110 bool mWriteTsValid; // whether mWriteTs is valid 111 struct timespec mWriteTs; // time that the previous write() completed 112 size_t mSetpoint; // target value for pipe fill depth 113 const bool mWriteCanBlock; // whether write() should block if the pipe is full 114 115 int64_t offsetTimestampByAudioFrames(int64_t ts, size_t audFrames); 116 LinearTransform mSamplesToLocalTime; 117}; 118 119} // namespace android 120 121#endif // ANDROID_AUDIO_MONO_PIPE_H 122