source: mainline/uspace/srv/audio/hound/audio_source.c@ 7f84430

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7f84430 was 4eff63c, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

hound: implement device source events

  • 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 */
58int audio_source_init(audio_source_t *source, const char *name, void *data,
59 int (*connection_change)(audio_source_t *, bool new),
60 int (*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 */
98int audio_source_push_data(audio_source_t *source, const 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, it) {
109 connection_t *conn = connection_from_source_list(it);
110 const int ret = connection_push_data(conn, adata);
111 if (ret != EOK) {
112 log_warning("Failed push data to %s: %s",
113 connection_sink_name(conn), str_error(ret));
114 }
115 }
116 audio_data_unref(adata);
117 return EOK;
118}
119
120/**
121 * @}
122 */
Note: See TracBrowser for help on using the repository browser.