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>
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 @@
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 @@
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);