Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 "audio_hw_default" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | #include <errno.h> |
Elliott Hughes | 07c0855 | 2015-01-29 21:19:10 -0800 | [diff] [blame] | 21 | #include <malloc.h> |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 22 | #include <pthread.h> |
| 23 | #include <stdint.h> |
Mark Salyzyn | d88dfe8 | 2017-04-11 08:56:09 -0700 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <time.h> |
| 27 | #include <unistd.h> |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 28 | |
Steven Moreland | 02b5d16 | 2017-04-11 09:36:38 -0700 | [diff] [blame] | 29 | #include <log/log.h> |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 30 | |
Mark Salyzyn | d88dfe8 | 2017-04-11 08:56:09 -0700 | [diff] [blame] | 31 | #include <hardware/audio.h> |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 32 | #include <hardware/hardware.h> |
Dima Zavin | aa21172 | 2011-05-11 14:15:53 -0700 | [diff] [blame] | 33 | #include <system/audio.h> |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 34 | |
John Muir | 001e6f4 | 2018-01-19 16:50:25 -0800 | [diff] [blame] | 35 | #define STUB_DEFAULT_SAMPLE_RATE 48000 |
| 36 | #define STUB_DEFAULT_AUDIO_FORMAT AUDIO_FORMAT_PCM_16_BIT |
| 37 | |
| 38 | #define STUB_INPUT_BUFFER_MILLISECONDS 20 |
| 39 | #define STUB_INPUT_DEFAULT_CHANNEL_MASK AUDIO_CHANNEL_IN_STEREO |
| 40 | |
| 41 | #define STUB_OUTPUT_BUFFER_MILLISECONDS 10 |
| 42 | #define STUB_OUTPUT_DEFAULT_CHANNEL_MASK AUDIO_CHANNEL_OUT_STEREO |
| 43 | |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 44 | struct stub_audio_device { |
| 45 | struct audio_hw_device device; |
| 46 | }; |
| 47 | |
| 48 | struct stub_stream_out { |
| 49 | struct audio_stream_out stream; |
Andy Hung | 0caeee8 | 2016-06-24 19:41:06 -0700 | [diff] [blame] | 50 | int64_t last_write_time_us; |
John Muir | 001e6f4 | 2018-01-19 16:50:25 -0800 | [diff] [blame] | 51 | uint32_t sample_rate; |
| 52 | audio_channel_mask_t channel_mask; |
| 53 | audio_format_t format; |
| 54 | size_t frame_count; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | struct stub_stream_in { |
| 58 | struct audio_stream_in stream; |
Andy Hung | 0caeee8 | 2016-06-24 19:41:06 -0700 | [diff] [blame] | 59 | int64_t last_read_time_us; |
John Muir | 001e6f4 | 2018-01-19 16:50:25 -0800 | [diff] [blame] | 60 | uint32_t sample_rate; |
| 61 | audio_channel_mask_t channel_mask; |
| 62 | audio_format_t format; |
| 63 | size_t frame_count; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | static uint32_t out_get_sample_rate(const struct audio_stream *stream) |
| 67 | { |
John Muir | 001e6f4 | 2018-01-19 16:50:25 -0800 | [diff] [blame] | 68 | const struct stub_stream_out *out = (const struct stub_stream_out *)stream; |
| 69 | |
| 70 | ALOGV("out_get_sample_rate: %u", out->sample_rate); |
| 71 | return out->sample_rate; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 75 | { |
John Muir | 001e6f4 | 2018-01-19 16:50:25 -0800 | [diff] [blame] | 76 | struct stub_stream_out *out = (struct stub_stream_out *)stream; |
| 77 | |
| 78 | ALOGV("out_set_sample_rate: %d", rate); |
| 79 | out->sample_rate = rate; |
| 80 | return 0; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static size_t out_get_buffer_size(const struct audio_stream *stream) |
| 84 | { |
John Muir | 001e6f4 | 2018-01-19 16:50:25 -0800 | [diff] [blame] | 85 | const struct stub_stream_out *out = (const struct stub_stream_out *)stream; |
| 86 | size_t buffer_size = out->frame_count * |
| 87 | audio_stream_out_frame_size(&out->stream); |
| 88 | |
| 89 | ALOGV("out_get_buffer_size: %zu", buffer_size); |
| 90 | return buffer_size; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Glenn Kasten | a635449 | 2012-06-19 12:16:04 -0700 | [diff] [blame] | 93 | static audio_channel_mask_t out_get_channels(const struct audio_stream *stream) |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 94 | { |
John Muir | 001e6f4 | 2018-01-19 16:50:25 -0800 | [diff] [blame] | 95 | const struct stub_stream_out *out = (const struct stub_stream_out *)stream; |
| 96 | |
| 97 | ALOGV("out_get_channels: %x", out->channel_mask); |
| 98 | return out->channel_mask; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Glenn Kasten | fe79eb3 | 2012-01-12 14:55:57 -0800 | [diff] [blame] | 101 | static audio_format_t out_get_format(const struct audio_stream *stream) |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 102 | { |
John Muir | 001e6f4 | 2018-01-19 16:50:25 -0800 | [diff] [blame] | 103 | const struct stub_stream_out *out = (const struct stub_stream_out *)stream; |
| 104 | |
| 105 | ALOGV("out_get_format: %d", out->format); |
| 106 | return out->format; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Glenn Kasten | fe79eb3 | 2012-01-12 14:55:57 -0800 | [diff] [blame] | 109 | static int out_set_format(struct audio_stream *stream, audio_format_t format) |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 110 | { |
John Muir | 001e6f4 | 2018-01-19 16:50:25 -0800 | [diff] [blame] | 111 | struct stub_stream_out *out = (struct stub_stream_out *)stream; |
| 112 | |
| 113 | ALOGV("out_set_format: %d", format); |
| 114 | out->format = format; |
| 115 | return 0; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static int out_standby(struct audio_stream *stream) |
| 119 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 120 | ALOGV("out_standby"); |
Andy Hung | 0caeee8 | 2016-06-24 19:41:06 -0700 | [diff] [blame] | 121 | // out->last_write_time_us = 0; unnecessary as a stale write time has same effect |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int out_dump(const struct audio_stream *stream, int fd) |
| 126 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 127 | ALOGV("out_dump"); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 132 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 133 | ALOGV("out_set_parameters"); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static char * out_get_parameters(const struct audio_stream *stream, const char *keys) |
| 138 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 139 | ALOGV("out_get_parameters"); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 140 | return strdup(""); |
| 141 | } |
| 142 | |
| 143 | static uint32_t out_get_latency(const struct audio_stream_out *stream) |
| 144 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 145 | ALOGV("out_get_latency"); |
John Muir | 001e6f4 | 2018-01-19 16:50:25 -0800 | [diff] [blame] | 146 | return STUB_OUTPUT_BUFFER_MILLISECONDS; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static int out_set_volume(struct audio_stream_out *stream, float left, |
| 150 | float right) |
| 151 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 152 | ALOGV("out_set_volume: Left:%f Right:%f", left, right); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, |
| 157 | size_t bytes) |
| 158 | { |
Greg Kaiser | 6c59aad | 2016-06-30 16:08:53 -0700 | [diff] [blame] | 159 | ALOGV("out_write: bytes: %zu", bytes); |
Andy Hung | 0caeee8 | 2016-06-24 19:41:06 -0700 | [diff] [blame] | 160 | |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 161 | /* XXX: fake timing for audio output */ |
Andy Hung | 0caeee8 | 2016-06-24 19:41:06 -0700 | [diff] [blame] | 162 | struct stub_stream_out *out = (struct stub_stream_out *)stream; |
| 163 | struct timespec t = { .tv_sec = 0, .tv_nsec = 0 }; |
| 164 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 165 | const int64_t now = (t.tv_sec * 1000000000LL + t.tv_nsec) / 1000; |
| 166 | const int64_t elapsed_time_since_last_write = now - out->last_write_time_us; |
| 167 | int64_t sleep_time = bytes * 1000000LL / audio_stream_out_frame_size(stream) / |
| 168 | out_get_sample_rate(&stream->common) - elapsed_time_since_last_write; |
| 169 | if (sleep_time > 0) { |
| 170 | usleep(sleep_time); |
| 171 | } else { |
| 172 | // we don't sleep when we exit standby (this is typical for a real alsa buffer). |
| 173 | sleep_time = 0; |
| 174 | } |
| 175 | out->last_write_time_us = now + sleep_time; |
| 176 | // last_write_time_us is an approximation of when the (simulated) alsa |
| 177 | // buffer is believed completely full. The usleep above waits for more space |
| 178 | // in the buffer, but by the end of the sleep the buffer is considered |
| 179 | // topped-off. |
| 180 | // |
| 181 | // On the subsequent out_write(), we measure the elapsed time spent in |
| 182 | // the mixer. This is subtracted from the sleep estimate based on frames, |
| 183 | // thereby accounting for drain in the alsa buffer during mixing. |
| 184 | // This is a crude approximation; we don't handle underruns precisely. |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 185 | return bytes; |
| 186 | } |
| 187 | |
| 188 | static int out_get_render_position(const struct audio_stream_out *stream, |
| 189 | uint32_t *dsp_frames) |
| 190 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 191 | *dsp_frames = 0; |
Glenn Kasten | e03bfa4 | 2015-03-23 10:52:31 -0700 | [diff] [blame] | 192 | ALOGV("out_get_render_position: dsp_frames: %p", dsp_frames); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 193 | return -EINVAL; |
| 194 | } |
| 195 | |
Eric Laurent | f3008aa | 2011-06-17 16:53:12 -0700 | [diff] [blame] | 196 | static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 197 | { |
Glenn Kasten | e03bfa4 | 2015-03-23 10:52:31 -0700 | [diff] [blame] | 198 | ALOGV("out_add_audio_effect: %p", effect); |
Eric Laurent | f3008aa | 2011-06-17 16:53:12 -0700 | [diff] [blame] | 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 203 | { |
Glenn Kasten | e03bfa4 | 2015-03-23 10:52:31 -0700 | [diff] [blame] | 204 | ALOGV("out_remove_audio_effect: %p", effect); |
Eric Laurent | f3008aa | 2011-06-17 16:53:12 -0700 | [diff] [blame] | 205 | return 0; |
| 206 | } |
| 207 | |
Mike J. Chen | 5ad38a9 | 2011-08-15 12:05:00 -0700 | [diff] [blame] | 208 | static int out_get_next_write_timestamp(const struct audio_stream_out *stream, |
| 209 | int64_t *timestamp) |
| 210 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 211 | *timestamp = 0; |
| 212 | ALOGV("out_get_next_write_timestamp: %ld", (long int)(*timestamp)); |
Mike J. Chen | 5ad38a9 | 2011-08-15 12:05:00 -0700 | [diff] [blame] | 213 | return -EINVAL; |
| 214 | } |
| 215 | |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 216 | /** audio_stream_in implementation **/ |
| 217 | static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| 218 | { |
John Muir | 001e6f4 | 2018-01-19 16:50:25 -0800 | [diff] [blame] | 219 | const struct stub_stream_in *in = (const struct stub_stream_in *)stream; |
| 220 | |
| 221 | ALOGV("in_get_sample_rate: %u", in->sample_rate); |
| 222 | return in->sample_rate; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 226 | { |
John Muir | 001e6f4 | 2018-01-19 16:50:25 -0800 | [diff] [blame] | 227 | struct stub_stream_in *in = (struct stub_stream_in *)stream; |
| 228 | |
| 229 | ALOGV("in_set_sample_rate: %u", rate); |
| 230 | in->sample_rate = rate; |
| 231 | return 0; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static size_t in_get_buffer_size(const struct audio_stream *stream) |
| 235 | { |
John Muir | 001e6f4 | 2018-01-19 16:50:25 -0800 | [diff] [blame] | 236 | const struct stub_stream_in *in = (const struct stub_stream_in *)stream; |
| 237 | size_t buffer_size = in->frame_count * |
| 238 | audio_stream_in_frame_size(&in->stream); |
| 239 | |
| 240 | ALOGV("in_get_buffer_size: %zu", buffer_size); |
| 241 | return buffer_size; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Glenn Kasten | a635449 | 2012-06-19 12:16:04 -0700 | [diff] [blame] | 244 | static audio_channel_mask_t in_get_channels(const struct audio_stream *stream) |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 245 | { |
John Muir | 001e6f4 | 2018-01-19 16:50:25 -0800 | [diff] [blame] | 246 | const struct stub_stream_in *in = (const struct stub_stream_in *)stream; |
| 247 | |
| 248 | ALOGV("in_get_channels: %x", in->channel_mask); |
| 249 | return in->channel_mask; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Glenn Kasten | fe79eb3 | 2012-01-12 14:55:57 -0800 | [diff] [blame] | 252 | static audio_format_t in_get_format(const struct audio_stream *stream) |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 253 | { |
John Muir | 001e6f4 | 2018-01-19 16:50:25 -0800 | [diff] [blame] | 254 | const struct stub_stream_in *in = (const struct stub_stream_in *)stream; |
| 255 | |
| 256 | ALOGV("in_get_format: %d", in->format); |
| 257 | return in->format; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Glenn Kasten | fe79eb3 | 2012-01-12 14:55:57 -0800 | [diff] [blame] | 260 | static int in_set_format(struct audio_stream *stream, audio_format_t format) |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 261 | { |
John Muir | 001e6f4 | 2018-01-19 16:50:25 -0800 | [diff] [blame] | 262 | struct stub_stream_in *in = (struct stub_stream_in *)stream; |
| 263 | |
| 264 | ALOGV("in_set_format: %d", format); |
| 265 | in->format = format; |
| 266 | return 0; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static int in_standby(struct audio_stream *stream) |
| 270 | { |
Andy Hung | 0caeee8 | 2016-06-24 19:41:06 -0700 | [diff] [blame] | 271 | struct stub_stream_in *in = (struct stub_stream_in *)stream; |
| 272 | in->last_read_time_us = 0; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | static int in_dump(const struct audio_stream *stream, int fd) |
| 277 | { |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 282 | { |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static char * in_get_parameters(const struct audio_stream *stream, |
| 287 | const char *keys) |
| 288 | { |
| 289 | return strdup(""); |
| 290 | } |
| 291 | |
| 292 | static int in_set_gain(struct audio_stream_in *stream, float gain) |
| 293 | { |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static ssize_t in_read(struct audio_stream_in *stream, void* buffer, |
| 298 | size_t bytes) |
| 299 | { |
Greg Kaiser | 6c59aad | 2016-06-30 16:08:53 -0700 | [diff] [blame] | 300 | ALOGV("in_read: bytes %zu", bytes); |
Andy Hung | 0caeee8 | 2016-06-24 19:41:06 -0700 | [diff] [blame] | 301 | |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 302 | /* XXX: fake timing for audio input */ |
Andy Hung | 0caeee8 | 2016-06-24 19:41:06 -0700 | [diff] [blame] | 303 | struct stub_stream_in *in = (struct stub_stream_in *)stream; |
| 304 | struct timespec t = { .tv_sec = 0, .tv_nsec = 0 }; |
| 305 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 306 | const int64_t now = (t.tv_sec * 1000000000LL + t.tv_nsec) / 1000; |
| 307 | |
| 308 | // we do a full sleep when exiting standby. |
| 309 | const bool standby = in->last_read_time_us == 0; |
| 310 | const int64_t elapsed_time_since_last_read = standby ? |
| 311 | 0 : now - in->last_read_time_us; |
| 312 | int64_t sleep_time = bytes * 1000000LL / audio_stream_in_frame_size(stream) / |
| 313 | in_get_sample_rate(&stream->common) - elapsed_time_since_last_read; |
| 314 | if (sleep_time > 0) { |
| 315 | usleep(sleep_time); |
| 316 | } else { |
| 317 | sleep_time = 0; |
| 318 | } |
| 319 | in->last_read_time_us = now + sleep_time; |
| 320 | // last_read_time_us is an approximation of when the (simulated) alsa |
| 321 | // buffer is drained by the read, and is empty. |
| 322 | // |
| 323 | // On the subsequent in_read(), we measure the elapsed time spent in |
| 324 | // the recording thread. This is subtracted from the sleep estimate based on frames, |
| 325 | // thereby accounting for fill in the alsa buffer during the interim. |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 326 | memset(buffer, 0, bytes); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 327 | return bytes; |
| 328 | } |
| 329 | |
| 330 | static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) |
| 331 | { |
| 332 | return 0; |
| 333 | } |
| 334 | |
Eric Laurent | f3008aa | 2011-06-17 16:53:12 -0700 | [diff] [blame] | 335 | static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 336 | { |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 341 | { |
| 342 | return 0; |
| 343 | } |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 344 | |
John Muir | 001e6f4 | 2018-01-19 16:50:25 -0800 | [diff] [blame] | 345 | static size_t samples_per_milliseconds(size_t milliseconds, |
| 346 | uint32_t sample_rate, |
| 347 | size_t channel_count) |
| 348 | { |
| 349 | return milliseconds * sample_rate * channel_count / 1000; |
| 350 | } |
| 351 | |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 352 | static int adev_open_output_stream(struct audio_hw_device *dev, |
Eric Laurent | 55786bc | 2012-04-10 16:56:32 -0700 | [diff] [blame] | 353 | audio_io_handle_t handle, |
| 354 | audio_devices_t devices, |
| 355 | audio_output_flags_t flags, |
| 356 | struct audio_config *config, |
Eric Laurent | f5e2469 | 2014-07-27 16:14:57 -0700 | [diff] [blame] | 357 | struct audio_stream_out **stream_out, |
| 358 | const char *address __unused) |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 359 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 360 | ALOGV("adev_open_output_stream..."); |
| 361 | |
Glenn Kasten | 2494cae | 2016-09-19 18:16:16 -0700 | [diff] [blame] | 362 | *stream_out = NULL; |
| 363 | struct stub_stream_out *out = |
| 364 | (struct stub_stream_out *)calloc(1, sizeof(struct stub_stream_out)); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 365 | if (!out) |
| 366 | return -ENOMEM; |
| 367 | |
| 368 | out->stream.common.get_sample_rate = out_get_sample_rate; |
| 369 | out->stream.common.set_sample_rate = out_set_sample_rate; |
| 370 | out->stream.common.get_buffer_size = out_get_buffer_size; |
| 371 | out->stream.common.get_channels = out_get_channels; |
| 372 | out->stream.common.get_format = out_get_format; |
| 373 | out->stream.common.set_format = out_set_format; |
| 374 | out->stream.common.standby = out_standby; |
| 375 | out->stream.common.dump = out_dump; |
| 376 | out->stream.common.set_parameters = out_set_parameters; |
| 377 | out->stream.common.get_parameters = out_get_parameters; |
Eric Laurent | f3008aa | 2011-06-17 16:53:12 -0700 | [diff] [blame] | 378 | out->stream.common.add_audio_effect = out_add_audio_effect; |
| 379 | out->stream.common.remove_audio_effect = out_remove_audio_effect; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 380 | out->stream.get_latency = out_get_latency; |
| 381 | out->stream.set_volume = out_set_volume; |
| 382 | out->stream.write = out_write; |
| 383 | out->stream.get_render_position = out_get_render_position; |
Mike J. Chen | 5ad38a9 | 2011-08-15 12:05:00 -0700 | [diff] [blame] | 384 | out->stream.get_next_write_timestamp = out_get_next_write_timestamp; |
John Muir | 001e6f4 | 2018-01-19 16:50:25 -0800 | [diff] [blame] | 385 | out->sample_rate = config->sample_rate; |
| 386 | if (out->sample_rate == 0) |
| 387 | out->sample_rate = STUB_DEFAULT_SAMPLE_RATE; |
| 388 | out->channel_mask = config->channel_mask; |
| 389 | if (out->channel_mask == AUDIO_CHANNEL_NONE) |
| 390 | out->channel_mask = STUB_OUTPUT_DEFAULT_CHANNEL_MASK; |
| 391 | out->format = config->format; |
| 392 | if (out->format == AUDIO_FORMAT_DEFAULT) |
| 393 | out->format = STUB_DEFAULT_AUDIO_FORMAT; |
| 394 | out->frame_count = samples_per_milliseconds( |
| 395 | STUB_OUTPUT_BUFFER_MILLISECONDS, |
| 396 | out->sample_rate, 1); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 397 | |
John Muir | 001e6f4 | 2018-01-19 16:50:25 -0800 | [diff] [blame] | 398 | ALOGV("adev_open_output_stream: sample_rate: %u, channels: %x, format: %d," |
| 399 | " frames: %zu", out->sample_rate, out->channel_mask, out->format, |
| 400 | out->frame_count); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 401 | *stream_out = &out->stream; |
| 402 | return 0; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | static void adev_close_output_stream(struct audio_hw_device *dev, |
| 406 | struct audio_stream_out *stream) |
| 407 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 408 | ALOGV("adev_close_output_stream..."); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 409 | free(stream); |
| 410 | } |
| 411 | |
| 412 | static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) |
| 413 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 414 | ALOGV("adev_set_parameters"); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 415 | return -ENOSYS; |
| 416 | } |
| 417 | |
| 418 | static char * adev_get_parameters(const struct audio_hw_device *dev, |
| 419 | const char *keys) |
| 420 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 421 | ALOGV("adev_get_parameters"); |
| 422 | return strdup(""); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | static int adev_init_check(const struct audio_hw_device *dev) |
| 426 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 427 | ALOGV("adev_init_check"); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) |
| 432 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 433 | ALOGV("adev_set_voice_volume: %f", volume); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 434 | return -ENOSYS; |
| 435 | } |
| 436 | |
| 437 | static int adev_set_master_volume(struct audio_hw_device *dev, float volume) |
| 438 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 439 | ALOGV("adev_set_master_volume: %f", volume); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 440 | return -ENOSYS; |
| 441 | } |
| 442 | |
John Grossman | 47bf3d7 | 2012-07-17 11:54:04 -0700 | [diff] [blame] | 443 | static int adev_get_master_volume(struct audio_hw_device *dev, float *volume) |
| 444 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 445 | ALOGV("adev_get_master_volume: %f", *volume); |
John Grossman | 47bf3d7 | 2012-07-17 11:54:04 -0700 | [diff] [blame] | 446 | return -ENOSYS; |
| 447 | } |
| 448 | |
| 449 | static int adev_set_master_mute(struct audio_hw_device *dev, bool muted) |
| 450 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 451 | ALOGV("adev_set_master_mute: %d", muted); |
John Grossman | 47bf3d7 | 2012-07-17 11:54:04 -0700 | [diff] [blame] | 452 | return -ENOSYS; |
| 453 | } |
| 454 | |
| 455 | static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted) |
Mike J. Chen | 5ad38a9 | 2011-08-15 12:05:00 -0700 | [diff] [blame] | 456 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 457 | ALOGV("adev_get_master_mute: %d", *muted); |
Mike J. Chen | 5ad38a9 | 2011-08-15 12:05:00 -0700 | [diff] [blame] | 458 | return -ENOSYS; |
| 459 | } |
| 460 | |
Glenn Kasten | 6df641e | 2012-01-09 10:41:30 -0800 | [diff] [blame] | 461 | static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 462 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 463 | ALOGV("adev_set_mode: %d", mode); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) |
| 468 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 469 | ALOGV("adev_set_mic_mute: %d",state); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 470 | return -ENOSYS; |
| 471 | } |
| 472 | |
| 473 | static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) |
| 474 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 475 | ALOGV("adev_get_mic_mute"); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 476 | return -ENOSYS; |
| 477 | } |
| 478 | |
| 479 | static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev, |
Eric Laurent | 55786bc | 2012-04-10 16:56:32 -0700 | [diff] [blame] | 480 | const struct audio_config *config) |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 481 | { |
John Muir | 001e6f4 | 2018-01-19 16:50:25 -0800 | [diff] [blame] | 482 | size_t buffer_size = samples_per_milliseconds( |
| 483 | STUB_INPUT_BUFFER_MILLISECONDS, |
| 484 | config->sample_rate, |
| 485 | audio_channel_count_from_in_mask( |
| 486 | config->channel_mask)); |
| 487 | |
| 488 | if (!audio_has_proportional_frames(config->format)) { |
| 489 | // Since the audio data is not proportional choose an arbitrary size for |
| 490 | // the buffer. |
| 491 | buffer_size *= 4; |
| 492 | } else { |
| 493 | buffer_size *= audio_bytes_per_sample(config->format); |
| 494 | } |
| 495 | ALOGV("adev_get_input_buffer_size: %zu", buffer_size); |
| 496 | return buffer_size; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Eric Laurent | 55786bc | 2012-04-10 16:56:32 -0700 | [diff] [blame] | 499 | static int adev_open_input_stream(struct audio_hw_device *dev, |
| 500 | audio_io_handle_t handle, |
| 501 | audio_devices_t devices, |
| 502 | struct audio_config *config, |
Glenn Kasten | 7d973ad | 2014-07-15 11:10:38 -0700 | [diff] [blame] | 503 | struct audio_stream_in **stream_in, |
Eric Laurent | f5e2469 | 2014-07-27 16:14:57 -0700 | [diff] [blame] | 504 | audio_input_flags_t flags __unused, |
| 505 | const char *address __unused, |
| 506 | audio_source_t source __unused) |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 507 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 508 | ALOGV("adev_open_input_stream..."); |
| 509 | |
Glenn Kasten | 2494cae | 2016-09-19 18:16:16 -0700 | [diff] [blame] | 510 | *stream_in = NULL; |
| 511 | struct stub_stream_in *in = (struct stub_stream_in *)calloc(1, sizeof(struct stub_stream_in)); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 512 | if (!in) |
| 513 | return -ENOMEM; |
| 514 | |
| 515 | in->stream.common.get_sample_rate = in_get_sample_rate; |
| 516 | in->stream.common.set_sample_rate = in_set_sample_rate; |
| 517 | in->stream.common.get_buffer_size = in_get_buffer_size; |
| 518 | in->stream.common.get_channels = in_get_channels; |
| 519 | in->stream.common.get_format = in_get_format; |
| 520 | in->stream.common.set_format = in_set_format; |
| 521 | in->stream.common.standby = in_standby; |
| 522 | in->stream.common.dump = in_dump; |
| 523 | in->stream.common.set_parameters = in_set_parameters; |
| 524 | in->stream.common.get_parameters = in_get_parameters; |
Eric Laurent | f3008aa | 2011-06-17 16:53:12 -0700 | [diff] [blame] | 525 | in->stream.common.add_audio_effect = in_add_audio_effect; |
| 526 | in->stream.common.remove_audio_effect = in_remove_audio_effect; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 527 | in->stream.set_gain = in_set_gain; |
| 528 | in->stream.read = in_read; |
| 529 | in->stream.get_input_frames_lost = in_get_input_frames_lost; |
John Muir | 001e6f4 | 2018-01-19 16:50:25 -0800 | [diff] [blame] | 530 | in->sample_rate = config->sample_rate; |
| 531 | if (in->sample_rate == 0) |
| 532 | in->sample_rate = STUB_DEFAULT_SAMPLE_RATE; |
| 533 | in->channel_mask = config->channel_mask; |
| 534 | if (in->channel_mask == AUDIO_CHANNEL_NONE) |
| 535 | in->channel_mask = STUB_INPUT_DEFAULT_CHANNEL_MASK; |
| 536 | in->format = config->format; |
| 537 | if (in->format == AUDIO_FORMAT_DEFAULT) |
| 538 | in->format = STUB_DEFAULT_AUDIO_FORMAT; |
| 539 | in->frame_count = samples_per_milliseconds( |
| 540 | STUB_INPUT_BUFFER_MILLISECONDS, in->sample_rate, 1); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 541 | |
John Muir | 001e6f4 | 2018-01-19 16:50:25 -0800 | [diff] [blame] | 542 | ALOGV("adev_open_input_stream: sample_rate: %u, channels: %x, format: %d," |
| 543 | "frames: %zu", in->sample_rate, in->channel_mask, in->format, |
| 544 | in->frame_count); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 545 | *stream_in = &in->stream; |
| 546 | return 0; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | static void adev_close_input_stream(struct audio_hw_device *dev, |
| 550 | struct audio_stream_in *in) |
| 551 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 552 | ALOGV("adev_close_input_stream..."); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 553 | return; |
| 554 | } |
| 555 | |
| 556 | static int adev_dump(const audio_hw_device_t *device, int fd) |
| 557 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 558 | ALOGV("adev_dump"); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | static int adev_close(hw_device_t *device) |
| 563 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 564 | ALOGV("adev_close"); |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 565 | free(device); |
| 566 | return 0; |
| 567 | } |
| 568 | |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 569 | static int adev_open(const hw_module_t* module, const char* name, |
| 570 | hw_device_t** device) |
| 571 | { |
Ricardo Garcia | 37cd772 | 2015-02-23 15:42:26 -0800 | [diff] [blame] | 572 | ALOGV("adev_open: %s", name); |
| 573 | |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 574 | struct stub_audio_device *adev; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 575 | |
| 576 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) |
| 577 | return -EINVAL; |
| 578 | |
| 579 | adev = calloc(1, sizeof(struct stub_audio_device)); |
| 580 | if (!adev) |
| 581 | return -ENOMEM; |
| 582 | |
| 583 | adev->device.common.tag = HARDWARE_DEVICE_TAG; |
Eric Laurent | 85e08e2 | 2012-08-28 14:30:35 -0700 | [diff] [blame] | 584 | adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 585 | adev->device.common.module = (struct hw_module_t *) module; |
| 586 | adev->device.common.close = adev_close; |
| 587 | |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 588 | adev->device.init_check = adev_init_check; |
| 589 | adev->device.set_voice_volume = adev_set_voice_volume; |
| 590 | adev->device.set_master_volume = adev_set_master_volume; |
Mike J. Chen | 5ad38a9 | 2011-08-15 12:05:00 -0700 | [diff] [blame] | 591 | adev->device.get_master_volume = adev_get_master_volume; |
John Grossman | 47bf3d7 | 2012-07-17 11:54:04 -0700 | [diff] [blame] | 592 | adev->device.set_master_mute = adev_set_master_mute; |
| 593 | adev->device.get_master_mute = adev_get_master_mute; |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 594 | adev->device.set_mode = adev_set_mode; |
| 595 | adev->device.set_mic_mute = adev_set_mic_mute; |
| 596 | adev->device.get_mic_mute = adev_get_mic_mute; |
| 597 | adev->device.set_parameters = adev_set_parameters; |
| 598 | adev->device.get_parameters = adev_get_parameters; |
| 599 | adev->device.get_input_buffer_size = adev_get_input_buffer_size; |
| 600 | adev->device.open_output_stream = adev_open_output_stream; |
| 601 | adev->device.close_output_stream = adev_close_output_stream; |
| 602 | adev->device.open_input_stream = adev_open_input_stream; |
| 603 | adev->device.close_input_stream = adev_close_input_stream; |
| 604 | adev->device.dump = adev_dump; |
| 605 | |
| 606 | *device = &adev->device.common; |
| 607 | |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | static struct hw_module_methods_t hal_module_methods = { |
| 612 | .open = adev_open, |
| 613 | }; |
| 614 | |
| 615 | struct audio_module HAL_MODULE_INFO_SYM = { |
| 616 | .common = { |
| 617 | .tag = HARDWARE_MODULE_TAG, |
Eric Laurent | 55786bc | 2012-04-10 16:56:32 -0700 | [diff] [blame] | 618 | .module_api_version = AUDIO_MODULE_API_VERSION_0_1, |
| 619 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 620 | .id = AUDIO_HARDWARE_MODULE_ID, |
| 621 | .name = "Default audio HW HAL", |
| 622 | .author = "The Android Open Source Project", |
| 623 | .methods = &hal_module_methods, |
| 624 | }, |
| 625 | }; |