blob: d2cd21876c511681208f608e37f3678c587dab85 [file] [log] [blame]
Glenn Kasten01066232012-02-27 11:50:44 -08001/*
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"
Glenn Kastena07a1c22013-08-23 10:54:35 -070022#include <media/SingleStateQueue.h>
Glenn Kasten01066232012-02-27 11:50:44 -080023
24namespace android {
25
Andy Hung818e7a32016-02-16 18:08:07 -080026typedef SingleStateQueue<ExtendedTimestamp> ExtendedTimestampSingleStateQueue;
Glenn Kastena07a1c22013-08-23 10:54:35 -070027
Glenn Kasten01066232012-02-27 11:50:44 -080028// 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:
Glenn Kasten820ba702012-05-30 16:34:17 -070039 // reqFrames will be rounded up to a power of 2, and all slots are available. Must be >= 2.
Glenn Kasten01066232012-02-27 11:50:44 -080040 // 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.
Glenn Kasten72e54af2014-01-31 09:37:35 -080043 MonoPipe(size_t reqFrames, const NBAIO_Format& format, bool writeCanBlock = false);
Glenn Kasten01066232012-02-27 11:50:44 -080044 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
Andy Hung818e7a32016-02-16 18:08:07 -080054 //virtual int64_t framesWritten() const;
55 //virtual int64_t framesUnderrun() const;
56 //virtual int64_t underruns() const;
Glenn Kasten01066232012-02-27 11:50:44 -080057
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
Eric Laurente737cda2012-05-22 18:55:44 -070062 // average number of frames present in the pipe under normal conditions.
63 // See throttling mechanism in MonoPipe::write()
Glenn Kasten28ed2f92012-06-07 10:17:54 -070064 size_t getAvgFrames() const { return mSetpoint; }
65 void setAvgFrames(size_t setpoint);
66 size_t maxFrames() const { return mMaxFrames; }
Eric Laurente737cda2012-05-22 18:55:44 -070067
Glenn Kasten003d9f72012-09-28 12:22:52 -070068 // 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
Glenn Kasten767094d2013-08-23 13:51:43 -070079 // Return NO_ERROR if there is a timestamp available
Andy Hung818e7a32016-02-16 18:08:07 -080080 status_t getTimestamp(ExtendedTimestamp &timestamp);
Glenn Kasten767094d2013-08-23 13:51:43 -070081
Glenn Kasten01066232012-02-27 11:50:44 -080082private:
Glenn Kasten820ba702012-05-30 16:34:17 -070083 const size_t mReqFrames; // as requested in constructor, unrounded
Glenn Kasten01066232012-02-27 11:50:44 -080084 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.
Glenn Kastend79072e2016-01-06 08:41:20 -080089 volatile int32_t mFront; // written by reader with android_atomic_release_store,
90 // read by writer with android_atomic_acquire_load
Glenn Kasten01066232012-02-27 11:50:44 -080091 volatile int32_t mRear; // written by writer with android_atomic_release_store,
92 // read by reader with android_atomic_acquire_load
Glenn Kasten28ed2f92012-06-07 10:17:54 -070093 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
Glenn Kasten01066232012-02-27 11:50:44 -080096 const bool mWriteCanBlock; // whether write() should block if the pipe is full
John Grossman2c3b2da2012-08-02 17:08:54 -070097
Glenn Kasten003d9f72012-09-28 12:22:52 -070098 bool mIsShutdown; // whether shutdown(true) was called, no barriers are needed
Glenn Kastena07a1c22013-08-23 10:54:35 -070099
Andy Hung818e7a32016-02-16 18:08:07 -0800100 ExtendedTimestampSingleStateQueue::Shared mTimestampShared;
101 ExtendedTimestampSingleStateQueue::Mutator mTimestampMutator;
102 ExtendedTimestampSingleStateQueue::Observer mTimestampObserver;
Glenn Kasten01066232012-02-27 11:50:44 -0800103};
104
105} // namespace android
106
107#endif // ANDROID_AUDIO_MONO_PIPE_H