| 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 <atomic.h>
|
|---|
| 41 | #include <errno.h>
|
|---|
| 42 | #include <fibril_synch.h>
|
|---|
| 43 | #include <pcm/format.h>
|
|---|
| 44 |
|
|---|
| 45 | typedef struct {
|
|---|
| 46 | const void *data;
|
|---|
| 47 | size_t size;
|
|---|
| 48 | pcm_format_t format;
|
|---|
| 49 | atomic_t refcount;
|
|---|
| 50 | } audio_data_t;
|
|---|
| 51 |
|
|---|
| 52 | typedef struct {
|
|---|
| 53 | link_t link;
|
|---|
| 54 | audio_data_t *adata;
|
|---|
| 55 | size_t position;
|
|---|
| 56 | } audio_data_link_t;
|
|---|
| 57 |
|
|---|
| 58 | typedef struct {
|
|---|
| 59 | list_t list;
|
|---|
| 60 | size_t bytes;
|
|---|
| 61 | size_t frames;
|
|---|
| 62 | fibril_mutex_t guard;
|
|---|
| 63 | } audio_pipe_t;
|
|---|
| 64 |
|
|---|
| 65 | static inline audio_data_link_t * audio_data_link_list_instance(link_t *l)
|
|---|
| 66 | {
|
|---|
| 67 | return l ? list_get_instance(l, audio_data_link_t, link) : NULL;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | audio_data_t * audio_data_create(const void *data, size_t size,
|
|---|
| 71 | pcm_format_t format);
|
|---|
| 72 | void audio_data_addref(audio_data_t *adata);
|
|---|
| 73 | void audio_data_unref(audio_data_t *adata);
|
|---|
| 74 |
|
|---|
| 75 | audio_data_link_t * audio_data_link_create_data(const void *data, size_t size,
|
|---|
| 76 | pcm_format_t format);
|
|---|
| 77 | audio_data_link_t *audio_data_link_create(audio_data_t *adata);
|
|---|
| 78 | void audio_data_link_destroy(audio_data_link_t *link);
|
|---|
| 79 |
|
|---|
| 80 | size_t audio_data_link_available_frames(audio_data_link_t *alink);
|
|---|
| 81 | static inline const void * audio_data_link_start(audio_data_link_t *alink)
|
|---|
| 82 | {
|
|---|
| 83 | assert(alink);
|
|---|
| 84 | assert(alink->adata);
|
|---|
| 85 | return alink->adata->data + alink->position;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | static inline size_t audio_data_link_remain_size(audio_data_link_t *alink)
|
|---|
| 89 | {
|
|---|
| 90 | assert(alink);
|
|---|
| 91 | assert(alink->adata);
|
|---|
| 92 | assert(alink->position <= alink->adata->size);
|
|---|
| 93 | return alink->adata->size - alink->position;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | void audio_pipe_init(audio_pipe_t *pipe);
|
|---|
| 97 | void audio_pipe_fini(audio_pipe_t *pipe);
|
|---|
| 98 |
|
|---|
| 99 | int audio_pipe_push(audio_pipe_t *pipe, audio_data_t *data);
|
|---|
| 100 | audio_data_t *audio_pipe_pop(audio_pipe_t *pipe);
|
|---|
| 101 |
|
|---|
| 102 | ssize_t audio_pipe_mix_data(audio_pipe_t *pipe, void *buffer, size_t size,
|
|---|
| 103 | const pcm_format_t *f);
|
|---|
| 104 |
|
|---|
| 105 | static inline size_t audio_pipe_bytes(audio_pipe_t *pipe)
|
|---|
| 106 | {
|
|---|
| 107 | assert(pipe);
|
|---|
| 108 | return pipe->bytes;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | static inline size_t audio_pipe_frames(audio_pipe_t *pipe)
|
|---|
| 112 | {
|
|---|
| 113 | assert(pipe);
|
|---|
| 114 | return pipe->frames;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | static inline int audio_pipe_push_data(audio_pipe_t *pipe,
|
|---|
| 118 | const void *data, size_t size, pcm_format_t f)
|
|---|
| 119 | {
|
|---|
| 120 | audio_data_t *adata = audio_data_create(data, size, f);
|
|---|
| 121 | if (adata) {
|
|---|
| 122 | const int ret = audio_pipe_push(pipe, adata);
|
|---|
| 123 | audio_data_unref(adata);
|
|---|
| 124 | return ret;
|
|---|
| 125 | }
|
|---|
| 126 | return ENOMEM;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 | #endif
|
|---|
| 131 |
|
|---|
| 132 | /**
|
|---|
| 133 | * @}
|
|---|
| 134 | */
|
|---|