Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "r_submix" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <pthread.h> |
| 22 | #include <stdint.h> |
| 23 | #include <sys/time.h> |
| 24 | #include <stdlib.h> |
| 25 | |
| 26 | #include <cutils/log.h> |
| 27 | #include <cutils/str_parms.h> |
| 28 | #include <cutils/properties.h> |
| 29 | |
| 30 | #include <hardware/hardware.h> |
| 31 | #include <system/audio.h> |
| 32 | #include <hardware/audio.h> |
| 33 | |
| 34 | #include <media/nbaio/Pipe.h> |
| 35 | #include <media/nbaio/PipeReader.h> |
| 36 | #include <media/AudioBufferProvider.h> |
| 37 | |
| 38 | extern "C" { |
| 39 | |
| 40 | namespace android { |
| 41 | |
| 42 | #define MAX_PIPE_DEPTH_IN_FRAMES (1024*4) |
| 43 | #define MAX_READ_ATTEMPTS 10 |
| 44 | #define READ_ATTEMPT_SLEEP_MS 10 // 10ms between two read attempts when pipe is empty |
| 45 | #define DEFAULT_RATE_HZ 48000 // default sample rate |
| 46 | |
| 47 | struct submix_config { |
| 48 | audio_format_t format; |
| 49 | audio_channel_mask_t channel_mask; |
| 50 | unsigned int rate; // sample rate for the device |
| 51 | unsigned int period_size; // size of the audio pipe is period_size * period_count in frames |
| 52 | unsigned int period_count; |
| 53 | }; |
| 54 | |
| 55 | struct submix_audio_device { |
| 56 | struct audio_hw_device device; |
| 57 | submix_config config; |
| 58 | // Pipe variables: they handle the ring buffer that "pipes" audio: |
| 59 | // - from the submix virtual audio output == what needs to be played by |
| 60 | // the remotely, seen as an output for AudioFlinger |
| 61 | // - to the virtual audio source == what is captured by the component |
| 62 | // which "records" the submix / virtual audio source, and handles it as needed. |
| 63 | // An usecase example is one where the component capturing the audio is then sending it over |
| 64 | // Wifi for presentation on a remote Wifi Display device (e.g. a dongle attached to a TV, or a |
| 65 | // TV with Wifi Display capabilities), or to a wireless audio player. |
| 66 | sp<Pipe> rsxSink; |
| 67 | sp<PipeReader> rsxSource; |
| 68 | |
| 69 | pthread_mutex_t lock; |
| 70 | }; |
| 71 | |
| 72 | struct submix_stream_out { |
| 73 | struct audio_stream_out stream; |
| 74 | struct submix_audio_device *dev; |
| 75 | }; |
| 76 | |
| 77 | struct submix_stream_in { |
| 78 | struct audio_stream_in stream; |
| 79 | struct submix_audio_device *dev; |
| 80 | }; |
| 81 | |
| 82 | |
| 83 | /* audio HAL functions */ |
| 84 | |
| 85 | static uint32_t out_get_sample_rate(const struct audio_stream *stream) |
| 86 | { |
| 87 | const struct submix_stream_out *out = |
| 88 | reinterpret_cast<const struct submix_stream_out *>(stream); |
| 89 | uint32_t out_rate = out->dev->config.rate; |
| 90 | //ALOGV("out_get_sample_rate() returns %u", out_rate); |
| 91 | return out_rate; |
| 92 | } |
| 93 | |
| 94 | static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 95 | { |
| 96 | if ((rate != 44100) && (rate != 48000)) { |
| 97 | ALOGE("out_set_sample_rate(rate=%u) rate unsupported", rate); |
| 98 | return -ENOSYS; |
| 99 | } |
| 100 | struct submix_stream_out *out = reinterpret_cast<struct submix_stream_out *>(stream); |
| 101 | //ALOGV("out_set_sample_rate(rate=%u)", rate); |
| 102 | out->dev->config.rate = rate; |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static size_t out_get_buffer_size(const struct audio_stream *stream) |
| 107 | { |
| 108 | const struct submix_stream_out *out = |
| 109 | reinterpret_cast<const struct submix_stream_out *>(stream); |
| 110 | const struct submix_config& config_out = out->dev->config; |
| 111 | size_t buffer_size = config_out.period_size * popcount(config_out.channel_mask) |
| 112 | * sizeof(int16_t); // only PCM 16bit |
| 113 | //ALOGV("out_get_buffer_size() returns %u, period size=%u", |
| 114 | // buffer_size, config_out.period_size); |
| 115 | return buffer_size; |
| 116 | } |
| 117 | |
| 118 | static audio_channel_mask_t out_get_channels(const struct audio_stream *stream) |
| 119 | { |
| 120 | const struct submix_stream_out *out = |
| 121 | reinterpret_cast<const struct submix_stream_out *>(stream); |
| 122 | uint32_t channels = out->dev->config.channel_mask; |
| 123 | //ALOGV("out_get_channels() returns %08x", channels); |
| 124 | return channels; |
| 125 | } |
| 126 | |
| 127 | static audio_format_t out_get_format(const struct audio_stream *stream) |
| 128 | { |
| 129 | return AUDIO_FORMAT_PCM_16_BIT; |
| 130 | } |
| 131 | |
| 132 | static int out_set_format(struct audio_stream *stream, audio_format_t format) |
| 133 | { |
| 134 | if (format != AUDIO_FORMAT_PCM_16_BIT) { |
| 135 | return -ENOSYS; |
| 136 | } else { |
| 137 | return 0; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | static int out_standby(struct audio_stream *stream) |
| 142 | { |
| 143 | // REMOTE_SUBMIX is a proxy / virtual audio device, so the notion of standby doesn't apply here |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static int out_dump(const struct audio_stream *stream, int fd) |
| 148 | { |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 153 | { |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static char * out_get_parameters(const struct audio_stream *stream, const char *keys) |
| 158 | { |
| 159 | return strdup(""); |
| 160 | } |
| 161 | |
| 162 | static uint32_t out_get_latency(const struct audio_stream_out *stream) |
| 163 | { |
| 164 | const struct submix_stream_out *out = |
| 165 | reinterpret_cast<const struct submix_stream_out *>(stream); |
| 166 | const struct submix_config * config_out = &(out->dev->config); |
| 167 | uint32_t latency = (MAX_PIPE_DEPTH_IN_FRAMES * 1000) / config_out->rate; |
| 168 | ALOGV("out_get_latency() returns %u", latency); |
| 169 | return latency; |
| 170 | } |
| 171 | |
| 172 | static int out_set_volume(struct audio_stream_out *stream, float left, |
| 173 | float right) |
| 174 | { |
| 175 | return -ENOSYS; |
| 176 | } |
| 177 | |
| 178 | static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, |
| 179 | size_t bytes) |
| 180 | { |
| 181 | //ALOGV("out_write(bytes=%d)", bytes); |
| 182 | ssize_t written = 0; |
| 183 | struct submix_stream_out *out = reinterpret_cast<struct submix_stream_out *>(stream); |
| 184 | |
| 185 | pthread_mutex_lock(&out->dev->lock); |
| 186 | |
| 187 | Pipe* sink = out->dev->rsxSink.get(); |
| 188 | if (sink != NULL) { |
| 189 | out->dev->rsxSink->incStrong(buffer); |
| 190 | } else { |
| 191 | pthread_mutex_unlock(&out->dev->lock); |
| 192 | ALOGE("out_write without a pipe!"); |
| 193 | ALOG_ASSERT("out_write without a pipe!"); |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | pthread_mutex_unlock(&out->dev->lock); |
| 198 | |
| 199 | const size_t frames = bytes / audio_stream_frame_size(&stream->common); |
| 200 | written = sink->write(buffer, frames); |
| 201 | if (written < 0) { |
| 202 | if (written == (ssize_t)NEGOTIATE) { |
| 203 | ALOGE("out_write() write to pipe returned NEGOTIATE"); |
| 204 | written = 0; |
| 205 | } else { |
| 206 | // write() returned UNDERRUN or WOULD_BLOCK, retry |
| 207 | written = sink->write(buffer, frames); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | pthread_mutex_lock(&out->dev->lock); |
| 212 | |
| 213 | out->dev->rsxSink->decStrong(buffer); |
| 214 | |
| 215 | pthread_mutex_unlock(&out->dev->lock); |
| 216 | |
| 217 | if (written > 0) { |
| 218 | // fake timing for audio output, we can't return right after pushing the data in the pipe |
| 219 | // TODO who's doing the flow control here? the wifi display link, or the audio HAL? |
| 220 | usleep(written * 1000000 / out_get_sample_rate(&stream->common)); |
| 221 | return written * audio_stream_frame_size(&stream->common);; |
| 222 | } else { |
| 223 | // error occurred, fake timing |
| 224 | usleep(frames * 1000000 / out_get_sample_rate(&stream->common)); |
| 225 | ALOGE("out_write error=%16lx", written); |
| 226 | return 0; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | static int out_get_render_position(const struct audio_stream_out *stream, |
| 231 | uint32_t *dsp_frames) |
| 232 | { |
| 233 | return -EINVAL; |
| 234 | } |
| 235 | |
| 236 | static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 237 | { |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 242 | { |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static int out_get_next_write_timestamp(const struct audio_stream_out *stream, |
| 247 | int64_t *timestamp) |
| 248 | { |
| 249 | return -EINVAL; |
| 250 | } |
| 251 | |
| 252 | /** audio_stream_in implementation **/ |
| 253 | static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| 254 | { |
| 255 | const struct submix_stream_in *in = reinterpret_cast<const struct submix_stream_in *>(stream); |
| 256 | ALOGV("in_get_sample_rate() returns %u", in->dev->config.rate); |
| 257 | return in->dev->config.rate; |
| 258 | } |
| 259 | |
| 260 | static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 261 | { |
| 262 | return -ENOSYS; |
| 263 | } |
| 264 | |
| 265 | static size_t in_get_buffer_size(const struct audio_stream *stream) |
| 266 | { |
| 267 | const struct submix_stream_in *in = reinterpret_cast<const struct submix_stream_in *>(stream); |
| 268 | ALOGV("in_get_buffer_size() returns %u", |
| 269 | in->dev->config.period_size * audio_stream_frame_size(stream)); |
| 270 | return in->dev->config.period_size * audio_stream_frame_size(stream); |
| 271 | } |
| 272 | |
| 273 | static audio_channel_mask_t in_get_channels(const struct audio_stream *stream) |
| 274 | { |
| 275 | return AUDIO_CHANNEL_IN_STEREO; |
| 276 | } |
| 277 | |
| 278 | static audio_format_t in_get_format(const struct audio_stream *stream) |
| 279 | { |
| 280 | return AUDIO_FORMAT_PCM_16_BIT; |
| 281 | } |
| 282 | |
| 283 | static int in_set_format(struct audio_stream *stream, audio_format_t format) |
| 284 | { |
| 285 | if (format != AUDIO_FORMAT_PCM_16_BIT) { |
| 286 | return -ENOSYS; |
| 287 | } else { |
| 288 | return 0; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | static int in_standby(struct audio_stream *stream) |
| 293 | { |
| 294 | // REMOTE_SUBMIX is a proxy / virtual audio device, so the notion of standby doesn't apply here |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static int in_dump(const struct audio_stream *stream, int fd) |
| 299 | { |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 304 | { |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static char * in_get_parameters(const struct audio_stream *stream, |
| 309 | const char *keys) |
| 310 | { |
| 311 | return strdup(""); |
| 312 | } |
| 313 | |
| 314 | static int in_set_gain(struct audio_stream_in *stream, float gain) |
| 315 | { |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static ssize_t in_read(struct audio_stream_in *stream, void* buffer, |
| 320 | size_t bytes) |
| 321 | { |
| 322 | ssize_t frames_read = -1977; |
| 323 | const struct submix_stream_in *in = reinterpret_cast<const struct submix_stream_in *>(stream); |
| 324 | const size_t frame_size = audio_stream_frame_size(&stream->common); |
| 325 | |
| 326 | pthread_mutex_lock(&in->dev->lock); |
| 327 | |
| 328 | PipeReader* source = in->dev->rsxSource.get(); |
| 329 | if (source != NULL) { |
| 330 | in->dev->rsxSource->incStrong(in); |
| 331 | } else { |
| 332 | pthread_mutex_unlock(&in->dev->lock); |
| 333 | usleep((bytes / frame_size) * 1000000 / in_get_sample_rate(&stream->common)); |
| 334 | memset(buffer, 0, bytes); |
| 335 | return bytes; |
| 336 | } |
| 337 | |
| 338 | pthread_mutex_unlock(&in->dev->lock); |
| 339 | |
| 340 | int attempts = MAX_READ_ATTEMPTS; |
| 341 | size_t remaining_frames = bytes / frame_size; |
| 342 | char* buff = (char*)buffer; |
| 343 | while (attempts > 0) { |
| 344 | frames_read = source->read(buff, remaining_frames, AudioBufferProvider::kInvalidPTS); |
| 345 | if (frames_read > 0) { |
| 346 | //ALOGV("in_read frames=%ld size=%u", remaining_frames, frame_size); |
| 347 | remaining_frames -= frames_read; |
| 348 | buff += frames_read * frame_size; |
| 349 | if (remaining_frames == 0) { |
| 350 | // TODO simplify code by breaking out of loop |
| 351 | |
| 352 | pthread_mutex_lock(&in->dev->lock); |
| 353 | |
| 354 | in->dev->rsxSource->decStrong(in); |
| 355 | |
| 356 | pthread_mutex_unlock(&in->dev->lock); |
| 357 | |
| 358 | return bytes; |
| 359 | } |
| 360 | } else if (frames_read == 0) { |
| 361 | // TODO sleep should be tied to how much data is expected |
| 362 | usleep(READ_ATTEMPT_SLEEP_MS*1000); |
| 363 | attempts--; |
| 364 | } else { // frames_read is an error code |
| 365 | if (frames_read != (ssize_t)OVERRUN) { |
| 366 | attempts--; |
| 367 | } |
| 368 | // else OVERRUN: error has been signaled, ok to read, do not decrement counter |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | pthread_mutex_lock(&in->dev->lock); |
| 373 | |
| 374 | in->dev->rsxSource->decStrong(in); |
| 375 | |
| 376 | pthread_mutex_unlock(&in->dev->lock); |
| 377 | |
| 378 | // TODO how to handle partial reads? |
| 379 | |
| 380 | if (frames_read < 0) { |
| 381 | ALOGE("in_read error=%16lx", frames_read); |
| 382 | } |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) |
| 387 | { |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 392 | { |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 397 | { |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | static int adev_open_output_stream(struct audio_hw_device *dev, |
| 402 | audio_io_handle_t handle, |
| 403 | audio_devices_t devices, |
| 404 | audio_output_flags_t flags, |
| 405 | struct audio_config *config, |
| 406 | struct audio_stream_out **stream_out) |
| 407 | { |
| 408 | ALOGV("adev_open_output_stream()"); |
| 409 | struct submix_audio_device *rsxadev = (struct submix_audio_device *)dev; |
| 410 | struct submix_stream_out *out; |
| 411 | int ret; |
| 412 | |
| 413 | out = (struct submix_stream_out *)calloc(1, sizeof(struct submix_stream_out)); |
| 414 | if (!out) { |
| 415 | ret = -ENOMEM; |
| 416 | goto err_open; |
| 417 | } |
| 418 | |
| 419 | pthread_mutex_lock(&rsxadev->lock); |
| 420 | |
| 421 | out->stream.common.get_sample_rate = out_get_sample_rate; |
| 422 | out->stream.common.set_sample_rate = out_set_sample_rate; |
| 423 | out->stream.common.get_buffer_size = out_get_buffer_size; |
| 424 | out->stream.common.get_channels = out_get_channels; |
| 425 | out->stream.common.get_format = out_get_format; |
| 426 | out->stream.common.set_format = out_set_format; |
| 427 | out->stream.common.standby = out_standby; |
| 428 | out->stream.common.dump = out_dump; |
| 429 | out->stream.common.set_parameters = out_set_parameters; |
| 430 | out->stream.common.get_parameters = out_get_parameters; |
| 431 | out->stream.common.add_audio_effect = out_add_audio_effect; |
| 432 | out->stream.common.remove_audio_effect = out_remove_audio_effect; |
| 433 | out->stream.get_latency = out_get_latency; |
| 434 | out->stream.set_volume = out_set_volume; |
| 435 | out->stream.write = out_write; |
| 436 | out->stream.get_render_position = out_get_render_position; |
| 437 | out->stream.get_next_write_timestamp = out_get_next_write_timestamp; |
| 438 | |
| 439 | config->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 440 | rsxadev->config.channel_mask = config->channel_mask; |
| 441 | |
| 442 | if ((config->sample_rate != 48000) || (config->sample_rate != 44100)) { |
| 443 | config->sample_rate = DEFAULT_RATE_HZ; |
| 444 | } |
| 445 | rsxadev->config.rate = config->sample_rate; |
| 446 | |
| 447 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 448 | rsxadev->config.format = config->format; |
| 449 | |
| 450 | rsxadev->config.period_size = 1024; |
| 451 | rsxadev->config.period_count = 4; |
| 452 | out->dev = rsxadev; |
| 453 | |
| 454 | *stream_out = &out->stream; |
| 455 | |
| 456 | // initialize pipe |
| 457 | { |
| 458 | ALOGV(" initializing pipe"); |
| 459 | const NBAIO_Format format = |
| 460 | config->sample_rate == 48000 ? Format_SR48_C2_I16 : Format_SR44_1_C2_I16; |
| 461 | const NBAIO_Format offers[1] = {format}; |
| 462 | size_t numCounterOffers = 0; |
| 463 | // creating a Pipe, not a MonoPipe with optional blocking set to true, so audio frames |
| 464 | // entering a full sink will overwrite the contents of the pipe. |
| 465 | Pipe* sink = new Pipe(MAX_PIPE_DEPTH_IN_FRAMES, format); |
| 466 | ssize_t index = sink->negotiate(offers, 1, NULL, numCounterOffers); |
| 467 | ALOG_ASSERT(index == 0); |
| 468 | PipeReader* source = new PipeReader(*sink); |
| 469 | numCounterOffers = 0; |
| 470 | index = source->negotiate(offers, 1, NULL, numCounterOffers); |
| 471 | ALOG_ASSERT(index == 0); |
| 472 | rsxadev->rsxSink = sink; |
| 473 | rsxadev->rsxSource = source; |
| 474 | } |
| 475 | |
| 476 | pthread_mutex_unlock(&rsxadev->lock); |
| 477 | |
| 478 | return 0; |
| 479 | |
| 480 | err_open: |
| 481 | *stream_out = NULL; |
| 482 | return ret; |
| 483 | } |
| 484 | |
| 485 | static void adev_close_output_stream(struct audio_hw_device *dev, |
| 486 | struct audio_stream_out *stream) |
| 487 | { |
| 488 | ALOGV("adev_close_output_stream()"); |
| 489 | struct submix_audio_device *rsxadev = (struct submix_audio_device *)dev; |
| 490 | |
| 491 | pthread_mutex_lock(&rsxadev->lock); |
| 492 | |
| 493 | rsxadev->rsxSink.clear(); |
| 494 | rsxadev->rsxSource.clear(); |
| 495 | free(stream); |
| 496 | |
| 497 | pthread_mutex_unlock(&rsxadev->lock); |
| 498 | } |
| 499 | |
| 500 | static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) |
| 501 | { |
| 502 | return -ENOSYS; |
| 503 | } |
| 504 | |
| 505 | static char * adev_get_parameters(const struct audio_hw_device *dev, |
| 506 | const char *keys) |
| 507 | { |
| 508 | return strdup("");; |
| 509 | } |
| 510 | |
| 511 | static int adev_init_check(const struct audio_hw_device *dev) |
| 512 | { |
| 513 | ALOGI("adev_init_check()"); |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) |
| 518 | { |
| 519 | return -ENOSYS; |
| 520 | } |
| 521 | |
| 522 | static int adev_set_master_volume(struct audio_hw_device *dev, float volume) |
| 523 | { |
| 524 | return -ENOSYS; |
| 525 | } |
| 526 | |
| 527 | static int adev_get_master_volume(struct audio_hw_device *dev, float *volume) |
| 528 | { |
| 529 | return -ENOSYS; |
| 530 | } |
| 531 | |
| 532 | static int adev_set_master_mute(struct audio_hw_device *dev, bool muted) |
| 533 | { |
| 534 | return -ENOSYS; |
| 535 | } |
| 536 | |
| 537 | static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted) |
| 538 | { |
| 539 | return -ENOSYS; |
| 540 | } |
| 541 | |
| 542 | static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) |
| 543 | { |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) |
| 548 | { |
| 549 | return -ENOSYS; |
| 550 | } |
| 551 | |
| 552 | static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) |
| 553 | { |
| 554 | return -ENOSYS; |
| 555 | } |
| 556 | |
| 557 | static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev, |
| 558 | const struct audio_config *config) |
| 559 | { |
| 560 | //### TODO correlate this with pipe parameters |
| 561 | return 4096; |
| 562 | } |
| 563 | |
| 564 | static int adev_open_input_stream(struct audio_hw_device *dev, |
| 565 | audio_io_handle_t handle, |
| 566 | audio_devices_t devices, |
| 567 | struct audio_config *config, |
| 568 | struct audio_stream_in **stream_in) |
| 569 | { |
| 570 | ALOGI("adev_open_input_stream()"); |
| 571 | |
| 572 | struct submix_audio_device *rsxadev = (struct submix_audio_device *)dev; |
| 573 | struct submix_stream_in *in; |
| 574 | int ret; |
| 575 | |
| 576 | in = (struct submix_stream_in *)calloc(1, sizeof(struct submix_stream_in)); |
| 577 | if (!in) { |
| 578 | ret = -ENOMEM; |
| 579 | goto err_open; |
| 580 | } |
| 581 | |
| 582 | pthread_mutex_lock(&rsxadev->lock); |
| 583 | |
| 584 | in->stream.common.get_sample_rate = in_get_sample_rate; |
| 585 | in->stream.common.set_sample_rate = in_set_sample_rate; |
| 586 | in->stream.common.get_buffer_size = in_get_buffer_size; |
| 587 | in->stream.common.get_channels = in_get_channels; |
| 588 | in->stream.common.get_format = in_get_format; |
| 589 | in->stream.common.set_format = in_set_format; |
| 590 | in->stream.common.standby = in_standby; |
| 591 | in->stream.common.dump = in_dump; |
| 592 | in->stream.common.set_parameters = in_set_parameters; |
| 593 | in->stream.common.get_parameters = in_get_parameters; |
| 594 | in->stream.common.add_audio_effect = in_add_audio_effect; |
| 595 | in->stream.common.remove_audio_effect = in_remove_audio_effect; |
| 596 | in->stream.set_gain = in_set_gain; |
| 597 | in->stream.read = in_read; |
| 598 | in->stream.get_input_frames_lost = in_get_input_frames_lost; |
| 599 | |
| 600 | config->channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 601 | rsxadev->config.channel_mask = config->channel_mask; |
| 602 | |
| 603 | if ((config->sample_rate != 48000) || (config->sample_rate != 44100)) { |
| 604 | config->sample_rate = DEFAULT_RATE_HZ; |
| 605 | } |
| 606 | rsxadev->config.rate = config->sample_rate; |
| 607 | |
| 608 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 609 | rsxadev->config.format = config->format; |
| 610 | |
| 611 | rsxadev->config.period_size = 1024; |
| 612 | rsxadev->config.period_count = 4; |
| 613 | |
| 614 | *stream_in = &in->stream; |
| 615 | |
| 616 | in->dev = rsxadev; |
| 617 | |
| 618 | pthread_mutex_unlock(&rsxadev->lock); |
| 619 | |
| 620 | return 0; |
| 621 | |
| 622 | err_open: |
| 623 | *stream_in = NULL; |
| 624 | return ret; |
| 625 | } |
| 626 | |
| 627 | static void adev_close_input_stream(struct audio_hw_device *dev, |
| 628 | struct audio_stream_in *stream) |
| 629 | { |
| 630 | ALOGV("adev_close_input_stream()"); |
| 631 | struct submix_audio_device *rsxadev = (struct submix_audio_device *)dev; |
| 632 | |
| 633 | pthread_mutex_lock(&rsxadev->lock); |
| 634 | |
| 635 | free(stream); |
| 636 | |
| 637 | pthread_mutex_unlock(&rsxadev->lock); |
| 638 | } |
| 639 | |
| 640 | static int adev_dump(const audio_hw_device_t *device, int fd) |
| 641 | { |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | static int adev_close(hw_device_t *device) |
| 646 | { |
| 647 | ALOGI("adev_close()"); |
| 648 | free(device); |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | static uint32_t adev_get_supported_devices(const struct audio_hw_device *dev) |
| 653 | { |
| 654 | ALOGI("adev_get_supported_devices() returns %08x", |
| 655 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX |AUDIO_DEVICE_IN_REMOTE_SUBMIX); |
| 656 | return (/* OUT */ |
| 657 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX | |
| 658 | /* IN */ |
| 659 | AUDIO_DEVICE_IN_REMOTE_SUBMIX); |
| 660 | } |
| 661 | |
| 662 | static int adev_open(const hw_module_t* module, const char* name, |
| 663 | hw_device_t** device) |
| 664 | { |
| 665 | ALOGI("adev_open(name=%s)", name); |
| 666 | struct submix_audio_device *rsxadev; |
| 667 | |
| 668 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) |
| 669 | return -EINVAL; |
| 670 | |
| 671 | rsxadev = (submix_audio_device*) calloc(1, sizeof(struct submix_audio_device)); |
| 672 | if (!rsxadev) |
| 673 | return -ENOMEM; |
| 674 | |
| 675 | rsxadev->device.common.tag = HARDWARE_DEVICE_TAG; |
| 676 | rsxadev->device.common.version = AUDIO_DEVICE_API_VERSION_1_0; |
| 677 | rsxadev->device.common.module = (struct hw_module_t *) module; |
| 678 | rsxadev->device.common.close = adev_close; |
| 679 | |
| 680 | rsxadev->device.get_supported_devices = adev_get_supported_devices; |
| 681 | rsxadev->device.init_check = adev_init_check; |
| 682 | rsxadev->device.set_voice_volume = adev_set_voice_volume; |
| 683 | rsxadev->device.set_master_volume = adev_set_master_volume; |
| 684 | rsxadev->device.get_master_volume = adev_get_master_volume; |
| 685 | rsxadev->device.set_master_mute = adev_set_master_mute; |
| 686 | rsxadev->device.get_master_mute = adev_get_master_mute; |
| 687 | rsxadev->device.set_mode = adev_set_mode; |
| 688 | rsxadev->device.set_mic_mute = adev_set_mic_mute; |
| 689 | rsxadev->device.get_mic_mute = adev_get_mic_mute; |
| 690 | rsxadev->device.set_parameters = adev_set_parameters; |
| 691 | rsxadev->device.get_parameters = adev_get_parameters; |
| 692 | rsxadev->device.get_input_buffer_size = adev_get_input_buffer_size; |
| 693 | rsxadev->device.open_output_stream = adev_open_output_stream; |
| 694 | rsxadev->device.close_output_stream = adev_close_output_stream; |
| 695 | rsxadev->device.open_input_stream = adev_open_input_stream; |
| 696 | rsxadev->device.close_input_stream = adev_close_input_stream; |
| 697 | rsxadev->device.dump = adev_dump; |
| 698 | |
| 699 | *device = &rsxadev->device.common; |
| 700 | |
| 701 | return 0; |
| 702 | } |
| 703 | |
| 704 | static struct hw_module_methods_t hal_module_methods = { |
| 705 | /* open */ adev_open, |
| 706 | }; |
| 707 | |
| 708 | struct audio_module HAL_MODULE_INFO_SYM = { |
| 709 | /* common */ { |
| 710 | /* tag */ HARDWARE_MODULE_TAG, |
| 711 | /* module_api_version */ AUDIO_MODULE_API_VERSION_0_1, |
| 712 | /* hal_api_version */ HARDWARE_HAL_API_VERSION, |
| 713 | /* id */ AUDIO_HARDWARE_MODULE_ID, |
| 714 | /* name */ "Wifi Display audio HAL", |
| 715 | /* author */ "The Android Open Source Project", |
| 716 | /* methods */ &hal_module_methods, |
| 717 | /* dso */ NULL, |
| 718 | /* reserved */ { 0 }, |
| 719 | }, |
| 720 | }; |
| 721 | |
| 722 | } //namespace android |
| 723 | |
| 724 | } //extern "C" |