blob: 60ae92ea93e152657709e9ffcced809898799bc6 [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>
Glenn Kastened99c2b2016-12-12 08:31:24 -080021#include <audio_utils/fifo.h>
Glenn Kastena07a1c22013-08-23 10:54:35 -070022#include <media/SingleStateQueue.h>
Glenn Kastened99c2b2016-12-12 08:31:24 -080023#include "NBAIO.h"
Glenn Kasten01066232012-02-27 11:50:44 -080024
25namespace android {
26
Andy Hung818e7a32016-02-16 18:08:07 -080027typedef SingleStateQueue<ExtendedTimestamp> ExtendedTimestampSingleStateQueue;
Glenn Kastena07a1c22013-08-23 10:54:35 -070028
Glenn Kasten01066232012-02-27 11:50:44 -080029// MonoPipe is similar to Pipe except:
30// - supports only a single reader, called MonoPipeReader
31// - write() cannot overrun; instead it will return a short actual count if insufficient space
32// - write() can optionally block if the pipe is full
33// Like Pipe, it is not multi-thread safe for either writer or reader
34// but writer and reader can be different threads.
35class MonoPipe : public NBAIO_Sink {
36
37 friend class MonoPipeReader;
38
39public:
Glenn Kasten820ba702012-05-30 16:34:17 -070040 // 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 -080041 // Note: whatever shares this object with another thread needs to do so in an SMP-safe way (like
42 // creating it the object before creating the other thread, or storing the object with a
43 // release_store). Otherwise the other thread could see a partially-constructed object.
Glenn Kasten72e54af2014-01-31 09:37:35 -080044 MonoPipe(size_t reqFrames, const NBAIO_Format& format, bool writeCanBlock = false);
Glenn Kasten01066232012-02-27 11:50:44 -080045 virtual ~MonoPipe();
46
47 // NBAIO_Port interface
48
49 //virtual ssize_t negotiate(const NBAIO_Format offers[], size_t numOffers,
50 // NBAIO_Format counterOffers[], size_t& numCounterOffers);
51 //virtual NBAIO_Format format() const;
52
53 // NBAIO_Sink interface
54
Andy Hung818e7a32016-02-16 18:08:07 -080055 //virtual int64_t framesWritten() const;
56 //virtual int64_t framesUnderrun() const;
57 //virtual int64_t underruns() const;
Glenn Kasten01066232012-02-27 11:50:44 -080058
Glenn Kastened99c2b2016-12-12 08:31:24 -080059 // returns n where 0 <= n <= mMaxFrames, or a negative status_t
60 // including the private status codes in NBAIO.h
61 virtual ssize_t availableToWrite();
62
Glenn Kasten01066232012-02-27 11:50:44 -080063 virtual ssize_t write(const void *buffer, size_t count);
64 //virtual ssize_t writeVia(writeVia_t via, size_t total, void *user, size_t block);
65
Eric Laurente737cda2012-05-22 18:55:44 -070066 // average number of frames present in the pipe under normal conditions.
67 // See throttling mechanism in MonoPipe::write()
Glenn Kasten28ed2f92012-06-07 10:17:54 -070068 size_t getAvgFrames() const { return mSetpoint; }
69 void setAvgFrames(size_t setpoint);
70 size_t maxFrames() const { return mMaxFrames; }
Eric Laurente737cda2012-05-22 18:55:44 -070071
Glenn Kasten003d9f72012-09-28 12:22:52 -070072 // Set the shutdown state for the write side of a pipe.
73 // This may be called by an unrelated thread. When shutdown state is 'true',
74 // a write that would otherwise block instead returns a short transfer count.
75 // There is no guarantee how long it will take for the shutdown to be recognized,
76 // but it will not be an unbounded amount of time.
77 // The state can be restored to normal by calling shutdown(false).
78 void shutdown(bool newState = true);
79
80 // Return true if the write side of a pipe is currently shutdown.
81 bool isShutdown();
82
Glenn Kasten767094d2013-08-23 13:51:43 -070083 // Return NO_ERROR if there is a timestamp available
Andy Hung818e7a32016-02-16 18:08:07 -080084 status_t getTimestamp(ExtendedTimestamp &timestamp);
Glenn Kasten767094d2013-08-23 13:51:43 -070085
Glenn Kasten01066232012-02-27 11:50:44 -080086private:
Glenn Kastened99c2b2016-12-12 08:31:24 -080087 const size_t mMaxFrames; // as requested in constructor, rounded up to a power of 2
Glenn Kasten01066232012-02-27 11:50:44 -080088 void * const mBuffer;
Glenn Kastened99c2b2016-12-12 08:31:24 -080089 audio_utils_fifo mFifo;
90 audio_utils_fifo_writer mFifoWriter;
Glenn Kasten28ed2f92012-06-07 10:17:54 -070091 bool mWriteTsValid; // whether mWriteTs is valid
92 struct timespec mWriteTs; // time that the previous write() completed
93 size_t mSetpoint; // target value for pipe fill depth
Glenn Kasten01066232012-02-27 11:50:44 -080094 const bool mWriteCanBlock; // whether write() should block if the pipe is full
John Grossman2c3b2da2012-08-02 17:08:54 -070095
Glenn Kasten003d9f72012-09-28 12:22:52 -070096 bool mIsShutdown; // whether shutdown(true) was called, no barriers are needed
Glenn Kastena07a1c22013-08-23 10:54:35 -070097
Andy Hung818e7a32016-02-16 18:08:07 -080098 ExtendedTimestampSingleStateQueue::Shared mTimestampShared;
99 ExtendedTimestampSingleStateQueue::Mutator mTimestampMutator;
100 ExtendedTimestampSingleStateQueue::Observer mTimestampObserver;
Glenn Kasten01066232012-02-27 11:50:44 -0800101};
102
103} // namespace android
104
105#endif // ANDROID_AUDIO_MONO_PIPE_H