[737b4c0] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 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 | #include <assert.h>
|
---|
| 37 | #include <errno.h>
|
---|
[ec49085] | 38 | #include <macros.h>
|
---|
[737b4c0] | 39 | #include <stdlib.h>
|
---|
| 40 | #include <str.h>
|
---|
| 41 | #include <str_error.h>
|
---|
| 42 |
|
---|
[4eff63c] | 43 | #include "audio_data.h"
|
---|
[737b4c0] | 44 | #include "audio_source.h"
|
---|
[1df3018a] | 45 | #include "audio_sink.h"
|
---|
[4eff63c] | 46 | #include "connection.h"
|
---|
[737b4c0] | 47 | #include "log.h"
|
---|
| 48 |
|
---|
[c799138] | 49 | /**
|
---|
| 50 | * Initialize audio source strcture.
|
---|
| 51 | * @param source The structure to initialize.
|
---|
| 52 | * @param name String identifier of the audio source.
|
---|
| 53 | * @param data Backend data.
|
---|
| 54 | * @param connection_change Connect/disconnect callback.
|
---|
| 55 | * @param update_available_data Data request callback.
|
---|
| 56 | * @return Error code.
|
---|
| 57 | */
|
---|
[5a6cc679] | 58 | errno_t audio_source_init(audio_source_t *source, const char *name, void *data,
|
---|
| 59 | errno_t (*connection_change)(audio_source_t *, bool new),
|
---|
| 60 | errno_t (*update_available_data)(audio_source_t *, size_t),
|
---|
[ea6c838] | 61 | const pcm_format_t *f)
|
---|
[737b4c0] | 62 | {
|
---|
| 63 | assert(source);
|
---|
[1df3018a] | 64 | if (!name || !f) {
|
---|
[737b4c0] | 65 | log_debug("Incorrect parameters.");
|
---|
| 66 | return EINVAL;
|
---|
| 67 | }
|
---|
| 68 | link_initialize(&source->link);
|
---|
[fa60cd69] | 69 | list_initialize(&source->connections);
|
---|
[737b4c0] | 70 | source->name = str_dup(name);
|
---|
[b893cd6] | 71 | if (!source->name)
|
---|
| 72 | return ENOMEM;
|
---|
[1df3018a] | 73 | source->private_data = data;
|
---|
| 74 | source->connection_change = connection_change;
|
---|
| 75 | source->update_available_data = update_available_data;
|
---|
| 76 | source->format = *f;
|
---|
| 77 | log_verbose("Initialized source (%p) '%s'", source, source->name);
|
---|
[737b4c0] | 78 | return EOK;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[c799138] | 81 | /**
|
---|
| 82 | * Release resources claimed by initialization.
|
---|
| 83 | * @param source The structure to cleanup.
|
---|
| 84 | */
|
---|
[1df3018a] | 85 | void audio_source_fini(audio_source_t *source)
|
---|
| 86 | {
|
---|
[63d6ff9] | 87 | assert(source);
|
---|
[1df3018a] | 88 | free(source->name);
|
---|
[63d6ff9] | 89 | source->name = NULL;
|
---|
[1df3018a] | 90 | }
|
---|
[4eff63c] | 91 | /**
|
---|
| 92 | * Push data to all connections.
|
---|
| 93 | * @param source The source of the data.
|
---|
| 94 | * @param dest Destination buffer.
|
---|
| 95 | * @param size size of the @p dest buffer.
|
---|
| 96 | * @return Error code.
|
---|
| 97 | */
|
---|
[5a6cc679] | 98 | errno_t audio_source_push_data(audio_source_t *source, const void *data,
|
---|
[4eff63c] | 99 | size_t size)
|
---|
| 100 | {
|
---|
| 101 | assert(source);
|
---|
| 102 | assert(data);
|
---|
| 103 |
|
---|
| 104 | audio_data_t *adata = audio_data_create(data, size, source->format);
|
---|
| 105 | if (!adata)
|
---|
| 106 | return ENOMEM;
|
---|
| 107 |
|
---|
[feeac0d] | 108 | list_foreach(source->connections, source_link, connection_t, conn) {
|
---|
[5a6cc679] | 109 | const errno_t ret = connection_push_data(conn, adata);
|
---|
[4eff63c] | 110 | if (ret != EOK) {
|
---|
| 111 | log_warning("Failed push data to %s: %s",
|
---|
| 112 | connection_sink_name(conn), str_error(ret));
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | audio_data_unref(adata);
|
---|
| 116 | return EOK;
|
---|
| 117 | }
|
---|
[1df3018a] | 118 |
|
---|
[737b4c0] | 119 | /**
|
---|
| 120 | * @}
|
---|
| 121 | */
|
---|