| 1 | /*
 | 
|---|
| 2 |  * Copyright (c) 2013 Jan Vesely
 | 
|---|
| 3 |  * All rights reserved.
 | 
|---|
| 4 |  *
 | 
|---|
| 5 |  * Redistribution and use in source and binary forms, with or without
 | 
|---|
| 6 |  * modification, are permitted provided that the following conditions
 | 
|---|
| 7 |  * are met:
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  * - Redistributions of source code must retain the above copyright
 | 
|---|
| 10 |  *   notice, this list of conditions and the following disclaimer.
 | 
|---|
| 11 |  * - Redistributions in binary form must reproduce the above copyright
 | 
|---|
| 12 |  *   notice, this list of conditions and the following disclaimer in the
 | 
|---|
| 13 |  *   documentation and/or other materials provided with the distribution.
 | 
|---|
| 14 |  * - The name of the author may not be used to endorse or promote products
 | 
|---|
| 15 |  *   derived from this software without specific prior written permission.
 | 
|---|
| 16 |  *
 | 
|---|
| 17 |  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
|---|
| 18 |  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
|---|
| 19 |  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
|---|
| 20 |  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
|---|
| 21 |  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
|---|
| 22 |  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
|---|
| 23 |  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
|---|
| 24 |  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
|---|
| 25 |  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
|---|
| 26 |  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
|---|
| 27 |  */
 | 
|---|
| 28 | 
 | 
|---|
| 29 | /** @addtogroup audio
 | 
|---|
| 30 |  * @brief HelenOS sound server
 | 
|---|
| 31 |  * @{
 | 
|---|
| 32 |  */
 | 
|---|
| 33 | /** @file
 | 
|---|
| 34 |  */
 | 
|---|
| 35 | 
 | 
|---|
| 36 | #ifndef AUDIO_DATA_H_
 | 
|---|
| 37 | #define AUDIO_DATA_H_
 | 
|---|
| 38 | 
 | 
|---|
| 39 | #include <adt/list.h>
 | 
|---|
| 40 | #include <refcount.h>
 | 
|---|
| 41 | #include <errno.h>
 | 
|---|
| 42 | #include <fibril_synch.h>
 | 
|---|
| 43 | #include <pcm/format.h>
 | 
|---|
| 44 | 
 | 
|---|
| 45 | /** Reference counted audio buffer */
 | 
|---|
| 46 | typedef struct {
 | 
|---|
| 47 |         /** Audio data */
 | 
|---|
| 48 |         void *data;
 | 
|---|
| 49 |         /** Size of the buffer pointer to by data */
 | 
|---|
| 50 |         size_t size;
 | 
|---|
| 51 |         /** Format of the audio data */
 | 
|---|
| 52 |         pcm_format_t format;
 | 
|---|
| 53 |         /** Reference counter */
 | 
|---|
| 54 |         atomic_refcount_t refcount;
 | 
|---|
| 55 | } audio_data_t;
 | 
|---|
| 56 | 
 | 
|---|
| 57 | /** Audio data pipe structure */
 | 
|---|
| 58 | typedef struct {
 | 
|---|
| 59 |         /** List of audio data buffers */
 | 
|---|
| 60 |         list_t list;
 | 
|---|
| 61 |         /** Total size of all buffers */
 | 
|---|
| 62 |         size_t bytes;
 | 
|---|
| 63 |         /** Total frames stored in all buffers */
 | 
|---|
| 64 |         size_t frames;
 | 
|---|
| 65 |         /** List access synchronization */
 | 
|---|
| 66 |         fibril_mutex_t guard;
 | 
|---|
| 67 | } audio_pipe_t;
 | 
|---|
| 68 | 
 | 
|---|
| 69 | audio_data_t *audio_data_create(void *data, size_t size,
 | 
|---|
| 70 |     pcm_format_t format);
 | 
|---|
| 71 | void audio_data_addref(audio_data_t *adata);
 | 
|---|
| 72 | void audio_data_unref(audio_data_t *adata);
 | 
|---|
| 73 | 
 | 
|---|
| 74 | void audio_pipe_init(audio_pipe_t *pipe);
 | 
|---|
| 75 | void audio_pipe_fini(audio_pipe_t *pipe);
 | 
|---|
| 76 | 
 | 
|---|
| 77 | errno_t audio_pipe_push(audio_pipe_t *pipe, audio_data_t *data);
 | 
|---|
| 78 | audio_data_t *audio_pipe_pop(audio_pipe_t *pipe);
 | 
|---|
| 79 | 
 | 
|---|
| 80 | size_t audio_pipe_mix_data(audio_pipe_t *pipe, void *buffer, size_t size,
 | 
|---|
| 81 |     const pcm_format_t *f);
 | 
|---|
| 82 | 
 | 
|---|
| 83 | /**
 | 
|---|
| 84 |  * Total bytes getter.
 | 
|---|
| 85 |  * @param pipe The audio pipe.
 | 
|---|
| 86 |  * @return Total size of buffer stored in the pipe.
 | 
|---|
| 87 |  */
 | 
|---|
| 88 | static inline size_t audio_pipe_bytes(audio_pipe_t *pipe)
 | 
|---|
| 89 | {
 | 
|---|
| 90 |         assert(pipe);
 | 
|---|
| 91 |         return pipe->bytes;
 | 
|---|
| 92 | }
 | 
|---|
| 93 | 
 | 
|---|
| 94 | /**
 | 
|---|
| 95 |  * Total bytes getter.
 | 
|---|
| 96 |  * @param pipe The audio pipe.
 | 
|---|
| 97 |  * @return Total number of frames stored in the pipe.
 | 
|---|
| 98 |  */
 | 
|---|
| 99 | static inline size_t audio_pipe_frames(audio_pipe_t *pipe)
 | 
|---|
| 100 | {
 | 
|---|
| 101 |         assert(pipe);
 | 
|---|
| 102 |         return pipe->frames;
 | 
|---|
| 103 | }
 | 
|---|
| 104 | 
 | 
|---|
| 105 | /**
 | 
|---|
| 106 |  * Push data form buffer directly to pipe.
 | 
|---|
| 107 |  * @param pipe The target pipe.
 | 
|---|
| 108 |  * @param data audio buffer.
 | 
|---|
| 109 |  * @param size size of the @p data buffer
 | 
|---|
| 110 |  * @param f format of the audio data.
 | 
|---|
| 111 |  * @return Error code.
 | 
|---|
| 112 |  *
 | 
|---|
| 113 |  * Reference counted buffer is created automatically.
 | 
|---|
| 114 |  */
 | 
|---|
| 115 | static inline errno_t audio_pipe_push_data(audio_pipe_t *pipe,
 | 
|---|
| 116 |     void *data, size_t size, pcm_format_t f)
 | 
|---|
| 117 | {
 | 
|---|
| 118 |         audio_data_t *adata = audio_data_create(data, size, f);
 | 
|---|
| 119 |         if (adata) {
 | 
|---|
| 120 |                 const errno_t ret = audio_pipe_push(pipe, adata);
 | 
|---|
| 121 |                 audio_data_unref(adata);
 | 
|---|
| 122 |                 return ret;
 | 
|---|
| 123 |         }
 | 
|---|
| 124 |         return ENOMEM;
 | 
|---|
| 125 | }
 | 
|---|
| 126 | 
 | 
|---|
| 127 | 
 | 
|---|
| 128 | #endif
 | 
|---|
| 129 | 
 | 
|---|
| 130 | /**
 | 
|---|
| 131 |  * @}
 | 
|---|
| 132 |  */
 | 
|---|