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