| [23878dc] | 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>
 | 
|---|
| [76ea1b7] | 40 | #include <atomic.h>
 | 
|---|
| [d988ef2] | 41 | #include <errno.h>
 | 
|---|
 | 42 | #include <fibril_synch.h>
 | 
|---|
 | 43 | #include <pcm/format.h>
 | 
|---|
| [23878dc] | 44 | 
 | 
|---|
| [c799138] | 45 | /** Reference counted audio buffer */
 | 
|---|
| [23878dc] | 46 | typedef struct {
 | 
|---|
| [c799138] | 47 |         /** Audio data */
 | 
|---|
| [23878dc] | 48 |         const void *data;
 | 
|---|
| [c799138] | 49 |         /** Size of the buffer pointer to by data */
 | 
|---|
| [23878dc] | 50 |         size_t size;
 | 
|---|
| [c799138] | 51 |         /** Format of the audio data */
 | 
|---|
| [5029c788] | 52 |         pcm_format_t format;
 | 
|---|
| [c799138] | 53 |         /** Reference counter */
 | 
|---|
| [76ea1b7] | 54 |         atomic_t refcount;
 | 
|---|
| [23878dc] | 55 | } audio_data_t;
 | 
|---|
 | 56 | 
 | 
|---|
| [c799138] | 57 | /** Audio data pipe structure */
 | 
|---|
| [d988ef2] | 58 | typedef struct {
 | 
|---|
| [c799138] | 59 |         /** List of audio data buffers */
 | 
|---|
| [d988ef2] | 60 |         list_t list;
 | 
|---|
| [c799138] | 61 |         /** Total size of all buffers */
 | 
|---|
| [d988ef2] | 62 |         size_t bytes;
 | 
|---|
| [c799138] | 63 |         /** Total frames stored in all buffers */
 | 
|---|
| [d988ef2] | 64 |         size_t frames;
 | 
|---|
| [c799138] | 65 |         /** List access synchronization */
 | 
|---|
| [d988ef2] | 66 |         fibril_mutex_t guard;
 | 
|---|
 | 67 | } audio_pipe_t;
 | 
|---|
 | 68 | 
 | 
|---|
| [5029c788] | 69 | audio_data_t * audio_data_create(const void *data, size_t size,
 | 
|---|
 | 70 |     pcm_format_t format);
 | 
|---|
| [d988ef2] | 71 | void audio_data_addref(audio_data_t *adata);
 | 
|---|
| [5029c788] | 72 | void audio_data_unref(audio_data_t *adata);
 | 
|---|
 | 73 | 
 | 
|---|
| [d988ef2] | 74 | void audio_pipe_init(audio_pipe_t *pipe);
 | 
|---|
 | 75 | void audio_pipe_fini(audio_pipe_t *pipe);
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 | int audio_pipe_push(audio_pipe_t *pipe, audio_data_t *data);
 | 
|---|
 | 78 | audio_data_t *audio_pipe_pop(audio_pipe_t *pipe);
 | 
|---|
 | 79 | 
 | 
|---|
 | 80 | ssize_t audio_pipe_mix_data(audio_pipe_t *pipe, void *buffer, size_t size,
 | 
|---|
 | 81 |     const pcm_format_t *f);
 | 
|---|
 | 82 | 
 | 
|---|
| [c799138] | 83 | /**
 | 
|---|
 | 84 |  * Total bytes getter.
 | 
|---|
 | 85 |  * @param pipe The audio pipe.
 | 
|---|
 | 86 |  * @return Total size of buffer stored in the pipe.
 | 
|---|
 | 87 |  */
 | 
|---|
| [d988ef2] | 88 | static inline size_t audio_pipe_bytes(audio_pipe_t *pipe)
 | 
|---|
 | 89 | {
 | 
|---|
 | 90 |         assert(pipe);
 | 
|---|
 | 91 |         return pipe->bytes;
 | 
|---|
 | 92 | }
 | 
|---|
 | 93 | 
 | 
|---|
| [c799138] | 94 | /**
 | 
|---|
 | 95 |  * Total bytes getter.
 | 
|---|
 | 96 |  * @param pipe The audio pipe.
 | 
|---|
 | 97 |  * @return Total number of frames stored in the pipe.
 | 
|---|
 | 98 |  */
 | 
|---|
| [d988ef2] | 99 | static inline size_t audio_pipe_frames(audio_pipe_t *pipe)
 | 
|---|
 | 100 | {
 | 
|---|
 | 101 |         assert(pipe);
 | 
|---|
 | 102 |         return pipe->frames;
 | 
|---|
 | 103 | }
 | 
|---|
 | 104 | 
 | 
|---|
| [c799138] | 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 |  */
 | 
|---|
| [d988ef2] | 115 | static inline int audio_pipe_push_data(audio_pipe_t *pipe,
 | 
|---|
 | 116 |     const 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 int ret = audio_pipe_push(pipe, adata);
 | 
|---|
 | 121 |                 audio_data_unref(adata);
 | 
|---|
 | 122 |                 return ret;
 | 
|---|
 | 123 |         }
 | 
|---|
 | 124 |         return ENOMEM;
 | 
|---|
 | 125 | }
 | 
|---|
 | 126 | 
 | 
|---|
 | 127 | 
 | 
|---|
| [23878dc] | 128 | #endif
 | 
|---|
 | 129 | 
 | 
|---|
 | 130 | /**
 | 
|---|
 | 131 |  * @}
 | 
|---|
 | 132 |  */
 | 
|---|