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