source: mainline/uspace/srv/audio/hound/audio_source.c@ 5029c788

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

hound: add null checks

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[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
43#include "audio_source.h"
[1df3018a]44#include "audio_sink.h"
[737b4c0]45#include "log.h"
46
47
[1df3018a]48int audio_source_init(audio_source_t *source, const char *name, void *data,
[fa60cd69]49 int (*connection_change)(audio_source_t *, bool new),
[1df3018a]50 int (*update_available_data)(audio_source_t *, size_t),
[ea6c838]51 const pcm_format_t *f)
[737b4c0]52{
53 assert(source);
[1df3018a]54 if (!name || !f) {
[737b4c0]55 log_debug("Incorrect parameters.");
56 return EINVAL;
57 }
58 link_initialize(&source->link);
[fa60cd69]59 list_initialize(&source->connections);
[737b4c0]60 source->name = str_dup(name);
[b893cd6]61 if (!source->name)
62 return ENOMEM;
[1df3018a]63 source->private_data = data;
64 source->connection_change = connection_change;
65 source->update_available_data = update_available_data;
66 source->format = *f;
67 source->available_data.base = NULL;
[ab07cf0]68 source->available_data.position = NULL;
[1df3018a]69 source->available_data.size = 0;
70 log_verbose("Initialized source (%p) '%s'", source, source->name);
[737b4c0]71 return EOK;
72}
73
[1df3018a]74void audio_source_fini(audio_source_t *source)
75{
[63d6ff9]76 assert(source);
[1df3018a]77 free(source->name);
[63d6ff9]78 source->name = NULL;
[1df3018a]79}
80
[ec49085]81int audio_source_add_self(audio_source_t *source, void *buffer, size_t size,
[ea6c838]82 const pcm_format_t *f)
[737b4c0]83{
84 assert(source);
[ec49085]85 if (!buffer) {
86 log_debug("Non-existent buffer");
87 return EBADMEM;
88 }
89 if (!f || size == 0) {
90 log_debug("No format or zero size");
91 }
92 if (size % (pcm_sample_format_size(f->sample_format) * f->channels)) {
93 log_debug("Buffer does not fit integer number of frames");
[737b4c0]94 return EINVAL;
95 }
[bb67def]96 if (source->format.sampling_rate != f->sampling_rate) {
97 log_debug("Resampling is not supported, yet");
[ec49085]98 return ENOTSUP;
99 }
[ea6c838]100 const size_t src_frame_size = pcm_format_frame_size(&source->format);
101 const size_t dst_frames = size / pcm_format_frame_size(f);
[950110ee]102
[ab07cf0]103 if (source->available_data.position == NULL ||
[1df3018a]104 source->available_data.size == 0) {
105 int ret = EOVERFLOW; /* In fact this is underflow... */
106 if (source->update_available_data)
[950110ee]107 ret = source->update_available_data(source,
108 dst_frames * src_frame_size);
[1df3018a]109 if (ret != EOK) {
[ab07cf0]110 log_debug("No data to add to %p(%zu)", buffer, size);
[1df3018a]111 return ret;
112 }
[ec49085]113 }
114
[ea6c838]115 const int ret = pcm_format_convert_and_mix(buffer, size,
[950110ee]116 source->available_data.position, source->available_data.size,
117 &source->format, f);
[ec49085]118 if (ret != EOK) {
[950110ee]119 log_debug("Mixing failed %p <= %p, frames: %zu",
120 buffer, source->available_data.position, dst_frames);
[ec49085]121 return ret;
122 }
[ab07cf0]123
[950110ee]124 source->available_data.position += (dst_frames * src_frame_size);
125 source->available_data.size -= (dst_frames * src_frame_size);
[737b4c0]126 return EOK;
127}
[ec49085]128
[737b4c0]129/**
130 * @}
131 */
Note: See TracBrowser for help on using the repository browser.