source: mainline/uspace/srv/audio/hound/audio_source.c

Last change on this file was 33b8d024, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Remove const qualifier from the argument of free() and realloc(),
as well as in numerous other variables that hold ownership of memory.

By convention, a pointer that holds ownership is _never_ qualified by const.
This is reflected in the standard type signature of free() and realloc().
Allowing const pointers to hold ownership may seem superficially convenient,
but is actually quite confusing to experienced C programmers.

  • Property mode set to 100644
File size: 3.6 KB
Line 
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>
38#include <macros.h>
39#include <stdlib.h>
40#include <str.h>
41#include <str_error.h>
42
43#include "audio_data.h"
44#include "audio_source.h"
45#include "audio_sink.h"
46#include "connection.h"
47#include "log.h"
48
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 */
58errno_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),
61 const pcm_format_t *f)
62{
63 assert(source);
64 if (!name || !f) {
65 log_debug("Incorrect parameters.");
66 return EINVAL;
67 }
68 link_initialize(&source->link);
69 list_initialize(&source->connections);
70 source->name = str_dup(name);
71 if (!source->name)
72 return ENOMEM;
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);
78 return EOK;
79}
80
81/**
82 * Release resources claimed by initialization.
83 * @param source The structure to cleanup.
84 */
85void audio_source_fini(audio_source_t *source)
86{
87 assert(source);
88 free(source->name);
89 source->name = NULL;
90}
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 */
98errno_t audio_source_push_data(audio_source_t *source, void *data,
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
108 list_foreach(source->connections, source_link, connection_t, conn) {
109 const errno_t ret = connection_push_data(conn, adata);
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}
118
119/**
120 * @}
121 */
Note: See TracBrowser for help on using the repository browser.