diff options
author | Misael Lopez Cruz <misael.lopez@ti.com> | 2014-01-28 14:06:45 -0600 |
---|---|---|
committer | Misael Lopez Cruz <misael.lopez@ti.com> | 2014-01-30 17:05:44 -0600 |
commit | 7c835cd660854083860e2ee8793ab6b5dff84c21 (patch) | |
tree | 0387420e7e4dcf1677091e6779b2b7e97dd0daad | |
parent | 46be7175bc6fdbbfad4cb143a69f7417b7066c55 (diff) | |
download | common-open-7c835cd660854083860e2ee8793ab6b5dff84c21.tar.gz |
audio: utils: Add flush() and size() methods to MonoPipe
Add two new methods to the MonoPipe class:
- flush(): Flushes the audio frames available in the pipe.
It should be called when te pipe is already in shutdown
state to ensure that no more new data is pushed.
- size(): Queries the pipe size.
Change-Id: Ia8c2ac287a475bd3e72f48368d297fc544b19eed
Signed-off-by: Misael Lopez Cruz <misael.lopez@ti.com>
-rw-r--r-- | audio/utils/include/tiaudioutils/MonoPipe.h | 21 | ||||
-rw-r--r-- | audio/utils/src/MonoPipe.cpp | 28 |
2 files changed, 49 insertions, 0 deletions
diff --git a/audio/utils/include/tiaudioutils/MonoPipe.h b/audio/utils/include/tiaudioutils/MonoPipe.h index 7fcef8c..8445ddc 100644 --- a/audio/utils/include/tiaudioutils/MonoPipe.h +++ b/audio/utils/include/tiaudioutils/MonoPipe.h @@ -139,6 +139,27 @@ class MonoPipe { virtual int availableToWrite() const; /** + * \brief Query the size of the pipe + * + * Queries the size of the pipe. It might be different to the requested + * number of frames if the pipe implementation relies does size rounding. + * + * \return Size of the pipe (in frames) + */ + virtual size_t size() const; + + /** + * \brief Flush the pipe + * + * Flushes all frames present in the pipe. It should be called when the + * pipe is in shutdown state to ensure no new data is written to the + * pipe. + * + * \return 0 on success, otherwise negative error code + */ + virtual int flush(); + + /** * \brief Shut down the pipe * * Shuts down the pipe, causing any blocking write() calls to unblock diff --git a/audio/utils/src/MonoPipe.cpp b/audio/utils/src/MonoPipe.cpp index e463f79..be021f1 100644 --- a/audio/utils/src/MonoPipe.cpp +++ b/audio/utils/src/MonoPipe.cpp @@ -110,6 +110,34 @@ int MonoPipe::availableToWrite() const return mSink->availableToWrite(); } +size_t MonoPipe::size() const +{ + return mSink->maxFrames(); +} + +int MonoPipe::flush() +{ + ALOGW_IF(!isShutdown(), "MonoPipe: flushing while not in shutdown state"); + + int avail = availableToRead(); + int16_t buffer[mParams.channels * avail]; + + /* Read all frames still present in the pipe */ + while (avail > 0) { + int ret = read(buffer, avail); + if (ret < 0) { + ALOGE("MonoPipe: failed to flush the pipe %d", ret); + return ret; + } + avail -= ret; + } + + avail = availableToRead(); + ALOGW_IF(avail, "MonoPipe: %d frames were not flushed", avail); + + return 0; +} + void MonoPipe::shutdown(bool state) { mSink->shutdown(state); |