summaryrefslogtreecommitdiff
path: root/nuttx/audio/buffer.c
diff options
context:
space:
mode:
authorGregory Nutt <gnutt@nuttx.org>2013-10-27 07:44:53 -0600
committerGregory Nutt <gnutt@nuttx.org>2013-10-27 07:44:53 -0600
commit66bc3094e689ff4a96c9ac2755335d45b8765aa1 (patch)
treea25e3cf4f1a61bcd727587fefc2725316c82ee05 /nuttx/audio/buffer.c
parent6dc974b4b0d8423cc9073e425a2e3a4370160df6 (diff)
downloadpx4-nuttx-66bc3094e689ff4a96c9ac2755335d45b8765aa1.tar.gz
px4-nuttx-66bc3094e689ff4a96c9ac2755335d45b8765aa1.tar.bz2
px4-nuttx-66bc3094e689ff4a96c9ac2755335d45b8765aa1.zip
Updated audio subsystem from Ken Pettit
Diffstat (limited to 'nuttx/audio/buffer.c')
-rw-r--r--nuttx/audio/buffer.c59
1 files changed, 52 insertions, 7 deletions
diff --git a/nuttx/audio/buffer.c b/nuttx/audio/buffer.c
index 3906891f6..b0b5c6c36 100644
--- a/nuttx/audio/buffer.c
+++ b/nuttx/audio/buffer.c
@@ -84,11 +84,11 @@
*
****************************************************************************/
-static void apb_semtake(sem_t *sem)
+static void apb_semtake(FAR struct ap_buffer_s *apb)
{
/* Take the semaphore (perhaps waiting) */
- while (sem_wait(sem) != 0)
+ while (sem_wait(&apb->sem) != 0)
{
/* The only case that an error should occr here is if
* the wait was awakened by a signal.
@@ -102,7 +102,7 @@ static void apb_semtake(sem_t *sem)
* Name: apb_semgive
****************************************************************************/
-#define apb_semgive(s) sem_post(s)
+#define apb_semgive(b) sem_post(&b->sem)
/****************************************************************************
* Name: apb_alloc
@@ -113,11 +113,41 @@ static void apb_semtake(sem_t *sem)
*
****************************************************************************/
-FAR struct ap_buffer_s *apb_alloc(int type, int sampleCount)
+int apb_alloc(FAR struct audio_buf_desc_s * bufdesc)
{
- /* TODO: Implement the alloc logic */
+ uint32_t bufsize;
+ int ret;
+ struct ap_buffer_s *pBuf;
- return NULL;
+ DEBUGASSERT(bufdesc->u.ppBuffer != NULL);
+
+ /* Perform a user mode allocation */
+
+ bufsize = sizeof(struct ap_buffer_s) + bufdesc->numbytes;
+ pBuf = kumalloc(bufsize);
+ *bufdesc->u.ppBuffer = pBuf;
+
+ /* Test if the allocation was successful or not */
+
+ if (*bufdesc->u.ppBuffer == NULL)
+ ret = -ENOMEM;
+ else
+ {
+ /* Populate the buffer contents */
+
+ memset(pBuf, bufsize, 0);
+ pBuf->i.channels = 1;
+ pBuf->crefs = 1;
+ pBuf->nmaxbytes = bufdesc->numbytes;
+ pBuf->nbytes = 0;
+#ifdef CONFIG_AUDIO_MULTI_SESSION
+ pBuf->session = bufdesc->session;
+#endif
+ sem_init(&pBuf->sem, 0, 1);
+ ret = sizeof(struct audio_buf_desc_s);
+ }
+
+ return ret;
}
/****************************************************************************
@@ -143,8 +173,19 @@ void apb_prepare(FAR struct ap_buffer_s *apb, int8_t allocmode, uint8_t format,
void apb_free(FAR struct ap_buffer_s *apb)
{
+ int refcount;
+
/* Perform a reference count decrement and possibly release the memory */
+ apb_semtake(apb);
+ refcount = apb->crefs--;
+ apb_semgive(apb);
+
+ if (refcount == 1)
+ {
+ auddbg("Freeing %p\n", apb);
+ kufree(apb);
+ }
}
/****************************************************************************
@@ -158,7 +199,11 @@ void apb_free(FAR struct ap_buffer_s *apb)
void apb_reference(FAR struct ap_buffer_s *apb)
{
- /* TODO: Implement the reference logic */
+ /* Do we need any thread protection here? Almost certaily... */
+
+ apb_semtake(apb);
+ apb->crefs++;
+ apb_semgive(apb);
}
#endif /* CONFIG_AUDIO */