blob: 45e6bb46b12cf3049cdc6e37afc0084f85b308f5 [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"
22
23namespace android {
24
25// MonoPipe is similar to Pipe except:
26// - supports only a single reader, called MonoPipeReader
27// - write() cannot overrun; instead it will return a short actual count if insufficient space
28// - write() can optionally block if the pipe is full
29// Like Pipe, it is not multi-thread safe for either writer or reader
30// but writer and reader can be different threads.
31class MonoPipe : public NBAIO_Sink {
32
33 friend class MonoPipeReader;
34
35public:
36 // maxFrames will be rounded up to a power of 2, and all slots are available. Must be >= 2.
37 // Note: whatever shares this object with another thread needs to do so in an SMP-safe way (like
38 // creating it the object before creating the other thread, or storing the object with a
39 // release_store). Otherwise the other thread could see a partially-constructed object.
40 MonoPipe(size_t maxFrames, NBAIO_Format format, bool writeCanBlock = false);
41 virtual ~MonoPipe();
42
43 // NBAIO_Port interface
44
45 //virtual ssize_t negotiate(const NBAIO_Format offers[], size_t numOffers,
46 // NBAIO_Format counterOffers[], size_t& numCounterOffers);
47 //virtual NBAIO_Format format() const;
48
49 // NBAIO_Sink interface
50
51 //virtual size_t framesWritten() const;
52 //virtual size_t framesUnderrun() const;
53 //virtual size_t underruns() const;
54
55 virtual ssize_t availableToWrite() const;
56 virtual ssize_t write(const void *buffer, size_t count);
57 //virtual ssize_t writeVia(writeVia_t via, size_t total, void *user, size_t block);
58
59private:
60 const size_t mMaxFrames; // always a power of 2
61 void * const mBuffer;
62 // mFront and mRear will never be separated by more than mMaxFrames.
63 // 32-bit overflow is possible if the pipe is active for a long time, but if that happens it's
64 // safe because we "&" with (mMaxFrames-1) at end of computations to calculate a buffer index.
65 volatile int32_t mFront; // written by reader with android_atomic_release_store,
66 // read by writer with android_atomic_acquire_load
67 volatile int32_t mRear; // written by writer with android_atomic_release_store,
68 // read by reader with android_atomic_acquire_load
69 const bool mWriteCanBlock; // whether write() should block if the pipe is full
70 struct timespec mSleep; // time to sleep if blocking is enabled and the pipe is full
71};
72
73} // namespace android
74
75#endif // ANDROID_AUDIO_MONO_PIPE_H