source: mainline/uspace/srv/audio/hound/connection.c@ 38d150e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 38d150e was 38d150e, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Prefer to get memory allocation functions through the standard stdlib header.

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[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]50connection_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);
63 list_append(&conn->sink_link, &sink->connections);
64 audio_sink_set_format(sink, audio_source_format(source));
[b893cd6]65 if (source->connection_change)
66 source->connection_change(source, true);
67 if (sink->connection_change)
68 sink->connection_change(sink, true);
[6da3baec]69 log_debug("CONNECTED: %s -> %s", source->name, sink->name);
[fa60cd69]70 }
71 return conn;
72}
73
[c799138]74/**
75 * Destroy existing connection
76 * @param connection The connection to destroy.
77 *
78 * Disconnects from both the source and the sink.
79 */
[fa60cd69]80void connection_destroy(connection_t *connection)
81{
82 assert(connection);
83 assert(!link_in_use(&connection->hound_link));
84 list_remove(&connection->source_link);
85 list_remove(&connection->sink_link);
[6da3baec]86 if (connection->sink && connection->sink->connection_change)
87 connection->sink->connection_change(connection->sink, false);
88 if (connection->source && connection->source->connection_change)
89 connection->source->connection_change(connection->source, false);
[50179b63]90 audio_pipe_fini(&connection->fifo);
[6da3baec]91 log_debug("DISCONNECTED: %s -> %s",
92 connection->source->name, connection->sink->name);
[fa60cd69]93 free(connection);
94}
95
[c799138]96/**
97 * Update and mix data provided by the source.
98 * @param connection the connection to add.
99 * @param data Destination audio buffer.
100 * @param size size of the destination audio buffer.
101 * @param format format of the destination audio buffer.
102 */
[fa60cd69]103ssize_t connection_add_source_data(connection_t *connection, void *data,
104 size_t size, pcm_format_t format)
105{
106 assert(connection);
107 if (!data)
108 return EBADMEM;
[98af9cc]109 const size_t needed_frames = pcm_format_size_to_frames(size, &format);
[4389076]110 if (needed_frames > audio_pipe_frames(&connection->fifo) &&
111 connection->source->update_available_data) {
112 log_debug("Asking source to provide more data");
113 connection->source->update_available_data(
114 connection->source, size);
115 }
116 log_verbose("Data available after update: %zu",
117 audio_pipe_bytes(&connection->fifo));
118 ssize_t ret =
119 audio_pipe_mix_data(&connection->fifo, data, size, &format);
120 if (ret != (ssize_t)size)
121 log_warning("Connection failed to provide enough data %zd/%zu",
122 ret, size);
123 return ret > 0 ? EOK : ret;
[fa60cd69]124}
[bee5349]125/**
126 * Add new data to the connection buffer.
127 * @param connection Target conneciton.
128 * @aparam adata Reference counted audio data buffer.
129 * @return Error code.
130 */
131int connection_push_data(connection_t *connection,
132 audio_data_t *adata)
133{
134 assert(connection);
135 assert(adata);
136 const int ret = audio_pipe_push(&connection->fifo, adata);
137 if (ret == EOK && connection->sink->data_available)
138 connection->sink->data_available(connection->sink);
139 return ret;
140}
[fa60cd69]141
142/**
143 * @}
144 */
Note: See TracBrowser for help on using the repository browser.