Pipe.h revision fc7992bd8220824f1404c0c54ac516d9e28b58c2
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
22namespace 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.
27class Pipe : public NBAIO_Sink {
28
29    friend class PipeReader;
30
31public:
32    // maxFrames will be rounded up to a power of 2, and all slots are available. Must be >= 2.
33    Pipe(size_t maxFrames, NBAIO_Format format);
34    virtual ~Pipe();
35
36    // NBAIO_Port interface
37
38    //virtual ssize_t negotiate(const NBAIO_Format offers[], size_t numOffers,
39    //                          NBAIO_Format counterOffers[], size_t& numCounterOffers);
40    //virtual NBAIO_Format format() const;
41
42    // NBAIO_Sink interface
43
44    //virtual size_t framesWritten() const;
45    //virtual size_t framesUnderrun() const;
46    //virtual size_t underruns() const;
47
48    // The write side of a pipe permits overruns; flow control is the caller's responsibility.
49    // It doesn't return +infinity because that would guarantee an overrun.
50    virtual ssize_t availableToWrite() const { return mMaxFrames; }
51
52    virtual ssize_t write(const void *buffer, size_t count);
53    //virtual ssize_t writeVia(writeVia_t via, size_t total, void *user, size_t block);
54
55private:
56    const size_t    mMaxFrames;     // always a power of 2
57    void * const    mBuffer;
58    volatile int32_t mRear;         // written by android_atomic_release_store
59    volatile int32_t mReaders;      // number of PipeReader clients currently attached to this Pipe
60};
61
62}   // namespace android
63
64#endif  // ANDROID_AUDIO_PIPE_H
65