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#include <media/SingleStateQueue.h>
23
24namespace android {
25
26typedef SingleStateQueue<ExtendedTimestamp> ExtendedTimestampSingleStateQueue;
27
28// MonoPipe is similar to Pipe except:
29//  - supports only a single reader, called MonoPipeReader
30//  - write() cannot overrun; instead it will return a short actual count if insufficient space
31//  - write() can optionally block if the pipe is full
32// Like Pipe, it is not multi-thread safe for either writer or reader
33// but writer and reader can be different threads.
34class MonoPipe : public NBAIO_Sink {
35
36    friend class MonoPipeReader;
37
38public:
39    // reqFrames will be rounded up to a power of 2, and all slots are available. Must be >= 2.
40    // Note: whatever shares this object with another thread needs to do so in an SMP-safe way (like
41    // creating it the object before creating the other thread, or storing the object with a
42    // release_store). Otherwise the other thread could see a partially-constructed object.
43    MonoPipe(size_t reqFrames, const NBAIO_Format& format, bool writeCanBlock = false);
44    virtual ~MonoPipe();
45
46    // NBAIO_Port interface
47
48    //virtual ssize_t negotiate(const NBAIO_Format offers[], size_t numOffers,
49    //                          NBAIO_Format counterOffers[], size_t& numCounterOffers);
50    //virtual NBAIO_Format format() const;
51
52    // NBAIO_Sink interface
53
54    //virtual int64_t framesWritten() const;
55    //virtual int64_t framesUnderrun() const;
56    //virtual int64_t underruns() const;
57
58    virtual ssize_t availableToWrite() const;
59    virtual ssize_t write(const void *buffer, size_t count);
60    //virtual ssize_t writeVia(writeVia_t via, size_t total, void *user, size_t block);
61
62            // average number of frames present in the pipe under normal conditions.
63            // See throttling mechanism in MonoPipe::write()
64            size_t  getAvgFrames() const { return mSetpoint; }
65            void    setAvgFrames(size_t setpoint);
66            size_t  maxFrames() const { return mMaxFrames; }
67
68            // Set the shutdown state for the write side of a pipe.
69            // This may be called by an unrelated thread.  When shutdown state is 'true',
70            // a write that would otherwise block instead returns a short transfer count.
71            // There is no guarantee how long it will take for the shutdown to be recognized,
72            // but it will not be an unbounded amount of time.
73            // The state can be restored to normal by calling shutdown(false).
74            void    shutdown(bool newState = true);
75
76            // Return true if the write side of a pipe is currently shutdown.
77            bool    isShutdown();
78
79            // Return NO_ERROR if there is a timestamp available
80            status_t getTimestamp(ExtendedTimestamp &timestamp);
81
82private:
83    const size_t    mReqFrames;     // as requested in constructor, unrounded
84    const size_t    mMaxFrames;     // always a power of 2
85    void * const    mBuffer;
86    // mFront and mRear will never be separated by more than mMaxFrames.
87    // 32-bit overflow is possible if the pipe is active for a long time, but if that happens it's
88    // safe because we "&" with (mMaxFrames-1) at end of computations to calculate a buffer index.
89    volatile int32_t mFront;        // written by reader with android_atomic_release_store,
90                                    // read by writer with android_atomic_acquire_load
91    volatile int32_t mRear;         // written by writer with android_atomic_release_store,
92                                    // read by reader with android_atomic_acquire_load
93    bool            mWriteTsValid;  // whether mWriteTs is valid
94    struct timespec mWriteTs;       // time that the previous write() completed
95    size_t          mSetpoint;      // target value for pipe fill depth
96    const bool      mWriteCanBlock; // whether write() should block if the pipe is full
97
98    bool            mIsShutdown;    // whether shutdown(true) was called, no barriers are needed
99
100    ExtendedTimestampSingleStateQueue::Shared      mTimestampShared;
101    ExtendedTimestampSingleStateQueue::Mutator     mTimestampMutator;
102    ExtendedTimestampSingleStateQueue::Observer    mTimestampObserver;
103};
104
105}   // namespace android
106
107#endif  // ANDROID_AUDIO_MONO_PIPE_H
108