[fa60cd69] | 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 | #include <errno.h>
|
---|
[38d150e] | 37 | #include <stdlib.h>
|
---|
[fa60cd69] | 38 |
|
---|
| 39 | #include "log.h"
|
---|
| 40 | #include "connection.h"
|
---|
| 41 |
|
---|
[c799138] | 42 | /**
|
---|
| 43 | * Create connection between source and sink.
|
---|
| 44 | * @param source Valid source structure.
|
---|
| 45 | * @param sink Valid sink structure.
|
---|
| 46 | * @return pointer to a valid connection structure, NULL on failure.
|
---|
| 47 | *
|
---|
| 48 | * Reports new connection to both the source and sink.
|
---|
| 49 | */
|
---|
[fa60cd69] | 50 | connection_t *connection_create(audio_source_t *source, audio_sink_t *sink)
|
---|
| 51 | {
|
---|
| 52 | assert(source);
|
---|
| 53 | assert(sink);
|
---|
| 54 | connection_t *conn = malloc(sizeof(connection_t));
|
---|
| 55 | if (conn) {
|
---|
[50179b63] | 56 | audio_pipe_init(&conn->fifo);
|
---|
[fa60cd69] | 57 | link_initialize(&conn->source_link);
|
---|
| 58 | link_initialize(&conn->sink_link);
|
---|
| 59 | link_initialize(&conn->hound_link);
|
---|
| 60 | conn->sink = sink;
|
---|
| 61 | conn->source = source;
|
---|
| 62 | list_append(&conn->source_link, &source->connections);
|
---|
[2fbd49c] | 63 | fibril_mutex_lock(&sink->lock);
|
---|
[fa60cd69] | 64 | list_append(&conn->sink_link, &sink->connections);
|
---|
[2fbd49c] | 65 | fibril_mutex_unlock(&sink->lock);
|
---|
[fa60cd69] | 66 | audio_sink_set_format(sink, audio_source_format(source));
|
---|
[b893cd6] | 67 | if (source->connection_change)
|
---|
| 68 | source->connection_change(source, true);
|
---|
| 69 | if (sink->connection_change)
|
---|
| 70 | sink->connection_change(sink, true);
|
---|
[6da3baec] | 71 | log_debug("CONNECTED: %s -> %s", source->name, sink->name);
|
---|
[fa60cd69] | 72 | }
|
---|
| 73 | return conn;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[c799138] | 76 | /**
|
---|
| 77 | * Destroy existing connection
|
---|
| 78 | * @param connection The connection to destroy.
|
---|
| 79 | *
|
---|
| 80 | * Disconnects from both the source and the sink.
|
---|
| 81 | */
|
---|
[fa60cd69] | 82 | void connection_destroy(connection_t *connection)
|
---|
| 83 | {
|
---|
| 84 | assert(connection);
|
---|
| 85 | assert(!link_in_use(&connection->hound_link));
|
---|
| 86 | list_remove(&connection->source_link);
|
---|
[2fbd49c] | 87 | fibril_mutex_lock(&connection->sink->lock);
|
---|
[fa60cd69] | 88 | list_remove(&connection->sink_link);
|
---|
[2fbd49c] | 89 | fibril_mutex_unlock(&connection->sink->lock);
|
---|
[6da3baec] | 90 | if (connection->sink && connection->sink->connection_change)
|
---|
| 91 | connection->sink->connection_change(connection->sink, false);
|
---|
| 92 | if (connection->source && connection->source->connection_change)
|
---|
| 93 | connection->source->connection_change(connection->source, false);
|
---|
[50179b63] | 94 | audio_pipe_fini(&connection->fifo);
|
---|
[6da3baec] | 95 | log_debug("DISCONNECTED: %s -> %s",
|
---|
| 96 | connection->source->name, connection->sink->name);
|
---|
[fa60cd69] | 97 | free(connection);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[c799138] | 100 | /**
|
---|
| 101 | * Update and mix data provided by the source.
|
---|
| 102 | * @param connection the connection to add.
|
---|
| 103 | * @param data Destination audio buffer.
|
---|
| 104 | * @param size size of the destination audio buffer.
|
---|
| 105 | * @param format format of the destination audio buffer.
|
---|
| 106 | */
|
---|
[b7fd2a0] | 107 | errno_t connection_add_source_data(connection_t *connection, void *data,
|
---|
[fa60cd69] | 108 | size_t size, pcm_format_t format)
|
---|
| 109 | {
|
---|
| 110 | assert(connection);
|
---|
| 111 | if (!data)
|
---|
| 112 | return EBADMEM;
|
---|
[98af9cc] | 113 | const size_t needed_frames = pcm_format_size_to_frames(size, &format);
|
---|
[4389076] | 114 | if (needed_frames > audio_pipe_frames(&connection->fifo) &&
|
---|
| 115 | connection->source->update_available_data) {
|
---|
| 116 | log_debug("Asking source to provide more data");
|
---|
| 117 | connection->source->update_available_data(
|
---|
| 118 | connection->source, size);
|
---|
| 119 | }
|
---|
| 120 | log_verbose("Data available after update: %zu",
|
---|
| 121 | audio_pipe_bytes(&connection->fifo));
|
---|
[541eb67] | 122 | size_t ret =
|
---|
[4389076] | 123 | audio_pipe_mix_data(&connection->fifo, data, size, &format);
|
---|
[541eb67] | 124 | if (ret != size)
|
---|
[4389076] | 125 | log_warning("Connection failed to provide enough data %zd/%zu",
|
---|
| 126 | ret, size);
|
---|
[541eb67] | 127 | return EOK;
|
---|
[fa60cd69] | 128 | }
|
---|
[bee5349] | 129 | /**
|
---|
| 130 | * Add new data to the connection buffer.
|
---|
| 131 | * @param connection Target conneciton.
|
---|
| 132 | * @aparam adata Reference counted audio data buffer.
|
---|
| 133 | * @return Error code.
|
---|
| 134 | */
|
---|
[b7fd2a0] | 135 | errno_t connection_push_data(connection_t *connection,
|
---|
[bee5349] | 136 | audio_data_t *adata)
|
---|
| 137 | {
|
---|
| 138 | assert(connection);
|
---|
| 139 | assert(adata);
|
---|
[b7fd2a0] | 140 | const errno_t ret = audio_pipe_push(&connection->fifo, adata);
|
---|
[bee5349] | 141 | if (ret == EOK && connection->sink->data_available)
|
---|
| 142 | connection->sink->data_available(connection->sink);
|
---|
| 143 | return ret;
|
---|
| 144 | }
|
---|
[fa60cd69] | 145 |
|
---|
| 146 | /**
|
---|
| 147 | * @}
|
---|
| 148 | */
|
---|