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" |
Jean-Michel Trivi | 35a2c16 | 2012-09-17 10:13:26 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 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 | |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 34 | #include <media/nbaio/MonoPipe.h> |
| 35 | #include <media/nbaio/MonoPipeReader.h> |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 36 | #include <media/AudioBufferProvider.h> |
| 37 | |
Jean-Michel Trivi | d441303 | 2012-09-30 11:08:06 -0700 | [diff] [blame^] | 38 | #include <utils/String8.h> |
| 39 | #include <media/AudioParameter.h> |
| 40 | |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 41 | extern "C" { |
| 42 | |
| 43 | namespace android { |
| 44 | |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 45 | #define MAX_PIPE_DEPTH_IN_FRAMES (1024*8) |
| 46 | // The duration of MAX_READ_ATTEMPTS * READ_ATTEMPT_SLEEP_MS must be stricly inferior to |
| 47 | // the duration of a record buffer at the current record sample rate (of the device, not of |
| 48 | // the recording itself). Here we have: |
| 49 | // 3 * 5ms = 15ms < 1024 frames * 1000 / 48000 = 21.333ms |
Jean-Michel Trivi | 6acd966 | 2012-09-11 19:19:08 -0700 | [diff] [blame] | 50 | #define MAX_READ_ATTEMPTS 3 |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 51 | #define READ_ATTEMPT_SLEEP_MS 5 // 5ms between two read attempts when pipe is empty |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 52 | #define DEFAULT_RATE_HZ 48000 // default sample rate |
| 53 | |
| 54 | struct submix_config { |
| 55 | audio_format_t format; |
| 56 | audio_channel_mask_t channel_mask; |
| 57 | unsigned int rate; // sample rate for the device |
| 58 | unsigned int period_size; // size of the audio pipe is period_size * period_count in frames |
| 59 | unsigned int period_count; |
| 60 | }; |
| 61 | |
| 62 | struct submix_audio_device { |
| 63 | struct audio_hw_device device; |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 64 | bool output_standby; |
| 65 | bool input_standby; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 66 | submix_config config; |
| 67 | // Pipe variables: they handle the ring buffer that "pipes" audio: |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 68 | // - from the submix virtual audio output == what needs to be played |
| 69 | // remotely, seen as an output for AudioFlinger |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 70 | // - to the virtual audio source == what is captured by the component |
| 71 | // which "records" the submix / virtual audio source, and handles it as needed. |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 72 | // A usecase example is one where the component capturing the audio is then sending it over |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 73 | // Wifi for presentation on a remote Wifi Display device (e.g. a dongle attached to a TV, or a |
| 74 | // TV with Wifi Display capabilities), or to a wireless audio player. |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 75 | sp<MonoPipe> rsxSink; |
| 76 | sp<MonoPipeReader> rsxSource; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 77 | |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 78 | // device lock, also used to protect access to the audio pipe |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 79 | pthread_mutex_t lock; |
| 80 | }; |
| 81 | |
| 82 | struct submix_stream_out { |
| 83 | struct audio_stream_out stream; |
| 84 | struct submix_audio_device *dev; |
| 85 | }; |
| 86 | |
| 87 | struct submix_stream_in { |
| 88 | struct audio_stream_in stream; |
| 89 | struct submix_audio_device *dev; |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 90 | bool output_standby; // output standby state as seen from record thread |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 91 | |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 92 | // wall clock when recording starts |
| 93 | struct timespec record_start_time; |
| 94 | // how many frames have been requested to be read |
| 95 | int64_t read_counter_frames; |
| 96 | }; |
Jean-Michel Trivi | 6acd966 | 2012-09-11 19:19:08 -0700 | [diff] [blame] | 97 | |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 98 | |
| 99 | /* audio HAL functions */ |
| 100 | |
| 101 | static uint32_t out_get_sample_rate(const struct audio_stream *stream) |
| 102 | { |
| 103 | const struct submix_stream_out *out = |
| 104 | reinterpret_cast<const struct submix_stream_out *>(stream); |
| 105 | uint32_t out_rate = out->dev->config.rate; |
| 106 | //ALOGV("out_get_sample_rate() returns %u", out_rate); |
| 107 | return out_rate; |
| 108 | } |
| 109 | |
| 110 | static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 111 | { |
| 112 | if ((rate != 44100) && (rate != 48000)) { |
| 113 | ALOGE("out_set_sample_rate(rate=%u) rate unsupported", rate); |
| 114 | return -ENOSYS; |
| 115 | } |
| 116 | struct submix_stream_out *out = reinterpret_cast<struct submix_stream_out *>(stream); |
| 117 | //ALOGV("out_set_sample_rate(rate=%u)", rate); |
| 118 | out->dev->config.rate = rate; |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static size_t out_get_buffer_size(const struct audio_stream *stream) |
| 123 | { |
| 124 | const struct submix_stream_out *out = |
| 125 | reinterpret_cast<const struct submix_stream_out *>(stream); |
| 126 | const struct submix_config& config_out = out->dev->config; |
| 127 | size_t buffer_size = config_out.period_size * popcount(config_out.channel_mask) |
| 128 | * sizeof(int16_t); // only PCM 16bit |
| 129 | //ALOGV("out_get_buffer_size() returns %u, period size=%u", |
| 130 | // buffer_size, config_out.period_size); |
| 131 | return buffer_size; |
| 132 | } |
| 133 | |
| 134 | static audio_channel_mask_t out_get_channels(const struct audio_stream *stream) |
| 135 | { |
| 136 | const struct submix_stream_out *out = |
| 137 | reinterpret_cast<const struct submix_stream_out *>(stream); |
| 138 | uint32_t channels = out->dev->config.channel_mask; |
| 139 | //ALOGV("out_get_channels() returns %08x", channels); |
| 140 | return channels; |
| 141 | } |
| 142 | |
| 143 | static audio_format_t out_get_format(const struct audio_stream *stream) |
| 144 | { |
| 145 | return AUDIO_FORMAT_PCM_16_BIT; |
| 146 | } |
| 147 | |
| 148 | static int out_set_format(struct audio_stream *stream, audio_format_t format) |
| 149 | { |
| 150 | if (format != AUDIO_FORMAT_PCM_16_BIT) { |
| 151 | return -ENOSYS; |
| 152 | } else { |
| 153 | return 0; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | static int out_standby(struct audio_stream *stream) |
| 158 | { |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 159 | ALOGI("out_standby()"); |
| 160 | |
| 161 | const struct submix_stream_out *out = reinterpret_cast<const struct submix_stream_out *>(stream); |
| 162 | |
| 163 | pthread_mutex_lock(&out->dev->lock); |
| 164 | |
| 165 | out->dev->output_standby = true; |
| 166 | |
| 167 | pthread_mutex_unlock(&out->dev->lock); |
| 168 | |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static int out_dump(const struct audio_stream *stream, int fd) |
| 173 | { |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 178 | { |
Jean-Michel Trivi | d441303 | 2012-09-30 11:08:06 -0700 | [diff] [blame^] | 179 | int exiting = -1; |
| 180 | AudioParameter parms = AudioParameter(String8(kvpairs)); |
| 181 | // FIXME this is using hard-coded strings but in the future, this functionality will be |
| 182 | // converted to use audio HAL extensions required to support tunneling |
| 183 | if ((parms.getInt(String8("exiting"), exiting) == NO_ERROR) && (exiting > 0)) { |
| 184 | const struct submix_stream_out *out = |
| 185 | reinterpret_cast<const struct submix_stream_out *>(stream); |
| 186 | |
| 187 | pthread_mutex_lock(&out->dev->lock); |
| 188 | |
| 189 | MonoPipe* sink = out->dev->rsxSink.get(); |
| 190 | if (sink != NULL) { |
| 191 | sink->incStrong(out); |
| 192 | } else { |
| 193 | pthread_mutex_unlock(&out->dev->lock); |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | ALOGI("shutdown"); |
| 198 | sink->shutdown(true); |
| 199 | |
| 200 | sink->decStrong(out); |
| 201 | |
| 202 | pthread_mutex_unlock(&out->dev->lock); |
| 203 | } |
| 204 | |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | static char * out_get_parameters(const struct audio_stream *stream, const char *keys) |
| 209 | { |
| 210 | return strdup(""); |
| 211 | } |
| 212 | |
| 213 | static uint32_t out_get_latency(const struct audio_stream_out *stream) |
| 214 | { |
| 215 | const struct submix_stream_out *out = |
| 216 | reinterpret_cast<const struct submix_stream_out *>(stream); |
| 217 | const struct submix_config * config_out = &(out->dev->config); |
| 218 | uint32_t latency = (MAX_PIPE_DEPTH_IN_FRAMES * 1000) / config_out->rate; |
| 219 | ALOGV("out_get_latency() returns %u", latency); |
| 220 | return latency; |
| 221 | } |
| 222 | |
| 223 | static int out_set_volume(struct audio_stream_out *stream, float left, |
| 224 | float right) |
| 225 | { |
| 226 | return -ENOSYS; |
| 227 | } |
| 228 | |
| 229 | static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, |
| 230 | size_t bytes) |
| 231 | { |
| 232 | //ALOGV("out_write(bytes=%d)", bytes); |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 233 | ssize_t written_frames = 0; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 234 | struct submix_stream_out *out = reinterpret_cast<struct submix_stream_out *>(stream); |
| 235 | |
| 236 | pthread_mutex_lock(&out->dev->lock); |
| 237 | |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 238 | out->dev->output_standby = false; |
| 239 | |
| 240 | MonoPipe* sink = out->dev->rsxSink.get(); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 241 | if (sink != NULL) { |
Jean-Michel Trivi | 97c262f | 2012-09-17 18:27:56 -0700 | [diff] [blame] | 242 | sink->incStrong(buffer); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 243 | } else { |
| 244 | pthread_mutex_unlock(&out->dev->lock); |
| 245 | ALOGE("out_write without a pipe!"); |
| 246 | ALOG_ASSERT("out_write without a pipe!"); |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | pthread_mutex_unlock(&out->dev->lock); |
| 251 | |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 252 | const size_t frame_size = audio_stream_frame_size(&stream->common); |
| 253 | const size_t frames = bytes / frame_size; |
| 254 | written_frames = sink->write(buffer, frames); |
| 255 | if (written_frames < 0) { |
| 256 | if (written_frames == (ssize_t)NEGOTIATE) { |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 257 | ALOGE("out_write() write to pipe returned NEGOTIATE"); |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 258 | |
| 259 | pthread_mutex_lock(&out->dev->lock); |
Jean-Michel Trivi | 97c262f | 2012-09-17 18:27:56 -0700 | [diff] [blame] | 260 | sink->decStrong(buffer); |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 261 | pthread_mutex_unlock(&out->dev->lock); |
| 262 | |
| 263 | written_frames = 0; |
Jean-Michel Trivi | 6acd966 | 2012-09-11 19:19:08 -0700 | [diff] [blame] | 264 | return 0; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 265 | } else { |
| 266 | // write() returned UNDERRUN or WOULD_BLOCK, retry |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 267 | ALOGE("out_write() write to pipe returned unexpected %16lx", written_frames); |
| 268 | written_frames = sink->write(buffer, frames); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
| 272 | pthread_mutex_lock(&out->dev->lock); |
| 273 | |
Jean-Michel Trivi | 97c262f | 2012-09-17 18:27:56 -0700 | [diff] [blame] | 274 | sink->decStrong(buffer); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 275 | |
| 276 | pthread_mutex_unlock(&out->dev->lock); |
| 277 | |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 278 | if (written_frames < 0) { |
| 279 | ALOGE("out_write() failed writing to pipe with %16lx", written_frames); |
| 280 | return 0; |
| 281 | } else { |
| 282 | ALOGV("out_write() wrote %lu bytes)", written_frames * frame_size); |
| 283 | return written_frames * frame_size; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| 287 | static int out_get_render_position(const struct audio_stream_out *stream, |
| 288 | uint32_t *dsp_frames) |
| 289 | { |
| 290 | return -EINVAL; |
| 291 | } |
| 292 | |
| 293 | static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 294 | { |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 299 | { |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static int out_get_next_write_timestamp(const struct audio_stream_out *stream, |
| 304 | int64_t *timestamp) |
| 305 | { |
| 306 | return -EINVAL; |
| 307 | } |
| 308 | |
| 309 | /** audio_stream_in implementation **/ |
| 310 | static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| 311 | { |
| 312 | const struct submix_stream_in *in = reinterpret_cast<const struct submix_stream_in *>(stream); |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 313 | //ALOGV("in_get_sample_rate() returns %u", in->dev->config.rate); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 314 | return in->dev->config.rate; |
| 315 | } |
| 316 | |
| 317 | static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 318 | { |
| 319 | return -ENOSYS; |
| 320 | } |
| 321 | |
| 322 | static size_t in_get_buffer_size(const struct audio_stream *stream) |
| 323 | { |
| 324 | const struct submix_stream_in *in = reinterpret_cast<const struct submix_stream_in *>(stream); |
| 325 | ALOGV("in_get_buffer_size() returns %u", |
| 326 | in->dev->config.period_size * audio_stream_frame_size(stream)); |
| 327 | return in->dev->config.period_size * audio_stream_frame_size(stream); |
| 328 | } |
| 329 | |
| 330 | static audio_channel_mask_t in_get_channels(const struct audio_stream *stream) |
| 331 | { |
| 332 | return AUDIO_CHANNEL_IN_STEREO; |
| 333 | } |
| 334 | |
| 335 | static audio_format_t in_get_format(const struct audio_stream *stream) |
| 336 | { |
| 337 | return AUDIO_FORMAT_PCM_16_BIT; |
| 338 | } |
| 339 | |
| 340 | static int in_set_format(struct audio_stream *stream, audio_format_t format) |
| 341 | { |
| 342 | if (format != AUDIO_FORMAT_PCM_16_BIT) { |
| 343 | return -ENOSYS; |
| 344 | } else { |
| 345 | return 0; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | static int in_standby(struct audio_stream *stream) |
| 350 | { |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 351 | ALOGI("in_standby()"); |
| 352 | const struct submix_stream_in *in = reinterpret_cast<const struct submix_stream_in *>(stream); |
| 353 | |
| 354 | pthread_mutex_lock(&in->dev->lock); |
| 355 | |
| 356 | in->dev->input_standby = true; |
| 357 | |
| 358 | pthread_mutex_unlock(&in->dev->lock); |
| 359 | |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | static int in_dump(const struct audio_stream *stream, int fd) |
| 364 | { |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 369 | { |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | static char * in_get_parameters(const struct audio_stream *stream, |
| 374 | const char *keys) |
| 375 | { |
| 376 | return strdup(""); |
| 377 | } |
| 378 | |
| 379 | static int in_set_gain(struct audio_stream_in *stream, float gain) |
| 380 | { |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | static ssize_t in_read(struct audio_stream_in *stream, void* buffer, |
| 385 | size_t bytes) |
| 386 | { |
Jean-Michel Trivi | 6acd966 | 2012-09-11 19:19:08 -0700 | [diff] [blame] | 387 | //ALOGV("in_read bytes=%u", bytes); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 388 | ssize_t frames_read = -1977; |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 389 | struct submix_stream_in *in = reinterpret_cast<struct submix_stream_in *>(stream); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 390 | const size_t frame_size = audio_stream_frame_size(&stream->common); |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 391 | const size_t frames_to_read = bytes / frame_size; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 392 | |
| 393 | pthread_mutex_lock(&in->dev->lock); |
| 394 | |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 395 | const bool output_standby_transition = (in->output_standby != in->dev->output_standby); |
| 396 | in->output_standby = in->dev->output_standby; |
| 397 | |
| 398 | if (in->dev->input_standby || output_standby_transition) { |
| 399 | in->dev->input_standby = false; |
| 400 | // keep track of when we exit input standby (== first read == start "real recording") |
| 401 | // or when we start recording silence, and reset projected time |
| 402 | int rc = clock_gettime(CLOCK_MONOTONIC, &in->record_start_time); |
| 403 | if (rc == 0) { |
| 404 | in->read_counter_frames = 0; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | in->read_counter_frames += frames_to_read; |
| 409 | |
| 410 | MonoPipeReader* source = in->dev->rsxSource.get(); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 411 | if (source != NULL) { |
Jean-Michel Trivi | 97c262f | 2012-09-17 18:27:56 -0700 | [diff] [blame] | 412 | source->incStrong(buffer); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 413 | } else { |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 414 | ALOGE("no audio pipe yet we're trying to read!"); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 415 | pthread_mutex_unlock(&in->dev->lock); |
| 416 | usleep((bytes / frame_size) * 1000000 / in_get_sample_rate(&stream->common)); |
| 417 | memset(buffer, 0, bytes); |
| 418 | return bytes; |
| 419 | } |
| 420 | |
| 421 | pthread_mutex_unlock(&in->dev->lock); |
| 422 | |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 423 | // read the data from the pipe (it's non blocking) |
| 424 | size_t remaining_frames = frames_to_read; |
| 425 | int attempts = 0; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 426 | char* buff = (char*)buffer; |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 427 | while ((remaining_frames > 0) && (attempts < MAX_READ_ATTEMPTS)) { |
| 428 | attempts++; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 429 | frames_read = source->read(buff, remaining_frames, AudioBufferProvider::kInvalidPTS); |
| 430 | if (frames_read > 0) { |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 431 | remaining_frames -= frames_read; |
| 432 | buff += frames_read * frame_size; |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 433 | //ALOGV(" in_read (att=%d) got %ld frames, remaining=%u", |
| 434 | // attempts, frames_read, remaining_frames); |
| 435 | } else { |
| 436 | //ALOGE(" in_read read returned %ld", frames_read); |
| 437 | usleep(READ_ATTEMPT_SLEEP_MS * 1000); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 441 | // done using the source |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 442 | pthread_mutex_lock(&in->dev->lock); |
| 443 | |
Jean-Michel Trivi | 97c262f | 2012-09-17 18:27:56 -0700 | [diff] [blame] | 444 | source->decStrong(buffer); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 445 | |
| 446 | pthread_mutex_unlock(&in->dev->lock); |
| 447 | |
Jean-Michel Trivi | 6acd966 | 2012-09-11 19:19:08 -0700 | [diff] [blame] | 448 | if (remaining_frames > 0) { |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 449 | ALOGV(" remaining_frames = %d", remaining_frames); |
Jean-Michel Trivi | 6acd966 | 2012-09-11 19:19:08 -0700 | [diff] [blame] | 450 | memset(((char*)buffer)+ bytes - (remaining_frames * frame_size), 0, |
| 451 | remaining_frames * frame_size); |
Jean-Michel Trivi | 6acd966 | 2012-09-11 19:19:08 -0700 | [diff] [blame] | 452 | } |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 453 | |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 454 | // compute how much we need to sleep after reading the data by comparing the wall clock with |
| 455 | // the projected time at which we should return. |
| 456 | struct timespec time_after_read;// wall clock after reading from the pipe |
| 457 | struct timespec record_duration;// observed record duration |
| 458 | int rc = clock_gettime(CLOCK_MONOTONIC, &time_after_read); |
| 459 | const uint32_t sample_rate = in_get_sample_rate(&stream->common); |
| 460 | if (rc == 0) { |
| 461 | // for how long have we been recording? |
| 462 | record_duration.tv_sec = time_after_read.tv_sec - in->record_start_time.tv_sec; |
| 463 | record_duration.tv_nsec = time_after_read.tv_nsec - in->record_start_time.tv_nsec; |
| 464 | if (record_duration.tv_nsec < 0) { |
| 465 | record_duration.tv_sec--; |
| 466 | record_duration.tv_nsec += 1000000000; |
| 467 | } |
| 468 | |
| 469 | // read_counter_frames contains the number of frames that have been read since the beginning |
| 470 | // of recording (including this call): it's converted to usec and compared to how long we've |
| 471 | // been recording for, which gives us how long we must wait to sync the projected recording |
| 472 | // time, and the observed recording time |
| 473 | long projected_vs_observed_offset_us = |
| 474 | ((int64_t)(in->read_counter_frames |
| 475 | - (record_duration.tv_sec*sample_rate))) |
| 476 | * 1000000 / sample_rate |
| 477 | - (record_duration.tv_nsec / 1000); |
| 478 | |
| 479 | ALOGV(" record duration %5lds %3ldms, will wait: %7ldus", |
| 480 | record_duration.tv_sec, record_duration.tv_nsec/1000000, |
| 481 | projected_vs_observed_offset_us); |
| 482 | if (projected_vs_observed_offset_us > 0) { |
| 483 | usleep(projected_vs_observed_offset_us); |
| 484 | } |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 485 | } |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 486 | |
| 487 | |
| 488 | ALOGV("in_read returns %d", bytes); |
| 489 | return bytes; |
| 490 | |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) |
| 494 | { |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 499 | { |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 504 | { |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | static int adev_open_output_stream(struct audio_hw_device *dev, |
| 509 | audio_io_handle_t handle, |
| 510 | audio_devices_t devices, |
| 511 | audio_output_flags_t flags, |
| 512 | struct audio_config *config, |
| 513 | struct audio_stream_out **stream_out) |
| 514 | { |
| 515 | ALOGV("adev_open_output_stream()"); |
| 516 | struct submix_audio_device *rsxadev = (struct submix_audio_device *)dev; |
| 517 | struct submix_stream_out *out; |
| 518 | int ret; |
| 519 | |
| 520 | out = (struct submix_stream_out *)calloc(1, sizeof(struct submix_stream_out)); |
| 521 | if (!out) { |
| 522 | ret = -ENOMEM; |
| 523 | goto err_open; |
| 524 | } |
| 525 | |
| 526 | pthread_mutex_lock(&rsxadev->lock); |
| 527 | |
| 528 | out->stream.common.get_sample_rate = out_get_sample_rate; |
| 529 | out->stream.common.set_sample_rate = out_set_sample_rate; |
| 530 | out->stream.common.get_buffer_size = out_get_buffer_size; |
| 531 | out->stream.common.get_channels = out_get_channels; |
| 532 | out->stream.common.get_format = out_get_format; |
| 533 | out->stream.common.set_format = out_set_format; |
| 534 | out->stream.common.standby = out_standby; |
| 535 | out->stream.common.dump = out_dump; |
| 536 | out->stream.common.set_parameters = out_set_parameters; |
| 537 | out->stream.common.get_parameters = out_get_parameters; |
| 538 | out->stream.common.add_audio_effect = out_add_audio_effect; |
| 539 | out->stream.common.remove_audio_effect = out_remove_audio_effect; |
| 540 | out->stream.get_latency = out_get_latency; |
| 541 | out->stream.set_volume = out_set_volume; |
| 542 | out->stream.write = out_write; |
| 543 | out->stream.get_render_position = out_get_render_position; |
| 544 | out->stream.get_next_write_timestamp = out_get_next_write_timestamp; |
| 545 | |
| 546 | config->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 547 | rsxadev->config.channel_mask = config->channel_mask; |
| 548 | |
| 549 | if ((config->sample_rate != 48000) || (config->sample_rate != 44100)) { |
| 550 | config->sample_rate = DEFAULT_RATE_HZ; |
| 551 | } |
| 552 | rsxadev->config.rate = config->sample_rate; |
| 553 | |
| 554 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 555 | rsxadev->config.format = config->format; |
| 556 | |
| 557 | rsxadev->config.period_size = 1024; |
| 558 | rsxadev->config.period_count = 4; |
| 559 | out->dev = rsxadev; |
| 560 | |
| 561 | *stream_out = &out->stream; |
| 562 | |
| 563 | // initialize pipe |
| 564 | { |
| 565 | ALOGV(" initializing pipe"); |
| 566 | const NBAIO_Format format = |
| 567 | config->sample_rate == 48000 ? Format_SR48_C2_I16 : Format_SR44_1_C2_I16; |
| 568 | const NBAIO_Format offers[1] = {format}; |
| 569 | size_t numCounterOffers = 0; |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 570 | // creating a MonoPipe with optional blocking set to true. |
| 571 | MonoPipe* sink = new MonoPipe(MAX_PIPE_DEPTH_IN_FRAMES, format, true/*writeCanBlock*/); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 572 | ssize_t index = sink->negotiate(offers, 1, NULL, numCounterOffers); |
| 573 | ALOG_ASSERT(index == 0); |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 574 | MonoPipeReader* source = new MonoPipeReader(sink); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 575 | numCounterOffers = 0; |
| 576 | index = source->negotiate(offers, 1, NULL, numCounterOffers); |
| 577 | ALOG_ASSERT(index == 0); |
| 578 | rsxadev->rsxSink = sink; |
| 579 | rsxadev->rsxSource = source; |
| 580 | } |
| 581 | |
| 582 | pthread_mutex_unlock(&rsxadev->lock); |
| 583 | |
| 584 | return 0; |
| 585 | |
| 586 | err_open: |
| 587 | *stream_out = NULL; |
| 588 | return ret; |
| 589 | } |
| 590 | |
| 591 | static void adev_close_output_stream(struct audio_hw_device *dev, |
| 592 | struct audio_stream_out *stream) |
| 593 | { |
| 594 | ALOGV("adev_close_output_stream()"); |
| 595 | struct submix_audio_device *rsxadev = (struct submix_audio_device *)dev; |
| 596 | |
| 597 | pthread_mutex_lock(&rsxadev->lock); |
| 598 | |
| 599 | rsxadev->rsxSink.clear(); |
| 600 | rsxadev->rsxSource.clear(); |
| 601 | free(stream); |
| 602 | |
| 603 | pthread_mutex_unlock(&rsxadev->lock); |
| 604 | } |
| 605 | |
| 606 | static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) |
| 607 | { |
| 608 | return -ENOSYS; |
| 609 | } |
| 610 | |
| 611 | static char * adev_get_parameters(const struct audio_hw_device *dev, |
| 612 | const char *keys) |
| 613 | { |
| 614 | return strdup("");; |
| 615 | } |
| 616 | |
| 617 | static int adev_init_check(const struct audio_hw_device *dev) |
| 618 | { |
| 619 | ALOGI("adev_init_check()"); |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) |
| 624 | { |
| 625 | return -ENOSYS; |
| 626 | } |
| 627 | |
| 628 | static int adev_set_master_volume(struct audio_hw_device *dev, float volume) |
| 629 | { |
| 630 | return -ENOSYS; |
| 631 | } |
| 632 | |
| 633 | static int adev_get_master_volume(struct audio_hw_device *dev, float *volume) |
| 634 | { |
| 635 | return -ENOSYS; |
| 636 | } |
| 637 | |
| 638 | static int adev_set_master_mute(struct audio_hw_device *dev, bool muted) |
| 639 | { |
| 640 | return -ENOSYS; |
| 641 | } |
| 642 | |
| 643 | static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted) |
| 644 | { |
| 645 | return -ENOSYS; |
| 646 | } |
| 647 | |
| 648 | static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) |
| 649 | { |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) |
| 654 | { |
| 655 | return -ENOSYS; |
| 656 | } |
| 657 | |
| 658 | static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) |
| 659 | { |
| 660 | return -ENOSYS; |
| 661 | } |
| 662 | |
| 663 | static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev, |
| 664 | const struct audio_config *config) |
| 665 | { |
| 666 | //### TODO correlate this with pipe parameters |
| 667 | return 4096; |
| 668 | } |
| 669 | |
| 670 | static int adev_open_input_stream(struct audio_hw_device *dev, |
| 671 | audio_io_handle_t handle, |
| 672 | audio_devices_t devices, |
| 673 | struct audio_config *config, |
| 674 | struct audio_stream_in **stream_in) |
| 675 | { |
| 676 | ALOGI("adev_open_input_stream()"); |
| 677 | |
| 678 | struct submix_audio_device *rsxadev = (struct submix_audio_device *)dev; |
| 679 | struct submix_stream_in *in; |
| 680 | int ret; |
| 681 | |
| 682 | in = (struct submix_stream_in *)calloc(1, sizeof(struct submix_stream_in)); |
| 683 | if (!in) { |
| 684 | ret = -ENOMEM; |
| 685 | goto err_open; |
| 686 | } |
| 687 | |
| 688 | pthread_mutex_lock(&rsxadev->lock); |
| 689 | |
| 690 | in->stream.common.get_sample_rate = in_get_sample_rate; |
| 691 | in->stream.common.set_sample_rate = in_set_sample_rate; |
| 692 | in->stream.common.get_buffer_size = in_get_buffer_size; |
| 693 | in->stream.common.get_channels = in_get_channels; |
| 694 | in->stream.common.get_format = in_get_format; |
| 695 | in->stream.common.set_format = in_set_format; |
| 696 | in->stream.common.standby = in_standby; |
| 697 | in->stream.common.dump = in_dump; |
| 698 | in->stream.common.set_parameters = in_set_parameters; |
| 699 | in->stream.common.get_parameters = in_get_parameters; |
| 700 | in->stream.common.add_audio_effect = in_add_audio_effect; |
| 701 | in->stream.common.remove_audio_effect = in_remove_audio_effect; |
| 702 | in->stream.set_gain = in_set_gain; |
| 703 | in->stream.read = in_read; |
| 704 | in->stream.get_input_frames_lost = in_get_input_frames_lost; |
| 705 | |
| 706 | config->channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 707 | rsxadev->config.channel_mask = config->channel_mask; |
| 708 | |
| 709 | if ((config->sample_rate != 48000) || (config->sample_rate != 44100)) { |
| 710 | config->sample_rate = DEFAULT_RATE_HZ; |
| 711 | } |
| 712 | rsxadev->config.rate = config->sample_rate; |
| 713 | |
| 714 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 715 | rsxadev->config.format = config->format; |
| 716 | |
| 717 | rsxadev->config.period_size = 1024; |
| 718 | rsxadev->config.period_count = 4; |
| 719 | |
| 720 | *stream_in = &in->stream; |
| 721 | |
| 722 | in->dev = rsxadev; |
| 723 | |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 724 | in->read_counter_frames = 0; |
| 725 | in->output_standby = rsxadev->output_standby; |
| 726 | |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 727 | pthread_mutex_unlock(&rsxadev->lock); |
| 728 | |
| 729 | return 0; |
| 730 | |
| 731 | err_open: |
| 732 | *stream_in = NULL; |
| 733 | return ret; |
| 734 | } |
| 735 | |
| 736 | static void adev_close_input_stream(struct audio_hw_device *dev, |
| 737 | struct audio_stream_in *stream) |
| 738 | { |
| 739 | ALOGV("adev_close_input_stream()"); |
| 740 | struct submix_audio_device *rsxadev = (struct submix_audio_device *)dev; |
| 741 | |
| 742 | pthread_mutex_lock(&rsxadev->lock); |
| 743 | |
| 744 | free(stream); |
| 745 | |
| 746 | pthread_mutex_unlock(&rsxadev->lock); |
| 747 | } |
| 748 | |
| 749 | static int adev_dump(const audio_hw_device_t *device, int fd) |
| 750 | { |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | static int adev_close(hw_device_t *device) |
| 755 | { |
| 756 | ALOGI("adev_close()"); |
| 757 | free(device); |
| 758 | return 0; |
| 759 | } |
| 760 | |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 761 | static int adev_open(const hw_module_t* module, const char* name, |
| 762 | hw_device_t** device) |
| 763 | { |
| 764 | ALOGI("adev_open(name=%s)", name); |
| 765 | struct submix_audio_device *rsxadev; |
| 766 | |
| 767 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) |
| 768 | return -EINVAL; |
| 769 | |
| 770 | rsxadev = (submix_audio_device*) calloc(1, sizeof(struct submix_audio_device)); |
| 771 | if (!rsxadev) |
| 772 | return -ENOMEM; |
| 773 | |
| 774 | rsxadev->device.common.tag = HARDWARE_DEVICE_TAG; |
Eric Laurent | 5d85c53 | 2012-09-10 10:36:09 -0700 | [diff] [blame] | 775 | rsxadev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 776 | rsxadev->device.common.module = (struct hw_module_t *) module; |
| 777 | rsxadev->device.common.close = adev_close; |
| 778 | |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 779 | rsxadev->device.init_check = adev_init_check; |
| 780 | rsxadev->device.set_voice_volume = adev_set_voice_volume; |
| 781 | rsxadev->device.set_master_volume = adev_set_master_volume; |
| 782 | rsxadev->device.get_master_volume = adev_get_master_volume; |
| 783 | rsxadev->device.set_master_mute = adev_set_master_mute; |
| 784 | rsxadev->device.get_master_mute = adev_get_master_mute; |
| 785 | rsxadev->device.set_mode = adev_set_mode; |
| 786 | rsxadev->device.set_mic_mute = adev_set_mic_mute; |
| 787 | rsxadev->device.get_mic_mute = adev_get_mic_mute; |
| 788 | rsxadev->device.set_parameters = adev_set_parameters; |
| 789 | rsxadev->device.get_parameters = adev_get_parameters; |
| 790 | rsxadev->device.get_input_buffer_size = adev_get_input_buffer_size; |
| 791 | rsxadev->device.open_output_stream = adev_open_output_stream; |
| 792 | rsxadev->device.close_output_stream = adev_close_output_stream; |
| 793 | rsxadev->device.open_input_stream = adev_open_input_stream; |
| 794 | rsxadev->device.close_input_stream = adev_close_input_stream; |
| 795 | rsxadev->device.dump = adev_dump; |
| 796 | |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 797 | rsxadev->input_standby = true; |
| 798 | rsxadev->output_standby = true; |
| 799 | |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 800 | *device = &rsxadev->device.common; |
| 801 | |
| 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | static struct hw_module_methods_t hal_module_methods = { |
| 806 | /* open */ adev_open, |
| 807 | }; |
| 808 | |
| 809 | struct audio_module HAL_MODULE_INFO_SYM = { |
| 810 | /* common */ { |
| 811 | /* tag */ HARDWARE_MODULE_TAG, |
| 812 | /* module_api_version */ AUDIO_MODULE_API_VERSION_0_1, |
| 813 | /* hal_api_version */ HARDWARE_HAL_API_VERSION, |
| 814 | /* id */ AUDIO_HARDWARE_MODULE_ID, |
| 815 | /* name */ "Wifi Display audio HAL", |
| 816 | /* author */ "The Android Open Source Project", |
| 817 | /* methods */ &hal_module_methods, |
| 818 | /* dso */ NULL, |
| 819 | /* reserved */ { 0 }, |
| 820 | }, |
| 821 | }; |
| 822 | |
| 823 | } //namespace android |
| 824 | |
| 825 | } //extern "C" |