[2d1fdcad] | 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 | /**
|
---|
| 30 | * @addtogroup audio
|
---|
| 31 | * @brief HelenOS sound server.
|
---|
| 32 | * @{
|
---|
| 33 | */
|
---|
| 34 | /** @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <hound/protocol.h>
|
---|
[e5bc912] | 39 | #include <inttypes.h>
|
---|
[2d1fdcad] | 40 | #include <malloc.h>
|
---|
[3b6c1d4] | 41 |
|
---|
| 42 | #include "hound.h"
|
---|
| 43 | #include "hound_ctx.h"
|
---|
[2d1fdcad] | 44 | #include "log.h"
|
---|
| 45 |
|
---|
| 46 | static int iface_add_context(void *server, hound_context_id_t *id,
|
---|
| 47 | const char *name, bool record)
|
---|
| 48 | {
|
---|
[3b6c1d4] | 49 | assert(server);
|
---|
| 50 | assert(id);
|
---|
| 51 | assert(name);
|
---|
| 52 |
|
---|
| 53 | hound_ctx_t *ctx = record ? hound_record_ctx_get(name) :
|
---|
| 54 | hound_playback_ctx_get(name);
|
---|
| 55 | if (!ctx)
|
---|
| 56 | return ENOMEM;
|
---|
| 57 |
|
---|
| 58 | const int ret = hound_add_ctx(server, ctx);
|
---|
| 59 | if (ret != EOK)
|
---|
| 60 | hound_ctx_destroy(ctx);
|
---|
| 61 | else
|
---|
| 62 | *id = hound_ctx_get_id(ctx);
|
---|
| 63 | return ret;
|
---|
[2d1fdcad] | 64 | }
|
---|
| 65 |
|
---|
| 66 | static int iface_rem_context(void *server, hound_context_id_t id)
|
---|
| 67 | {
|
---|
[3b6c1d4] | 68 | assert(server);
|
---|
| 69 | hound_ctx_t *ctx = hound_get_ctx_by_id(server, id);
|
---|
| 70 | if (!ctx)
|
---|
| 71 | return EINVAL;
|
---|
[353f8cc] | 72 | int ret = hound_remove_ctx(server, ctx);
|
---|
| 73 | if (ret != EOK)
|
---|
| 74 | return ret;
|
---|
[3b6c1d4] | 75 | hound_ctx_destroy(ctx);
|
---|
[e5bc912] | 76 | log_info("%s: %p, %#" PRIxn, __FUNCTION__, server, id);
|
---|
[3b6c1d4] | 77 | return EOK;
|
---|
[2d1fdcad] | 78 | }
|
---|
| 79 |
|
---|
| 80 | static bool iface_is_record_context(void *server, hound_context_id_t id)
|
---|
| 81 | {
|
---|
[3b6c1d4] | 82 | assert(server);
|
---|
| 83 | hound_ctx_t *ctx = hound_get_ctx_by_id(server, id);
|
---|
| 84 | if (!ctx)
|
---|
| 85 | return false;
|
---|
| 86 | return hound_ctx_is_record(ctx);
|
---|
[2d1fdcad] | 87 | }
|
---|
| 88 |
|
---|
| 89 | static int iface_get_list(void *server, const char ***list, size_t *size,
|
---|
| 90 | const char *connection, int flags)
|
---|
| 91 | {
|
---|
[eafc7b2] | 92 | log_info("%s: %p, %zu, %s, %#x\n", __FUNCTION__, server, *size,
|
---|
| 93 | connection, flags);
|
---|
| 94 | if ((flags & (HOUND_SINK_DEVS | HOUND_SINK_APPS)) != 0)
|
---|
| 95 | return hound_list_sinks(server, list, size);
|
---|
| 96 | if ((flags & (HOUND_SOURCE_DEVS | HOUND_SOURCE_APPS)) != 0)
|
---|
| 97 | return hound_list_sources(server, list, size);
|
---|
| 98 | return ENOTSUP;
|
---|
[2d1fdcad] | 99 | }
|
---|
| 100 |
|
---|
| 101 | static int iface_connect(void *server, const char *source, const char *sink)
|
---|
| 102 | {
|
---|
| 103 | log_info("%s: %p, %s -> %s", __FUNCTION__, server, source, sink);
|
---|
[eafc7b2] | 104 | return hound_connect(server, source, sink);
|
---|
[2d1fdcad] | 105 | }
|
---|
| 106 |
|
---|
| 107 | static int iface_disconnect(void *server, const char *source, const char *sink)
|
---|
| 108 | {
|
---|
| 109 | log_info("%s: %p, %s -> %s", __FUNCTION__, server, source, sink);
|
---|
[eafc7b2] | 110 | return hound_disconnect(server, source, sink);
|
---|
[2d1fdcad] | 111 | }
|
---|
| 112 |
|
---|
| 113 | static int iface_add_stream(void *server, hound_context_id_t id, int flags,
|
---|
| 114 | pcm_format_t format, size_t size, void **data)
|
---|
| 115 | {
|
---|
[b266f9e] | 116 | assert(data);
|
---|
| 117 | assert(server);
|
---|
| 118 |
|
---|
[e5bc912] | 119 | log_verbose("%s: %p, %" PRIxn " %x ch:%u r:%u f:%s", __FUNCTION__,
|
---|
| 120 | server, id, flags, format.channels, format.sampling_rate,
|
---|
[2d1fdcad] | 121 | pcm_sample_format_str(format.sample_format));
|
---|
[b266f9e] | 122 | hound_ctx_t *ctx = hound_get_ctx_by_id(server, id);
|
---|
| 123 | if (!ctx)
|
---|
| 124 | return ENOENT;
|
---|
| 125 | hound_ctx_stream_t *stream =
|
---|
| 126 | hound_ctx_create_stream(ctx, flags, format, size);
|
---|
| 127 | if (!stream)
|
---|
| 128 | return ENOMEM;
|
---|
| 129 | *data = stream;
|
---|
| 130 | return EOK;
|
---|
[2d1fdcad] | 131 | }
|
---|
| 132 |
|
---|
| 133 | static int iface_rem_stream(void *server, void *stream)
|
---|
| 134 | {
|
---|
[b266f9e] | 135 | hound_ctx_destroy_stream(stream);
|
---|
| 136 | return EOK;
|
---|
[2d1fdcad] | 137 | }
|
---|
| 138 |
|
---|
[4b33db8e] | 139 | static int iface_drain_stream(void *stream)
|
---|
| 140 | {
|
---|
| 141 | hound_ctx_stream_drain(stream);
|
---|
| 142 | return EOK;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[2d1fdcad] | 145 | static int iface_stream_data_read(void *stream, void *buffer, size_t size)
|
---|
| 146 | {
|
---|
[bf13c9a4] | 147 | return hound_ctx_stream_read(stream, buffer, size);
|
---|
[2d1fdcad] | 148 | }
|
---|
| 149 |
|
---|
| 150 | static int iface_stream_data_write(void *stream, const void *buffer, size_t size)
|
---|
| 151 | {
|
---|
[78aca91b] | 152 | return hound_ctx_stream_write(stream, buffer, size);
|
---|
[2d1fdcad] | 153 | }
|
---|
| 154 |
|
---|
| 155 | hound_server_iface_t hound_iface = {
|
---|
| 156 | .add_context = iface_add_context,
|
---|
| 157 | .rem_context = iface_rem_context,
|
---|
| 158 | .is_record_context = iface_is_record_context,
|
---|
| 159 | .get_list = iface_get_list,
|
---|
| 160 | .connect = iface_connect,
|
---|
| 161 | .disconnect = iface_disconnect,
|
---|
| 162 | .add_stream = iface_add_stream,
|
---|
| 163 | .rem_stream = iface_rem_stream,
|
---|
[4b33db8e] | 164 | .drain_stream = iface_drain_stream,
|
---|
[2d1fdcad] | 165 | .stream_data_write = iface_stream_data_write,
|
---|
| 166 | .stream_data_read = iface_stream_data_read,
|
---|
| 167 | .server = NULL,
|
---|
| 168 | };
|
---|