| 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 libhound
|
|---|
| 30 | * @addtogroup audio
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file
|
|---|
| 34 | * Common USB functions.
|
|---|
| 35 | */
|
|---|
| 36 | #include <adt/list.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <loc.h>
|
|---|
| 39 | #include <str.h>
|
|---|
| 40 | #include <stdlib.h>
|
|---|
| 41 | #include <stdio.h>
|
|---|
| 42 | #include <libarch/types.h>
|
|---|
| 43 |
|
|---|
| 44 | #include "protocol.h"
|
|---|
| 45 | #include "client.h"
|
|---|
| 46 |
|
|---|
| 47 | /***
|
|---|
| 48 | * CLIENT SIDE
|
|---|
| 49 | ***/
|
|---|
| 50 |
|
|---|
| 51 | typedef struct hound_stream {
|
|---|
| 52 | link_t link;
|
|---|
| 53 | pcm_format_t format;
|
|---|
| 54 | async_exch_t *exch;
|
|---|
| 55 | hound_context_t *context;
|
|---|
| 56 | } hound_stream_t;
|
|---|
| 57 |
|
|---|
| 58 | typedef struct hound_context {
|
|---|
| 59 | hound_sess_t *session;
|
|---|
| 60 | const char *name;
|
|---|
| 61 | bool record;
|
|---|
| 62 | list_t stream_list;
|
|---|
| 63 | struct {
|
|---|
| 64 | hound_stream_t *stream;
|
|---|
| 65 | pcm_format_t format;
|
|---|
| 66 | size_t bsize;
|
|---|
| 67 | } main;
|
|---|
| 68 | hound_context_id_t id;
|
|---|
| 69 | } hound_context_t;
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 | static hound_context_t *hound_context_create(const char *name, bool record,
|
|---|
| 73 | pcm_format_t format, size_t bsize)
|
|---|
| 74 | {
|
|---|
| 75 | hound_context_t *new_context = malloc(sizeof(hound_context_t));
|
|---|
| 76 | if (new_context) {
|
|---|
| 77 | char *cont_name;
|
|---|
| 78 | int ret = asprintf(&cont_name, "%llu:%s", task_get_id(), name);
|
|---|
| 79 | if (ret < 0) {
|
|---|
| 80 | free(new_context);
|
|---|
| 81 | return NULL;
|
|---|
| 82 | }
|
|---|
| 83 | list_initialize(&new_context->stream_list);
|
|---|
| 84 | new_context->name = cont_name;
|
|---|
| 85 | new_context->record = record;
|
|---|
| 86 | new_context->session = hound_service_connect(HOUND_SERVICE);
|
|---|
| 87 | new_context->main.stream = NULL;
|
|---|
| 88 | new_context->main.format = format;
|
|---|
| 89 | new_context->main.bsize = bsize;
|
|---|
| 90 | if (!new_context->session) {
|
|---|
| 91 | free(new_context->name);
|
|---|
| 92 | free(new_context);
|
|---|
| 93 | return NULL;
|
|---|
| 94 | }
|
|---|
| 95 | new_context->id = hound_service_register_context(
|
|---|
| 96 | new_context->session, new_context->name, record);
|
|---|
| 97 | if (new_context->id <= 0) {
|
|---|
| 98 | free(new_context->name);
|
|---|
| 99 | free(new_context);
|
|---|
| 100 | return NULL;
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 | return new_context;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | hound_context_t * hound_context_create_playback(const char *name,
|
|---|
| 107 | pcm_format_t format, size_t bsize)
|
|---|
| 108 | {
|
|---|
| 109 | return hound_context_create(name, false, format, bsize);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | hound_context_t * hound_context_create_capture(const char *name,
|
|---|
| 113 | pcm_format_t format, size_t bsize)
|
|---|
| 114 | {
|
|---|
| 115 | return hound_context_create(name, true, format, bsize);
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | void hound_context_destroy(hound_context_t *hound)
|
|---|
| 119 | {
|
|---|
| 120 | assert(hound);
|
|---|
| 121 | hound_service_unregister_context(hound->session, hound->id);
|
|---|
| 122 | hound_service_disconnect(hound->session);
|
|---|
| 123 | free(hound->name);
|
|---|
| 124 | free(hound);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | int hound_context_enable(hound_context_t *hound)
|
|---|
| 128 | {
|
|---|
| 129 | assert(hound);
|
|---|
| 130 | return ENOTSUP;
|
|---|
| 131 | }
|
|---|
| 132 | int hound_context_disable(hound_context_t *hound)
|
|---|
| 133 | {
|
|---|
| 134 | assert(hound);
|
|---|
| 135 | return ENOTSUP;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | int hound_get_output_targets(const char **names, size_t *count)
|
|---|
| 139 | {
|
|---|
| 140 | assert(names);
|
|---|
| 141 | assert(count);
|
|---|
| 142 | return ENOTSUP;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | int hound_get_input_targets(const char **names, size_t *count)
|
|---|
| 146 | {
|
|---|
| 147 | assert(names);
|
|---|
| 148 | assert(count);
|
|---|
| 149 | return ENOTSUP;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | int hound_context_connect_target(hound_context_t *hound, const char* target)
|
|---|
| 153 | {
|
|---|
| 154 | assert(hound);
|
|---|
| 155 | return ENOTSUP;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | int hound_context_disconnect_target(hound_context_t *hound, const char* target)
|
|---|
| 159 | {
|
|---|
| 160 | assert(hound);
|
|---|
| 161 | return ENOTSUP;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | hound_stream_t *hound_stream_create(hound_context_t *hound, unsigned flags,
|
|---|
| 165 | pcm_format_t format, size_t bsize)
|
|---|
| 166 | {
|
|---|
| 167 | assert(hound);
|
|---|
| 168 | async_exch_t *stream_exch = async_exchange_begin(hound->session);
|
|---|
| 169 | if (!stream_exch)
|
|---|
| 170 | return NULL;
|
|---|
| 171 | hound_stream_t *new_stream = malloc(sizeof(hound_stream_t));
|
|---|
| 172 | if (new_stream) {
|
|---|
| 173 | link_initialize(&new_stream->link);
|
|---|
| 174 | new_stream->exch = stream_exch;
|
|---|
| 175 | new_stream->format = format;
|
|---|
| 176 | new_stream->context = hound;
|
|---|
| 177 | int ret = hound_service_stream_enter(new_stream->exch,
|
|---|
| 178 | hound->id, flags, format, bsize);
|
|---|
| 179 | if (ret != EOK) {
|
|---|
| 180 | async_exchange_end(new_stream->exch);
|
|---|
| 181 | free(new_stream);
|
|---|
| 182 | return NULL;
|
|---|
| 183 | }
|
|---|
| 184 | list_append(&new_stream->link, &hound->stream_list);
|
|---|
| 185 | }
|
|---|
| 186 | return new_stream;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | void hound_stream_destroy(hound_stream_t *stream)
|
|---|
| 190 | {
|
|---|
| 191 | if (stream) {
|
|---|
| 192 | // TODO drain?
|
|---|
| 193 | hound_service_stream_exit(stream->exch);
|
|---|
| 194 | async_exchange_end(stream->exch);
|
|---|
| 195 | list_remove(&stream->link);
|
|---|
| 196 | free(stream);
|
|---|
| 197 | }
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | int hound_stream_write(hound_stream_t *stream, const void *data, size_t size)
|
|---|
| 201 | {
|
|---|
| 202 | assert(stream);
|
|---|
| 203 | if (!data || size == 0)
|
|---|
| 204 | return EBADMEM;
|
|---|
| 205 | return hound_service_stream_write(stream->exch, data, size);
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | int hound_stream_read(hound_stream_t *stream, void *data, size_t size)
|
|---|
| 209 | {
|
|---|
| 210 | assert(stream);
|
|---|
| 211 | if (!data || size == 0)
|
|---|
| 212 | return EBADMEM;
|
|---|
| 213 | return hound_service_stream_read(stream->exch, data, size);
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | static hound_stream_t * hound_get_main_stream(hound_context_t *hound)
|
|---|
| 217 | {
|
|---|
| 218 | assert(hound);
|
|---|
| 219 | if (!hound->main.stream)
|
|---|
| 220 | hound->main.stream = hound_stream_create(hound, 0,
|
|---|
| 221 | hound->main.format, hound->main.bsize);
|
|---|
| 222 | return hound->main.stream;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | int hound_write_main_stream(hound_context_t *hound,
|
|---|
| 226 | const void *data, size_t size)
|
|---|
| 227 | {
|
|---|
| 228 | assert(hound);
|
|---|
| 229 | hound_stream_t *mstream = hound_get_main_stream(hound);
|
|---|
| 230 | if (!mstream)
|
|---|
| 231 | return ENOMEM;
|
|---|
| 232 | return hound_stream_write(mstream, data, size);
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | int hound_read_main_stream(hound_context_t *hound, void *data, size_t size)
|
|---|
| 236 | {
|
|---|
| 237 | assert(hound);
|
|---|
| 238 | hound_stream_t *mstream = hound_get_main_stream(hound);
|
|---|
| 239 | if (!mstream)
|
|---|
| 240 | return ENOMEM;
|
|---|
| 241 | return hound_stream_read(mstream, data, size);
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | int hound_write_replace_main_stream(hound_context_t *hound,
|
|---|
| 245 | const void *data, size_t size)
|
|---|
| 246 | {
|
|---|
| 247 | assert(hound);
|
|---|
| 248 | if (!data || size == 0)
|
|---|
| 249 | return EBADMEM;
|
|---|
| 250 | // TODO implement
|
|---|
| 251 | return ENOTSUP;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | int hound_context_set_main_stream_format(hound_context_t *hound,
|
|---|
| 255 | unsigned channels, unsigned rate, pcm_sample_format_t format)
|
|---|
| 256 | {
|
|---|
| 257 | assert(hound);
|
|---|
| 258 | // TODO implement
|
|---|
| 259 | return ENOTSUP;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | int hound_write_immediate(hound_context_t *hound, pcm_format_t format,
|
|---|
| 263 | const void *data, size_t size)
|
|---|
| 264 | {
|
|---|
| 265 | assert(hound);
|
|---|
| 266 | hound_stream_t *tmpstream = hound_stream_create(hound, 0, format, size);
|
|---|
| 267 | if (!tmpstream)
|
|---|
| 268 | return ENOMEM;
|
|---|
| 269 | const int ret = hound_stream_write(tmpstream, data, size);
|
|---|
| 270 | if (ret == EOK) {
|
|---|
| 271 | //TODO drain?
|
|---|
| 272 | hound_service_stream_drain(tmpstream->exch);
|
|---|
| 273 | }
|
|---|
| 274 | hound_stream_destroy(tmpstream);
|
|---|
| 275 | return ret;
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 | /**
|
|---|
| 280 | * @}
|
|---|
| 281 | */
|
|---|