2013-05-19 23:12:28 +02:00
|
|
|
/****************************************************************************
|
2013-11-10 18:20:06 +01:00
|
|
|
* libc/audio/lib_buffer.c
|
2013-05-19 23:12:28 +02:00
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Ken Pettit. All rights reserved.
|
|
|
|
* Author: Ken Pettit <pettitkd@gmail.com>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2013-05-21 21:13:05 +02:00
|
|
|
#include <nuttx/audio/audio.h>
|
2013-05-19 23:12:28 +02:00
|
|
|
#include <nuttx/usb/audio.h>
|
|
|
|
|
2015-12-30 00:31:17 +01:00
|
|
|
#include "libc.h"
|
2013-11-10 19:22:01 +01:00
|
|
|
|
2013-05-19 23:12:28 +02:00
|
|
|
#if defined(CONFIG_AUDIO)
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-04-08 15:32:08 +02:00
|
|
|
* Pre-processor Definitions
|
2013-05-19 23:12:28 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Configuration ************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Variables
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-03 00:30:35 +02:00
|
|
|
* Public Data
|
2013-05-19 23:12:28 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: apb_semtake
|
|
|
|
*
|
|
|
|
* Take an Audio Pipeline Buffer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-10-27 14:44:53 +01:00
|
|
|
static void apb_semtake(FAR struct ap_buffer_s *apb)
|
2013-05-19 23:12:28 +02:00
|
|
|
{
|
|
|
|
/* Take the semaphore (perhaps waiting) */
|
|
|
|
|
2013-10-27 14:44:53 +01:00
|
|
|
while (sem_wait(&apb->sem) != 0)
|
2013-05-19 23:12:28 +02:00
|
|
|
{
|
|
|
|
/* The only case that an error should occr here is if
|
|
|
|
* the wait was awakened by a signal.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ASSERT(errno == EINTR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: apb_semgive
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-10-27 14:44:53 +01:00
|
|
|
#define apb_semgive(b) sem_post(&b->sem)
|
2013-05-19 23:12:28 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: apb_alloc
|
|
|
|
*
|
|
|
|
* Allocate an Audio Pipeline Buffer for use in the Audio sub-system. This
|
|
|
|
* will perform the actual allocate based on buffer data format, number of
|
|
|
|
* channels, etc. and prepare the buffer for consumption.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-11-10 19:22:01 +01:00
|
|
|
int apb_alloc(FAR struct audio_buf_desc_s *bufdesc)
|
2013-05-19 23:12:28 +02:00
|
|
|
{
|
2013-10-27 14:44:53 +01:00
|
|
|
uint32_t bufsize;
|
|
|
|
int ret;
|
2014-08-01 00:36:09 +02:00
|
|
|
struct ap_buffer_s *apb;
|
2013-05-19 23:12:28 +02:00
|
|
|
|
2013-10-27 14:44:53 +01:00
|
|
|
DEBUGASSERT(bufdesc->u.ppBuffer != NULL);
|
|
|
|
|
|
|
|
/* Perform a user mode allocation */
|
|
|
|
|
|
|
|
bufsize = sizeof(struct ap_buffer_s) + bufdesc->numbytes;
|
2014-08-01 00:36:09 +02:00
|
|
|
apb = lib_umalloc(bufsize);
|
|
|
|
*bufdesc->u.ppBuffer = apb;
|
2013-10-27 14:44:53 +01:00
|
|
|
|
|
|
|
/* Test if the allocation was successful or not */
|
|
|
|
|
|
|
|
if (*bufdesc->u.ppBuffer == NULL)
|
2013-11-10 19:22:01 +01:00
|
|
|
{
|
|
|
|
ret = -ENOMEM;
|
|
|
|
}
|
2013-10-27 14:44:53 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Populate the buffer contents */
|
|
|
|
|
2014-08-01 00:36:09 +02:00
|
|
|
memset(apb, 0, bufsize);
|
|
|
|
apb->i.channels = 1;
|
|
|
|
apb->crefs = 1;
|
|
|
|
apb->nmaxbytes = bufdesc->numbytes;
|
|
|
|
apb->nbytes = 0;
|
|
|
|
apb->flags = 0;
|
2013-10-27 14:44:53 +01:00
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
2014-08-01 00:36:09 +02:00
|
|
|
apb->session = bufdesc->session;
|
2013-10-27 14:44:53 +01:00
|
|
|
#endif
|
2014-08-01 00:36:09 +02:00
|
|
|
|
|
|
|
sem_init(&apb->sem, 0, 1);
|
2013-10-27 14:44:53 +01:00
|
|
|
ret = sizeof(struct audio_buf_desc_s);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2013-05-19 23:12:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: apb_free
|
|
|
|
*
|
|
|
|
* Free's a previously allocated or referenced Audio Pipeline Buffer
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void apb_free(FAR struct ap_buffer_s *apb)
|
|
|
|
{
|
2013-11-10 19:22:01 +01:00
|
|
|
int refcount;
|
2013-10-27 14:44:53 +01:00
|
|
|
|
2013-05-19 23:12:28 +02:00
|
|
|
/* Perform a reference count decrement and possibly release the memory */
|
|
|
|
|
2013-10-27 14:44:53 +01:00
|
|
|
apb_semtake(apb);
|
|
|
|
refcount = apb->crefs--;
|
|
|
|
apb_semgive(apb);
|
|
|
|
|
2013-11-10 19:22:01 +01:00
|
|
|
if (refcount <= 1)
|
2013-10-27 14:44:53 +01:00
|
|
|
{
|
2014-08-08 19:02:45 +02:00
|
|
|
audvdbg("Freeing %p\n", apb);
|
2013-11-10 19:22:01 +01:00
|
|
|
lib_ufree(apb);
|
2013-10-27 14:44:53 +01:00
|
|
|
}
|
2013-05-19 23:12:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: apb_reference
|
|
|
|
*
|
|
|
|
* Claim a reference to an Audio Pipeline Buffer. Each call to apb_reference
|
|
|
|
* will increment the reference count and must have a matching apb_free
|
|
|
|
* call. When the refcount decrements to zero, the buffer will be freed.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void apb_reference(FAR struct ap_buffer_s *apb)
|
|
|
|
{
|
2013-10-27 14:44:53 +01:00
|
|
|
/* Do we need any thread protection here? Almost certaily... */
|
|
|
|
|
|
|
|
apb_semtake(apb);
|
|
|
|
apb->crefs++;
|
|
|
|
apb_semgive(apb);
|
2013-05-19 23:12:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_AUDIO */
|