• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
22 
23 namespace android {
24 
25 // MonoPipe is similar to Pipe except:
26 //  - supports only a single reader, called MonoPipeReader
27 //  - write() cannot overrun; instead it will return a short actual count if insufficient space
28 //  - write() can optionally block if the pipe is full
29 // Like Pipe, it is not multi-thread safe for either writer or reader
30 // but writer and reader can be different threads.
31 class MonoPipe : public NBAIO_Sink {
32 
33     friend class MonoPipeReader;
34 
35 public:
36     // reqFrames will be rounded up to a power of 2, and all slots are available. Must be >= 2.
37     // Note: whatever shares this object with another thread needs to do so in an SMP-safe way (like
38     // creating it the object before creating the other thread, or storing the object with a
39     // release_store). Otherwise the other thread could see a partially-constructed object.
40     MonoPipe(size_t reqFrames, NBAIO_Format format, bool writeCanBlock = false);
41     virtual ~MonoPipe();
42 
43     // NBAIO_Port interface
44 
45     //virtual ssize_t negotiate(const NBAIO_Format offers[], size_t numOffers,
46     //                          NBAIO_Format counterOffers[], size_t& numCounterOffers);
47     //virtual NBAIO_Format format() const;
48 
49     // NBAIO_Sink interface
50 
51     //virtual size_t framesWritten() const;
52     //virtual size_t framesUnderrun() const;
53     //virtual size_t underruns() const;
54 
55     virtual ssize_t availableToWrite() const;
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             // average number of frames present in the pipe under normal conditions.
60             // See throttling mechanism in MonoPipe::write()
getAvgFrames()61             size_t  getAvgFrames() const { return mSetpoint; }
62             void    setAvgFrames(size_t setpoint);
maxFrames()63             size_t  maxFrames() const { return mMaxFrames; }
64 
65 private:
66     const size_t    mReqFrames;     // as requested in constructor, unrounded
67     const size_t    mMaxFrames;     // always a power of 2
68     void * const    mBuffer;
69     // mFront and mRear will never be separated by more than mMaxFrames.
70     // 32-bit overflow is possible if the pipe is active for a long time, but if that happens it's
71     // safe because we "&" with (mMaxFrames-1) at end of computations to calculate a buffer index.
72     volatile int32_t mFront;        // written by reader with android_atomic_release_store,
73                                     // read by writer with android_atomic_acquire_load
74     volatile int32_t mRear;         // written by writer with android_atomic_release_store,
75                                     // read by reader with android_atomic_acquire_load
76     bool            mWriteTsValid;  // whether mWriteTs is valid
77     struct timespec mWriteTs;       // time that the previous write() completed
78     size_t          mSetpoint;      // target value for pipe fill depth
79     const bool      mWriteCanBlock; // whether write() should block if the pipe is full
80 };
81 
82 }   // namespace android
83 
84 #endif  // ANDROID_AUDIO_MONO_PIPE_H
85