[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>
|
---|
| 38 | #include <stdlib.h>
|
---|
| 39 | #include <str.h>
|
---|
| 40 | #include <str_error.h>
|
---|
| 41 |
|
---|
| 42 | #include "audio_source.h"
|
---|
| 43 | #include "log.h"
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | int audio_source_init(audio_source_t *source, const char* name)
|
---|
| 47 | {
|
---|
| 48 | assert(source);
|
---|
| 49 | if (!name) {
|
---|
| 50 | log_debug("Incorrect parameters.");
|
---|
| 51 | return EINVAL;
|
---|
| 52 | }
|
---|
| 53 | link_initialize(&source->link);
|
---|
| 54 | source->name = str_dup(name);
|
---|
| 55 | source->connected_change.hook = NULL;
|
---|
| 56 | source->connected_change.arg = NULL;
|
---|
| 57 | source->get_data.hook = NULL;
|
---|
| 58 | source->get_data.arg = NULL;
|
---|
| 59 | source->available.base = NULL;
|
---|
| 60 | source->available.size = 0;
|
---|
| 61 | log_verbose("Initialized source (%p) '%s' with ANY audio format",
|
---|
| 62 | source, source->name);
|
---|
| 63 | return EOK;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | int audio_source_connected(audio_source_t *source, const audio_format_t *f)
|
---|
| 67 | {
|
---|
| 68 | assert(source);
|
---|
| 69 | if (!f) {
|
---|
| 70 | log_debug("Incorrect parameters.");
|
---|
| 71 | return EINVAL;
|
---|
| 72 | }
|
---|
| 73 | if (source->connected_change.hook)
|
---|
| 74 | source->connected_change.hook(source->connected_change.arg, f);
|
---|
| 75 | return EOK;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | int audio_source_get_buffer(audio_source_t *source, const void **buffer, size_t *size)
|
---|
| 79 | {
|
---|
| 80 | assert(source);
|
---|
| 81 | if (!buffer || !size) {
|
---|
| 82 | log_debug("Incorrect parameters.");
|
---|
| 83 | return EINVAL;
|
---|
| 84 | }
|
---|
| 85 | if (source->get_data.hook)
|
---|
| 86 | source->get_data.hook(source->get_data.arg);
|
---|
| 87 | if (!source->available.base || !source->available.size)
|
---|
| 88 | return EOVERFLOW; /* In fact it's underflow */
|
---|
| 89 |
|
---|
| 90 | const size_t requested_size =
|
---|
| 91 | (*size == 0) || (*size > source->available.size)
|
---|
| 92 | ? source->available.size : *size;
|
---|
| 93 | *buffer = source->available.base;
|
---|
| 94 | *size = requested_size;
|
---|
| 95 | source->available.size -= requested_size;
|
---|
| 96 | source->available.base += requested_size;
|
---|
| 97 | return EOK;
|
---|
| 98 | }
|
---|
| 99 | /**
|
---|
| 100 | * @}
|
---|
| 101 | */
|
---|