blob: 58b9750142ee163f2c253f7e95e370850ac1d242 [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_PIPE_H
18#define ANDROID_AUDIO_PIPE_H
19
Glenn Kastened99c2b2016-12-12 08:31:24 -080020#include <audio_utils/fifo.h>
Glenn Kasten01066232012-02-27 11:50:44 -080021#include "NBAIO.h"
22
23namespace android {
24
25// Pipe is multi-thread safe for readers (see PipeReader), but safe for only a single writer thread.
26// It cannot UNDERRUN on write, unless we allow designation of a master reader that provides the
27// time-base. Readers can be added and removed dynamically, and it's OK to have no readers.
28class Pipe : public NBAIO_Sink {
29
30 friend class PipeReader;
31
32public:
33 // maxFrames will be rounded up to a power of 2, and all slots are available. Must be >= 2.
Glenn Kastenc26d9232014-05-09 13:37:29 -070034 // buffer is an optional parameter specifying the virtual address of the pipe buffer,
35 // which must be of size roundup(maxFrames) * Format_frameSize(format) bytes.
36 Pipe(size_t maxFrames, const NBAIO_Format& format, void *buffer = NULL);
37
38 // If a buffer was specified in the constructor, it is not automatically freed by destructor.
Glenn Kasten01066232012-02-27 11:50:44 -080039 virtual ~Pipe();
40
41 // NBAIO_Port interface
42
43 //virtual ssize_t negotiate(const NBAIO_Format offers[], size_t numOffers,
44 // NBAIO_Format counterOffers[], size_t& numCounterOffers);
45 //virtual NBAIO_Format format() const;
46
47 // NBAIO_Sink interface
48
Andy Hung818e7a32016-02-16 18:08:07 -080049 //virtual int64_t framesWritten() const;
50 //virtual int64_t framesUnderrun() const;
51 //virtual int64_t underruns() const;
Glenn Kasten01066232012-02-27 11:50:44 -080052
53 // The write side of a pipe permits overruns; flow control is the caller's responsibility.
54 // It doesn't return +infinity because that would guarantee an overrun.
Glenn Kastened99c2b2016-12-12 08:31:24 -080055 virtual ssize_t availableToWrite() { return mMaxFrames; }
Glenn Kasten01066232012-02-27 11:50:44 -080056
57 virtual ssize_t write(const void *buffer, size_t count);
58 //virtual ssize_t writeVia(writeVia_t via, size_t total, void *user, size_t block);
59
60private:
61 const size_t mMaxFrames; // always a power of 2
62 void * const mBuffer;
Glenn Kastened99c2b2016-12-12 08:31:24 -080063 audio_utils_fifo mFifo;
64 audio_utils_fifo_writer mFifoWriter;
Glenn Kasten01066232012-02-27 11:50:44 -080065 volatile int32_t mReaders; // number of PipeReader clients currently attached to this Pipe
Glenn Kastenc26d9232014-05-09 13:37:29 -070066 const bool mFreeBufferInDestructor;
Glenn Kasten01066232012-02-27 11:50:44 -080067};
68
69} // namespace android
70
71#endif // ANDROID_AUDIO_PIPE_H