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> |
Stewart Miles | e54c12c | 2014-05-01 09:03:27 -0700 | [diff] [blame^] | 26 | #include <sys/limits.h> |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 27 | |
| 28 | #include <cutils/log.h> |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 29 | #include <cutils/properties.h> |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 30 | #include <cutils/str_parms.h> |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 31 | |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 32 | #include <hardware/audio.h> |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 33 | #include <hardware/hardware.h> |
| 34 | #include <system/audio.h> |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 35 | |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 36 | #include <media/AudioParameter.h> |
| 37 | #include <media/AudioBufferProvider.h> |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 38 | #include <media/nbaio/MonoPipe.h> |
| 39 | #include <media/nbaio/MonoPipeReader.h> |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 40 | |
Jean-Michel Trivi | d441303 | 2012-09-30 11:08:06 -0700 | [diff] [blame] | 41 | #include <utils/String8.h> |
Jean-Michel Trivi | d441303 | 2012-09-30 11:08:06 -0700 | [diff] [blame] | 42 | |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 43 | extern "C" { |
| 44 | |
| 45 | namespace android { |
| 46 | |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 47 | // Set to 1 to enable extremely verbose logging in this module. |
| 48 | #define SUBMIX_VERBOSE_LOGGING 0 |
| 49 | #if SUBMIX_VERBOSE_LOGGING |
| 50 | #define SUBMIX_ALOGV(...) ALOGV(__VA_ARGS__) |
| 51 | #define SUBMIX_ALOGE(...) ALOGE(__VA_ARGS__) |
| 52 | #else |
| 53 | #define SUBMIX_ALOGV(...) |
| 54 | #define SUBMIX_ALOGE(...) |
| 55 | #endif // SUBMIX_VERBOSE_LOGGING |
| 56 | |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 57 | // NOTE: This value will be rounded up to the nearest power of 2 by MonoPipe(). |
| 58 | #define DEFAULT_PIPE_SIZE_IN_FRAMES (1024*8) |
| 59 | // Value used to divide the MonoPipe() buffer into segments that are written to the source and |
| 60 | // read from the sink. The maximum latency of the device is the size of the MonoPipe's buffer |
| 61 | // the minimum latency is the MonoPipe buffer size divided by this value. |
| 62 | #define DEFAULT_PIPE_PERIOD_COUNT 4 |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 63 | // The duration of MAX_READ_ATTEMPTS * READ_ATTEMPT_SLEEP_MS must be stricly inferior to |
| 64 | // the duration of a record buffer at the current record sample rate (of the device, not of |
| 65 | // the recording itself). Here we have: |
| 66 | // 3 * 5ms = 15ms < 1024 frames * 1000 / 48000 = 21.333ms |
Jean-Michel Trivi | 6acd966 | 2012-09-11 19:19:08 -0700 | [diff] [blame] | 67 | #define MAX_READ_ATTEMPTS 3 |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 68 | #define READ_ATTEMPT_SLEEP_MS 5 // 5ms between two read attempts when pipe is empty |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 69 | #define DEFAULT_SAMPLE_RATE_HZ 48000 // default sample rate |
| 70 | // See NBAIO_Format frameworks/av/include/media/nbaio/NBAIO.h. |
| 71 | #define DEFAULT_FORMAT AUDIO_FORMAT_PCM_16_BIT |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 72 | // A legacy user of this device does not close the input stream when it shuts down, which |
| 73 | // results in the application opening a new input stream before closing the old input stream |
| 74 | // handle it was previously using. Setting this value to 1 allows multiple clients to open |
| 75 | // multiple input streams from this device. If this option is enabled, each input stream returned |
| 76 | // is *the same stream* which means that readers will race to read data from these streams. |
| 77 | #define ENABLE_LEGACY_INPUT_OPEN 1 |
Stewart Miles | e54c12c | 2014-05-01 09:03:27 -0700 | [diff] [blame^] | 78 | // Whether channel conversion (16-bit signed PCM mono->stereo, stereo->mono) is enabled. |
| 79 | #define ENABLE_CHANNEL_CONVERSION 1 |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 80 | |
| 81 | // Common limits macros. |
| 82 | #ifndef min |
| 83 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
| 84 | #endif // min |
Stewart Miles | e54c12c | 2014-05-01 09:03:27 -0700 | [diff] [blame^] | 85 | #ifndef max |
| 86 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
| 87 | #endif // max |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 88 | |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 89 | // Set *result_variable_ptr to true if value_to_find is present in the array array_to_search, |
| 90 | // otherwise set *result_variable_ptr to false. |
| 91 | #define SUBMIX_VALUE_IN_SET(value_to_find, array_to_search, result_variable_ptr) \ |
| 92 | { \ |
| 93 | size_t i; \ |
| 94 | *(result_variable_ptr) = false; \ |
| 95 | for (i = 0; i < sizeof(array_to_search) / sizeof((array_to_search)[0]); i++) { \ |
| 96 | if ((value_to_find) == (array_to_search)[i]) { \ |
| 97 | *(result_variable_ptr) = true; \ |
| 98 | break; \ |
| 99 | } \ |
| 100 | } \ |
| 101 | } |
| 102 | |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 103 | // Configuration of the submix pipe. |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 104 | struct submix_config { |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 105 | // Channel mask field in this data structure is set to either input_channel_mask or |
| 106 | // output_channel_mask depending upon the last stream to be opened on this device. |
| 107 | struct audio_config common; |
| 108 | // Input stream and output stream channel masks. This is required since input and output |
| 109 | // channel bitfields are not equivalent. |
| 110 | audio_channel_mask_t input_channel_mask; |
| 111 | audio_channel_mask_t output_channel_mask; |
Stewart Miles | e54c12c | 2014-05-01 09:03:27 -0700 | [diff] [blame^] | 112 | size_t pipe_frame_size; // Number of bytes in each audio frame in the pipe. |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 113 | size_t buffer_size_frames; // Size of the audio pipe in frames. |
| 114 | // Maximum number of frames buffered by the input and output streams. |
| 115 | size_t buffer_period_size_frames; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | struct submix_audio_device { |
| 119 | struct audio_hw_device device; |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 120 | bool input_standby; |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 121 | bool output_standby; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 122 | submix_config config; |
| 123 | // Pipe variables: they handle the ring buffer that "pipes" audio: |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 124 | // - from the submix virtual audio output == what needs to be played |
| 125 | // remotely, seen as an output for AudioFlinger |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 126 | // - to the virtual audio source == what is captured by the component |
| 127 | // 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] | 128 | // 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] | 129 | // Wifi for presentation on a remote Wifi Display device (e.g. a dongle attached to a TV, or a |
| 130 | // TV with Wifi Display capabilities), or to a wireless audio player. |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 131 | sp<MonoPipe> rsxSink; |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 132 | sp<MonoPipeReader> rsxSource; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 133 | |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 134 | // Pointers to the current input and output stream instances. rsxSink and rsxSource are |
| 135 | // destroyed if both and input and output streams are destroyed. |
| 136 | struct submix_stream_out *output; |
| 137 | struct submix_stream_in *input; |
| 138 | |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 139 | // Device lock, also used to protect access to submix_audio_device from the input and output |
| 140 | // streams. |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 141 | pthread_mutex_t lock; |
| 142 | }; |
| 143 | |
| 144 | struct submix_stream_out { |
| 145 | struct audio_stream_out stream; |
| 146 | struct submix_audio_device *dev; |
| 147 | }; |
| 148 | |
| 149 | struct submix_stream_in { |
| 150 | struct audio_stream_in stream; |
| 151 | struct submix_audio_device *dev; |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 152 | bool output_standby; // output standby state as seen from record thread |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 153 | |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 154 | // wall clock when recording starts |
| 155 | struct timespec record_start_time; |
| 156 | // how many frames have been requested to be read |
| 157 | int64_t read_counter_frames; |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 158 | |
| 159 | #if ENABLE_LEGACY_INPUT_OPEN |
| 160 | // Number of references to this input stream. |
| 161 | volatile int32_t ref_count; |
| 162 | #endif // ENABLE_LEGACY_INPUT_OPEN |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 163 | }; |
Jean-Michel Trivi | 6acd966 | 2012-09-11 19:19:08 -0700 | [diff] [blame] | 164 | |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 165 | // Determine whether the specified sample rate is supported by the submix module. |
| 166 | static bool sample_rate_supported(const uint32_t sample_rate) |
| 167 | { |
| 168 | // Set of sample rates supported by Format_from_SR_C() frameworks/av/media/libnbaio/NAIO.cpp. |
| 169 | static const unsigned int supported_sample_rates[] = { |
| 170 | 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, |
| 171 | }; |
| 172 | bool return_value; |
| 173 | SUBMIX_VALUE_IN_SET(sample_rate, supported_sample_rates, &return_value); |
| 174 | return return_value; |
| 175 | } |
| 176 | |
| 177 | // Determine whether the specified sample rate is supported, if it is return the specified sample |
| 178 | // rate, otherwise return the default sample rate for the submix module. |
| 179 | static uint32_t get_supported_sample_rate(uint32_t sample_rate) |
| 180 | { |
| 181 | return sample_rate_supported(sample_rate) ? sample_rate : DEFAULT_SAMPLE_RATE_HZ; |
| 182 | } |
| 183 | |
| 184 | // Determine whether the specified channel in mask is supported by the submix module. |
| 185 | static bool channel_in_mask_supported(const audio_channel_mask_t channel_in_mask) |
| 186 | { |
| 187 | // Set of channel in masks supported by Format_from_SR_C() |
| 188 | // frameworks/av/media/libnbaio/NAIO.cpp. |
| 189 | static const audio_channel_mask_t supported_channel_in_masks[] = { |
| 190 | AUDIO_CHANNEL_IN_MONO, AUDIO_CHANNEL_IN_STEREO, |
| 191 | }; |
| 192 | bool return_value; |
| 193 | SUBMIX_VALUE_IN_SET(channel_in_mask, supported_channel_in_masks, &return_value); |
| 194 | return return_value; |
| 195 | } |
| 196 | |
| 197 | // Determine whether the specified channel in mask is supported, if it is return the specified |
| 198 | // channel in mask, otherwise return the default channel in mask for the submix module. |
| 199 | static audio_channel_mask_t get_supported_channel_in_mask( |
| 200 | const audio_channel_mask_t channel_in_mask) |
| 201 | { |
| 202 | return channel_in_mask_supported(channel_in_mask) ? channel_in_mask : |
| 203 | static_cast<audio_channel_mask_t>(AUDIO_CHANNEL_IN_STEREO); |
| 204 | } |
| 205 | |
| 206 | // Determine whether the specified channel out mask is supported by the submix module. |
| 207 | static bool channel_out_mask_supported(const audio_channel_mask_t channel_out_mask) |
| 208 | { |
| 209 | // Set of channel out masks supported by Format_from_SR_C() |
| 210 | // frameworks/av/media/libnbaio/NAIO.cpp. |
| 211 | static const audio_channel_mask_t supported_channel_out_masks[] = { |
| 212 | AUDIO_CHANNEL_OUT_MONO, AUDIO_CHANNEL_OUT_STEREO, |
| 213 | }; |
| 214 | bool return_value; |
| 215 | SUBMIX_VALUE_IN_SET(channel_out_mask, supported_channel_out_masks, &return_value); |
| 216 | return return_value; |
| 217 | } |
| 218 | |
| 219 | // Determine whether the specified channel out mask is supported, if it is return the specified |
| 220 | // channel out mask, otherwise return the default channel out mask for the submix module. |
| 221 | static audio_channel_mask_t get_supported_channel_out_mask( |
| 222 | const audio_channel_mask_t channel_out_mask) |
| 223 | { |
| 224 | return channel_out_mask_supported(channel_out_mask) ? channel_out_mask : |
| 225 | static_cast<audio_channel_mask_t>(AUDIO_CHANNEL_OUT_STEREO); |
| 226 | } |
| 227 | |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 228 | // Get a pointer to submix_stream_out given an audio_stream_out that is embedded within the |
| 229 | // structure. |
| 230 | static struct submix_stream_out * audio_stream_out_get_submix_stream_out( |
| 231 | struct audio_stream_out * const stream) |
| 232 | { |
| 233 | ALOG_ASSERT(stream); |
| 234 | return reinterpret_cast<struct submix_stream_out *>(reinterpret_cast<uint8_t *>(stream) - |
| 235 | offsetof(struct submix_stream_out, stream)); |
| 236 | } |
| 237 | |
| 238 | // Get a pointer to submix_stream_out given an audio_stream that is embedded within the structure. |
| 239 | static struct submix_stream_out * audio_stream_get_submix_stream_out( |
| 240 | struct audio_stream * const stream) |
| 241 | { |
| 242 | ALOG_ASSERT(stream); |
| 243 | return audio_stream_out_get_submix_stream_out( |
| 244 | reinterpret_cast<struct audio_stream_out *>(stream)); |
| 245 | } |
| 246 | |
| 247 | // Get a pointer to submix_stream_in given an audio_stream_in that is embedded within the |
| 248 | // structure. |
| 249 | static struct submix_stream_in * audio_stream_in_get_submix_stream_in( |
| 250 | struct audio_stream_in * const stream) |
| 251 | { |
| 252 | ALOG_ASSERT(stream); |
| 253 | return reinterpret_cast<struct submix_stream_in *>(reinterpret_cast<uint8_t *>(stream) - |
| 254 | offsetof(struct submix_stream_in, stream)); |
| 255 | } |
| 256 | |
| 257 | // Get a pointer to submix_stream_in given an audio_stream that is embedded within the structure. |
| 258 | static struct submix_stream_in * audio_stream_get_submix_stream_in( |
| 259 | struct audio_stream * const stream) |
| 260 | { |
| 261 | ALOG_ASSERT(stream); |
| 262 | return audio_stream_in_get_submix_stream_in( |
| 263 | reinterpret_cast<struct audio_stream_in *>(stream)); |
| 264 | } |
| 265 | |
| 266 | // Get a pointer to submix_audio_device given a pointer to an audio_device that is embedded within |
| 267 | // the structure. |
| 268 | static struct submix_audio_device * audio_hw_device_get_submix_audio_device( |
| 269 | struct audio_hw_device *device) |
| 270 | { |
| 271 | ALOG_ASSERT(device); |
| 272 | return reinterpret_cast<struct submix_audio_device *>(reinterpret_cast<uint8_t *>(device) - |
| 273 | offsetof(struct submix_audio_device, device)); |
| 274 | } |
| 275 | |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 276 | // Get the number of channels referenced by the specified channel_mask. The channel_mask can |
| 277 | // reference either input or output channels. |
| 278 | uint32_t get_channel_count_from_mask(const audio_channel_mask_t channel_mask) { |
| 279 | if (audio_is_input_channel(channel_mask)) { |
| 280 | return popcount(channel_mask & AUDIO_CHANNEL_IN_ALL); |
| 281 | } else if (audio_is_output_channel(channel_mask)) { |
| 282 | return popcount(channel_mask & AUDIO_CHANNEL_OUT_ALL); |
| 283 | } |
| 284 | ALOGE("get_channel_count(): No channels specified in channel mask %x", channel_mask); |
| 285 | return 0; |
| 286 | } |
| 287 | |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 288 | // Compare an audio_config with input channel mask and an audio_config with output channel mask |
| 289 | // returning false if they do *not* match, true otherwise. |
| 290 | static bool audio_config_compare(const audio_config * const input_config, |
| 291 | const audio_config * const output_config) |
| 292 | { |
Stewart Miles | e54c12c | 2014-05-01 09:03:27 -0700 | [diff] [blame^] | 293 | #if !ENABLE_CHANNEL_CONVERSION |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 294 | const uint32_t input_channels = get_channel_count_from_mask(input_config->channel_mask); |
| 295 | const uint32_t output_channels = get_channel_count_from_mask(output_config->channel_mask); |
| 296 | if (input_channels != output_channels) { |
| 297 | ALOGE("audio_config_compare() channel count mismatch input=%d vs. output=%d", |
| 298 | input_channels, output_channels); |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 299 | return false; |
| 300 | } |
Stewart Miles | e54c12c | 2014-05-01 09:03:27 -0700 | [diff] [blame^] | 301 | #endif // !ENABLE_CHANNEL_CONVERSION |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 302 | if (input_config->sample_rate != output_config->sample_rate) { |
| 303 | ALOGE("audio_config_compare() sample rate mismatch %ul vs. %ul", |
| 304 | input_config->sample_rate, output_config->sample_rate); |
| 305 | return false; |
| 306 | } |
| 307 | if (input_config->format != output_config->format) { |
| 308 | ALOGE("audio_config_compare() format mismatch %x vs. %x", |
| 309 | input_config->format, output_config->format); |
| 310 | return false; |
| 311 | } |
| 312 | // This purposely ignores offload_info as it's not required for the submix device. |
| 313 | return true; |
| 314 | } |
| 315 | |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 316 | // If one doesn't exist, create a pipe for the submix audio device rsxadev of size |
| 317 | // buffer_size_frames and optionally associate "in" or "out" with the submix audio device. |
| 318 | static void submix_audio_device_create_pipe(struct submix_audio_device * const rsxadev, |
| 319 | const struct audio_config * const config, |
| 320 | const size_t buffer_size_frames, |
| 321 | const uint32_t buffer_period_count, |
| 322 | struct submix_stream_in * const in, |
| 323 | struct submix_stream_out * const out) |
| 324 | { |
| 325 | ALOG_ASSERT(in || out); |
| 326 | ALOGV("submix_audio_device_create_pipe()"); |
| 327 | pthread_mutex_lock(&rsxadev->lock); |
| 328 | // Save a reference to the specified input or output stream and the associated channel |
| 329 | // mask. |
| 330 | if (in) { |
| 331 | rsxadev->input = in; |
| 332 | rsxadev->config.input_channel_mask = config->channel_mask; |
| 333 | } |
| 334 | if (out) { |
| 335 | rsxadev->output = out; |
| 336 | rsxadev->config.output_channel_mask = config->channel_mask; |
| 337 | } |
| 338 | // If a pipe isn't associated with the device, create one. |
| 339 | if (rsxadev->rsxSink == NULL || rsxadev->rsxSource == NULL) { |
| 340 | struct submix_config * const device_config = &rsxadev->config; |
| 341 | const NBAIO_Format format = Format_from_SR_C(config->sample_rate, |
| 342 | get_channel_count_from_mask(config->channel_mask), config->format); |
| 343 | const NBAIO_Format offers[1] = {format}; |
| 344 | size_t numCounterOffers = 0; |
| 345 | // Create a MonoPipe with optional blocking set to true. |
| 346 | MonoPipe* sink = new MonoPipe(buffer_size_frames, format, true /*writeCanBlock*/); |
| 347 | // Negotiation between the source and sink cannot fail as the device open operation |
| 348 | // creates both ends of the pipe using the same audio format. |
| 349 | ssize_t index = sink->negotiate(offers, 1, NULL, numCounterOffers); |
| 350 | ALOG_ASSERT(index == 0); |
| 351 | MonoPipeReader* source = new MonoPipeReader(sink); |
| 352 | numCounterOffers = 0; |
| 353 | index = source->negotiate(offers, 1, NULL, numCounterOffers); |
| 354 | ALOG_ASSERT(index == 0); |
| 355 | ALOGV("submix_audio_device_create_pipe(): created pipe"); |
| 356 | |
| 357 | // Save references to the source and sink. |
| 358 | ALOG_ASSERT(rsxadev->rsxSink == NULL); |
| 359 | ALOG_ASSERT(rsxadev->rsxSource == NULL); |
| 360 | rsxadev->rsxSink = sink; |
| 361 | rsxadev->rsxSource = source; |
| 362 | // Store the sanitized audio format in the device so that it's possible to determine |
| 363 | // the format of the pipe source when opening the input device. |
| 364 | memcpy(&device_config->common, config, sizeof(device_config->common)); |
| 365 | device_config->buffer_size_frames = sink->maxFrames(); |
| 366 | device_config->buffer_period_size_frames = device_config->buffer_size_frames / |
| 367 | buffer_period_count; |
Stewart Miles | e54c12c | 2014-05-01 09:03:27 -0700 | [diff] [blame^] | 368 | if (in) device_config->pipe_frame_size = audio_stream_frame_size(&in->stream.common); |
| 369 | if (out) device_config->pipe_frame_size = audio_stream_frame_size(&out->stream.common); |
| 370 | SUBMIX_ALOGV("submix_audio_device_create_pipe(): pipe frame size %zd, pipe size %zd, " |
| 371 | "period size %zd", device_config->pipe_frame_size, |
| 372 | device_config->buffer_size_frames, device_config->buffer_period_size_frames); |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 373 | } |
| 374 | pthread_mutex_unlock(&rsxadev->lock); |
| 375 | } |
| 376 | |
| 377 | // Release references to the sink and source. Input and output threads may maintain references |
| 378 | // to these objects via StrongPointer (sp<MonoPipe> and sp<MonoPipeReader>) which they can use |
| 379 | // before they shutdown. |
| 380 | static void submix_audio_device_release_pipe(struct submix_audio_device * const rsxadev) |
| 381 | { |
| 382 | ALOGV("submix_audio_device_release_pipe()"); |
| 383 | rsxadev->rsxSink.clear(); |
| 384 | rsxadev->rsxSource.clear(); |
| 385 | } |
| 386 | |
| 387 | // Remove references to the specified input and output streams. When the device no longer |
| 388 | // references input and output streams destroy the associated pipe. |
| 389 | static void submix_audio_device_destroy_pipe(struct submix_audio_device * const rsxadev, |
| 390 | const struct submix_stream_in * const in, |
| 391 | const struct submix_stream_out * const out) |
| 392 | { |
| 393 | MonoPipe* sink; |
| 394 | pthread_mutex_lock(&rsxadev->lock); |
| 395 | ALOGV("submix_audio_device_destroy_pipe()"); |
| 396 | ALOG_ASSERT(in == NULL || rsxadev->input == in); |
| 397 | ALOG_ASSERT(out == NULL || rsxadev->output == out); |
| 398 | if (in != NULL) { |
| 399 | #if ENABLE_LEGACY_INPUT_OPEN |
| 400 | const_cast<struct submix_stream_in*>(in)->ref_count--; |
| 401 | if (in->ref_count == 0) { |
| 402 | rsxadev->input = NULL; |
| 403 | } |
| 404 | ALOGV("submix_audio_device_destroy_pipe(): input ref_count %d", in->ref_count); |
| 405 | #else |
| 406 | rsxadev->input = NULL; |
| 407 | #endif // ENABLE_LEGACY_INPUT_OPEN |
| 408 | } |
| 409 | if (out != NULL) rsxadev->output = NULL; |
| 410 | if (rsxadev->input != NULL && rsxadev->output != NULL) { |
| 411 | submix_audio_device_release_pipe(rsxadev); |
| 412 | ALOGV("submix_audio_device_destroy_pipe(): pipe destroyed"); |
| 413 | } |
| 414 | pthread_mutex_unlock(&rsxadev->lock); |
| 415 | } |
| 416 | |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 417 | // Sanitize the user specified audio config for a submix input / output stream. |
| 418 | static void submix_sanitize_config(struct audio_config * const config, const bool is_input_format) |
| 419 | { |
| 420 | config->channel_mask = is_input_format ? get_supported_channel_in_mask(config->channel_mask) : |
| 421 | get_supported_channel_out_mask(config->channel_mask); |
| 422 | config->sample_rate = get_supported_sample_rate(config->sample_rate); |
| 423 | config->format = DEFAULT_FORMAT; |
| 424 | } |
| 425 | |
| 426 | // Verify a submix input or output stream can be opened. |
| 427 | static bool submix_open_validate(const struct submix_audio_device * const rsxadev, |
| 428 | pthread_mutex_t * const lock, |
| 429 | const struct audio_config * const config, |
| 430 | const bool opening_input) |
| 431 | { |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 432 | bool input_open; |
| 433 | bool output_open; |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 434 | audio_config pipe_config; |
| 435 | |
| 436 | // Query the device for the current audio config and whether input and output streams are open. |
| 437 | pthread_mutex_lock(lock); |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 438 | output_open = rsxadev->output != NULL; |
| 439 | input_open = rsxadev->input != NULL; |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 440 | memcpy(&pipe_config, &rsxadev->config.common, sizeof(pipe_config)); |
| 441 | pthread_mutex_unlock(lock); |
| 442 | |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 443 | // If the stream is already open, don't open it again. |
| 444 | if (opening_input ? !ENABLE_LEGACY_INPUT_OPEN && input_open : output_open) { |
| 445 | ALOGE("submix_open_validate(): %s stream already open.", opening_input ? "Input" : |
| 446 | "Output"); |
| 447 | return false; |
| 448 | } |
| 449 | |
| 450 | SUBMIX_ALOGV("submix_open_validate(): sample rate=%d format=%x " |
| 451 | "%s_channel_mask=%x", config->sample_rate, config->format, |
| 452 | opening_input ? "in" : "out", config->channel_mask); |
| 453 | |
| 454 | // If either stream is open, verify the existing audio config the pipe matches the user |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 455 | // specified config. |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 456 | if (input_open || output_open) { |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 457 | const audio_config * const input_config = opening_input ? config : &pipe_config; |
| 458 | const audio_config * const output_config = opening_input ? &pipe_config : config; |
| 459 | // Get the channel mask of the open device. |
| 460 | pipe_config.channel_mask = |
| 461 | opening_input ? rsxadev->config.output_channel_mask : |
| 462 | rsxadev->config.input_channel_mask; |
| 463 | if (!audio_config_compare(input_config, output_config)) { |
| 464 | ALOGE("submix_open_validate(): Unsupported format."); |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 465 | return false; |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | return true; |
| 469 | } |
| 470 | |
Stewart Miles | e54c12c | 2014-05-01 09:03:27 -0700 | [diff] [blame^] | 471 | // Calculate the maximum size of the pipe buffer in frames for the specified stream. |
| 472 | static size_t calculate_stream_pipe_size_in_frames(const struct audio_stream *stream, |
| 473 | const struct submix_config *config, |
| 474 | const size_t pipe_frames) |
| 475 | { |
| 476 | const size_t stream_frame_size = audio_stream_frame_size(stream); |
| 477 | const size_t pipe_frame_size = config->pipe_frame_size; |
| 478 | const size_t max_frame_size = max(stream_frame_size, pipe_frame_size); |
| 479 | return (pipe_frames * config->pipe_frame_size) / max_frame_size; |
| 480 | } |
| 481 | |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 482 | /* audio HAL functions */ |
| 483 | |
| 484 | static uint32_t out_get_sample_rate(const struct audio_stream *stream) |
| 485 | { |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 486 | const struct submix_stream_out * const out = audio_stream_get_submix_stream_out( |
| 487 | const_cast<struct audio_stream *>(stream)); |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 488 | const uint32_t out_rate = out->dev->config.common.sample_rate; |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 489 | SUBMIX_ALOGV("out_get_sample_rate() returns %u", out_rate); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 490 | return out_rate; |
| 491 | } |
| 492 | |
| 493 | static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 494 | { |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 495 | if (!sample_rate_supported(rate)) { |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 496 | ALOGE("out_set_sample_rate(rate=%u) rate unsupported", rate); |
| 497 | return -ENOSYS; |
| 498 | } |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 499 | struct submix_stream_out * const out = audio_stream_get_submix_stream_out(stream); |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 500 | SUBMIX_ALOGV("out_set_sample_rate(rate=%u)", rate); |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 501 | out->dev->config.common.sample_rate = rate; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | static size_t out_get_buffer_size(const struct audio_stream *stream) |
| 506 | { |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 507 | const struct submix_stream_out * const out = audio_stream_get_submix_stream_out( |
| 508 | const_cast<struct audio_stream *>(stream)); |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 509 | const struct submix_config * const config = &out->dev->config; |
Stewart Miles | e54c12c | 2014-05-01 09:03:27 -0700 | [diff] [blame^] | 510 | const size_t buffer_size_frames = calculate_stream_pipe_size_in_frames( |
| 511 | stream, config, config->buffer_period_size_frames); |
| 512 | const size_t buffer_size_bytes = buffer_size_frames * audio_stream_frame_size(stream); |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 513 | SUBMIX_ALOGV("out_get_buffer_size() returns %zu bytes, %zu frames", |
Stewart Miles | e54c12c | 2014-05-01 09:03:27 -0700 | [diff] [blame^] | 514 | buffer_size_bytes, buffer_size_frames); |
| 515 | return buffer_size_bytes; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | static audio_channel_mask_t out_get_channels(const struct audio_stream *stream) |
| 519 | { |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 520 | const struct submix_stream_out * const out = audio_stream_get_submix_stream_out( |
| 521 | const_cast<struct audio_stream *>(stream)); |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 522 | uint32_t channel_mask = out->dev->config.output_channel_mask; |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 523 | SUBMIX_ALOGV("out_get_channels() returns %08x", channel_mask); |
| 524 | return channel_mask; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | static audio_format_t out_get_format(const struct audio_stream *stream) |
| 528 | { |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 529 | const struct submix_stream_out * const out = audio_stream_get_submix_stream_out( |
| 530 | const_cast<struct audio_stream *>(stream)); |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 531 | const audio_format_t format = out->dev->config.common.format; |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 532 | SUBMIX_ALOGV("out_get_format() returns %x", format); |
| 533 | return format; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | static int out_set_format(struct audio_stream *stream, audio_format_t format) |
| 537 | { |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 538 | const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(stream); |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 539 | if (format != out->dev->config.common.format) { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 540 | ALOGE("out_set_format(format=%x) format unsupported", format); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 541 | return -ENOSYS; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 542 | } |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 543 | SUBMIX_ALOGV("out_set_format(format=%x)", format); |
| 544 | return 0; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | static int out_standby(struct audio_stream *stream) |
| 548 | { |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 549 | struct submix_audio_device * const rsxadev = audio_stream_get_submix_stream_out(stream)->dev; |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 550 | ALOGI("out_standby()"); |
| 551 | |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 552 | pthread_mutex_lock(&rsxadev->lock); |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 553 | |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 554 | rsxadev->output_standby = true; |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 555 | |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 556 | pthread_mutex_unlock(&rsxadev->lock); |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 557 | |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | static int out_dump(const struct audio_stream *stream, int fd) |
| 562 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 563 | (void)stream; |
| 564 | (void)fd; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 569 | { |
Jean-Michel Trivi | d441303 | 2012-09-30 11:08:06 -0700 | [diff] [blame] | 570 | int exiting = -1; |
| 571 | AudioParameter parms = AudioParameter(String8(kvpairs)); |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 572 | SUBMIX_ALOGV("out_set_parameters() kvpairs='%s'", kvpairs); |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 573 | |
Jean-Michel Trivi | d441303 | 2012-09-30 11:08:06 -0700 | [diff] [blame] | 574 | // FIXME this is using hard-coded strings but in the future, this functionality will be |
| 575 | // converted to use audio HAL extensions required to support tunneling |
| 576 | if ((parms.getInt(String8("exiting"), exiting) == NO_ERROR) && (exiting > 0)) { |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 577 | struct submix_audio_device * const rsxadev = |
| 578 | audio_stream_get_submix_stream_out(stream)->dev; |
| 579 | pthread_mutex_lock(&rsxadev->lock); |
Jean-Michel Trivi | eafbfa4 | 2012-12-18 11:30:33 -0800 | [diff] [blame] | 580 | { // using the sink |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 581 | sp<MonoPipe> sink = rsxadev->rsxSink; |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 582 | if (sink == NULL) { |
| 583 | pthread_mutex_unlock(&rsxadev->lock); |
Jean-Michel Trivi | eafbfa4 | 2012-12-18 11:30:33 -0800 | [diff] [blame] | 584 | return 0; |
| 585 | } |
Jean-Michel Trivi | d441303 | 2012-09-30 11:08:06 -0700 | [diff] [blame] | 586 | |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 587 | ALOGI("out_set_parameters(): shutdown"); |
Jean-Michel Trivi | eafbfa4 | 2012-12-18 11:30:33 -0800 | [diff] [blame] | 588 | sink->shutdown(true); |
| 589 | } // done using the sink |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 590 | pthread_mutex_unlock(&rsxadev->lock); |
Jean-Michel Trivi | d441303 | 2012-09-30 11:08:06 -0700 | [diff] [blame] | 591 | } |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | static char * out_get_parameters(const struct audio_stream *stream, const char *keys) |
| 596 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 597 | (void)stream; |
| 598 | (void)keys; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 599 | return strdup(""); |
| 600 | } |
| 601 | |
| 602 | static uint32_t out_get_latency(const struct audio_stream_out *stream) |
| 603 | { |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 604 | const struct submix_stream_out * const out = audio_stream_out_get_submix_stream_out( |
| 605 | const_cast<struct audio_stream_out *>(stream)); |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 606 | const struct submix_config * const config = &out->dev->config; |
Stewart Miles | e54c12c | 2014-05-01 09:03:27 -0700 | [diff] [blame^] | 607 | const size_t buffer_size_frames = calculate_stream_pipe_size_in_frames( |
| 608 | &stream->common, config, config->buffer_size_frames); |
| 609 | const uint32_t latency_ms = (buffer_size_frames * 1000) / config->common.sample_rate; |
| 610 | SUBMIX_ALOGV("out_get_latency() returns %u ms, size in frames %zu, sample rate %u", |
| 611 | latency_ms, buffer_size_frames, config->common.sample_rate); |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 612 | return latency_ms; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | static int out_set_volume(struct audio_stream_out *stream, float left, |
| 616 | float right) |
| 617 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 618 | (void)stream; |
| 619 | (void)left; |
| 620 | (void)right; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 621 | return -ENOSYS; |
| 622 | } |
| 623 | |
| 624 | static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, |
| 625 | size_t bytes) |
| 626 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 627 | SUBMIX_ALOGV("out_write(bytes=%zd)", bytes); |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 628 | ssize_t written_frames = 0; |
Jean-Michel Trivi | 90b0fbd | 2012-10-30 19:03:22 -0700 | [diff] [blame] | 629 | const size_t frame_size = audio_stream_frame_size(&stream->common); |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 630 | struct submix_stream_out * const out = audio_stream_out_get_submix_stream_out(stream); |
| 631 | struct submix_audio_device * const rsxadev = out->dev; |
Jean-Michel Trivi | 90b0fbd | 2012-10-30 19:03:22 -0700 | [diff] [blame] | 632 | const size_t frames = bytes / frame_size; |
| 633 | |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 634 | pthread_mutex_lock(&rsxadev->lock); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 635 | |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 636 | rsxadev->output_standby = false; |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 637 | |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 638 | sp<MonoPipe> sink = rsxadev->rsxSink; |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 639 | if (sink != NULL) { |
Jean-Michel Trivi | 90b0fbd | 2012-10-30 19:03:22 -0700 | [diff] [blame] | 640 | if (sink->isShutdown()) { |
Jean-Michel Trivi | eafbfa4 | 2012-12-18 11:30:33 -0800 | [diff] [blame] | 641 | sink.clear(); |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 642 | pthread_mutex_unlock(&rsxadev->lock); |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 643 | SUBMIX_ALOGV("out_write(): pipe shutdown, ignoring the write."); |
Jean-Michel Trivi | 90b0fbd | 2012-10-30 19:03:22 -0700 | [diff] [blame] | 644 | // the pipe has already been shutdown, this buffer will be lost but we must |
| 645 | // simulate timing so we don't drain the output faster than realtime |
| 646 | usleep(frames * 1000000 / out_get_sample_rate(&stream->common)); |
| 647 | return bytes; |
| 648 | } |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 649 | } else { |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 650 | pthread_mutex_unlock(&rsxadev->lock); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 651 | ALOGE("out_write without a pipe!"); |
| 652 | ALOG_ASSERT("out_write without a pipe!"); |
| 653 | return 0; |
| 654 | } |
| 655 | |
Stewart Miles | 2d199fe | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 656 | // If the write to the sink would block when no input stream is present, flush enough frames |
| 657 | // from the pipe to make space to write the most recent data. |
| 658 | { |
| 659 | const size_t availableToWrite = sink->availableToWrite(); |
| 660 | sp<MonoPipeReader> source = rsxadev->rsxSource; |
| 661 | if (rsxadev->input == NULL && availableToWrite < frames) { |
| 662 | static uint8_t flush_buffer[64]; |
| 663 | const size_t flushBufferSizeFrames = sizeof(flush_buffer) / frame_size; |
| 664 | size_t frames_to_flush_from_source = frames - availableToWrite; |
| 665 | SUBMIX_ALOGV("out_write(): flushing %d frames from the pipe to avoid blocking", |
| 666 | frames_to_flush_from_source); |
| 667 | while (frames_to_flush_from_source) { |
| 668 | const size_t flush_size = min(frames_to_flush_from_source, flushBufferSizeFrames); |
| 669 | frames_to_flush_from_source -= flush_size; |
| 670 | source->read(flush_buffer, flush_size, AudioBufferProvider::kInvalidPTS); |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 675 | pthread_mutex_unlock(&rsxadev->lock); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 676 | |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 677 | written_frames = sink->write(buffer, frames); |
Jean-Michel Trivi | eafbfa4 | 2012-12-18 11:30:33 -0800 | [diff] [blame] | 678 | |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 679 | if (written_frames < 0) { |
| 680 | if (written_frames == (ssize_t)NEGOTIATE) { |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 681 | ALOGE("out_write() write to pipe returned NEGOTIATE"); |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 682 | |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 683 | pthread_mutex_lock(&rsxadev->lock); |
Jean-Michel Trivi | eafbfa4 | 2012-12-18 11:30:33 -0800 | [diff] [blame] | 684 | sink.clear(); |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 685 | pthread_mutex_unlock(&rsxadev->lock); |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 686 | |
| 687 | written_frames = 0; |
Jean-Michel Trivi | 6acd966 | 2012-09-11 19:19:08 -0700 | [diff] [blame] | 688 | return 0; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 689 | } else { |
| 690 | // write() returned UNDERRUN or WOULD_BLOCK, retry |
Colin Cross | 5685a08 | 2014-04-18 15:45:42 -0700 | [diff] [blame] | 691 | ALOGE("out_write() write to pipe returned unexpected %zd", written_frames); |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 692 | written_frames = sink->write(buffer, frames); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 696 | pthread_mutex_lock(&rsxadev->lock); |
Jean-Michel Trivi | eafbfa4 | 2012-12-18 11:30:33 -0800 | [diff] [blame] | 697 | sink.clear(); |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 698 | pthread_mutex_unlock(&rsxadev->lock); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 699 | |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 700 | if (written_frames < 0) { |
Colin Cross | 5685a08 | 2014-04-18 15:45:42 -0700 | [diff] [blame] | 701 | ALOGE("out_write() failed writing to pipe with %zd", written_frames); |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 702 | return 0; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 703 | } |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 704 | const ssize_t written_bytes = written_frames * frame_size; |
| 705 | SUBMIX_ALOGV("out_write() wrote %zd bytes %zd frames)", written_bytes, written_frames); |
| 706 | return written_bytes; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | static int out_get_render_position(const struct audio_stream_out *stream, |
| 710 | uint32_t *dsp_frames) |
| 711 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 712 | (void)stream; |
| 713 | (void)dsp_frames; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 714 | return -EINVAL; |
| 715 | } |
| 716 | |
| 717 | static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 718 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 719 | (void)stream; |
| 720 | (void)effect; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 725 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 726 | (void)stream; |
| 727 | (void)effect; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | static int out_get_next_write_timestamp(const struct audio_stream_out *stream, |
| 732 | int64_t *timestamp) |
| 733 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 734 | (void)stream; |
| 735 | (void)timestamp; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 736 | return -EINVAL; |
| 737 | } |
| 738 | |
| 739 | /** audio_stream_in implementation **/ |
| 740 | static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| 741 | { |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 742 | const struct submix_stream_in * const in = audio_stream_get_submix_stream_in( |
| 743 | const_cast<struct audio_stream*>(stream)); |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 744 | SUBMIX_ALOGV("in_get_sample_rate() returns %u", in->dev->config.common.sample_rate); |
| 745 | return in->dev->config.common.sample_rate; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 749 | { |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 750 | const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(stream); |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 751 | if (!sample_rate_supported(rate)) { |
| 752 | ALOGE("in_set_sample_rate(rate=%u) rate unsupported", rate); |
| 753 | return -ENOSYS; |
| 754 | } |
| 755 | in->dev->config.common.sample_rate = rate; |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 756 | SUBMIX_ALOGV("in_set_sample_rate() set %u", rate); |
| 757 | return 0; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | static size_t in_get_buffer_size(const struct audio_stream *stream) |
| 761 | { |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 762 | const struct submix_stream_in * const in = audio_stream_get_submix_stream_in( |
| 763 | const_cast<struct audio_stream*>(stream)); |
Stewart Miles | e54c12c | 2014-05-01 09:03:27 -0700 | [diff] [blame^] | 764 | const struct submix_config * const config = &in->dev->config; |
| 765 | const size_t buffer_size_frames = calculate_stream_pipe_size_in_frames( |
| 766 | stream, config, config->buffer_period_size_frames); |
| 767 | const size_t buffer_size_bytes = buffer_size_frames * audio_stream_frame_size(stream); |
| 768 | SUBMIX_ALOGV("in_get_buffer_size() returns %zu bytes, %zu frames", buffer_size_bytes, |
| 769 | buffer_size_frames); |
| 770 | return buffer_size_bytes; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | static audio_channel_mask_t in_get_channels(const struct audio_stream *stream) |
| 774 | { |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 775 | const struct submix_stream_in * const in = audio_stream_get_submix_stream_in( |
| 776 | const_cast<struct audio_stream*>(stream)); |
| 777 | const audio_channel_mask_t channel_mask = in->dev->config.input_channel_mask; |
| 778 | SUBMIX_ALOGV("in_get_channels() returns %x", channel_mask); |
| 779 | return channel_mask; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | static audio_format_t in_get_format(const struct audio_stream *stream) |
| 783 | { |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 784 | const struct submix_stream_in * const in = audio_stream_get_submix_stream_in( |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 785 | const_cast<struct audio_stream*>(stream)); |
| 786 | const audio_format_t format = in->dev->config.common.format; |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 787 | SUBMIX_ALOGV("in_get_format() returns %x", format); |
| 788 | return format; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | static int in_set_format(struct audio_stream *stream, audio_format_t format) |
| 792 | { |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 793 | const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(stream); |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 794 | if (format != in->dev->config.common.format) { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 795 | ALOGE("in_set_format(format=%x) format unsupported", format); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 796 | return -ENOSYS; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 797 | } |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 798 | SUBMIX_ALOGV("in_set_format(format=%x)", format); |
| 799 | return 0; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | static int in_standby(struct audio_stream *stream) |
| 803 | { |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 804 | struct submix_audio_device * const rsxadev = audio_stream_get_submix_stream_in(stream)->dev; |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 805 | ALOGI("in_standby()"); |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 806 | |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 807 | pthread_mutex_lock(&rsxadev->lock); |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 808 | |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 809 | rsxadev->input_standby = true; |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 810 | |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 811 | pthread_mutex_unlock(&rsxadev->lock); |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 812 | |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | static int in_dump(const struct audio_stream *stream, int fd) |
| 817 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 818 | (void)stream; |
| 819 | (void)fd; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 824 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 825 | (void)stream; |
| 826 | (void)kvpairs; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 827 | return 0; |
| 828 | } |
| 829 | |
| 830 | static char * in_get_parameters(const struct audio_stream *stream, |
| 831 | const char *keys) |
| 832 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 833 | (void)stream; |
| 834 | (void)keys; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 835 | return strdup(""); |
| 836 | } |
| 837 | |
| 838 | static int in_set_gain(struct audio_stream_in *stream, float gain) |
| 839 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 840 | (void)stream; |
| 841 | (void)gain; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 842 | return 0; |
| 843 | } |
| 844 | |
| 845 | static ssize_t in_read(struct audio_stream_in *stream, void* buffer, |
| 846 | size_t bytes) |
| 847 | { |
| 848 | ssize_t frames_read = -1977; |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 849 | struct submix_stream_in * const in = audio_stream_in_get_submix_stream_in(stream); |
| 850 | struct submix_audio_device * const rsxadev = in->dev; |
Stewart Miles | e54c12c | 2014-05-01 09:03:27 -0700 | [diff] [blame^] | 851 | struct audio_config *format; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 852 | const size_t frame_size = audio_stream_frame_size(&stream->common); |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 853 | const size_t frames_to_read = bytes / frame_size; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 854 | |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 855 | SUBMIX_ALOGV("in_read bytes=%zu", bytes); |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 856 | pthread_mutex_lock(&rsxadev->lock); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 857 | |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 858 | const bool output_standby_transition = (in->output_standby != in->dev->output_standby); |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 859 | in->output_standby = rsxadev->output_standby; |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 860 | |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 861 | if (rsxadev->input_standby || output_standby_transition) { |
| 862 | rsxadev->input_standby = false; |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 863 | // keep track of when we exit input standby (== first read == start "real recording") |
| 864 | // or when we start recording silence, and reset projected time |
| 865 | int rc = clock_gettime(CLOCK_MONOTONIC, &in->record_start_time); |
| 866 | if (rc == 0) { |
| 867 | in->read_counter_frames = 0; |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | in->read_counter_frames += frames_to_read; |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 872 | size_t remaining_frames = frames_to_read; |
Jean-Michel Trivi | eafbfa4 | 2012-12-18 11:30:33 -0800 | [diff] [blame] | 873 | |
| 874 | { |
| 875 | // about to read from audio source |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 876 | sp<MonoPipeReader> source = rsxadev->rsxSource; |
| 877 | if (source == NULL) { |
Jean-Michel Trivi | eafbfa4 | 2012-12-18 11:30:33 -0800 | [diff] [blame] | 878 | ALOGE("no audio pipe yet we're trying to read!"); |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 879 | pthread_mutex_unlock(&rsxadev->lock); |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 880 | usleep(frames_to_read * 1000000 / in_get_sample_rate(&stream->common)); |
Jean-Michel Trivi | eafbfa4 | 2012-12-18 11:30:33 -0800 | [diff] [blame] | 881 | memset(buffer, 0, bytes); |
| 882 | return bytes; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 883 | } |
Jean-Michel Trivi | eafbfa4 | 2012-12-18 11:30:33 -0800 | [diff] [blame] | 884 | |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 885 | pthread_mutex_unlock(&rsxadev->lock); |
Jean-Michel Trivi | eafbfa4 | 2012-12-18 11:30:33 -0800 | [diff] [blame] | 886 | |
| 887 | // read the data from the pipe (it's non blocking) |
| 888 | int attempts = 0; |
| 889 | char* buff = (char*)buffer; |
Stewart Miles | e54c12c | 2014-05-01 09:03:27 -0700 | [diff] [blame^] | 890 | #if ENABLE_CHANNEL_CONVERSION |
| 891 | // Determine whether channel conversion is required. |
| 892 | const uint32_t input_channels = get_channel_count_from_mask( |
| 893 | rsxadev->config.input_channel_mask); |
| 894 | const uint32_t output_channels = get_channel_count_from_mask( |
| 895 | rsxadev->config.output_channel_mask); |
| 896 | if (input_channels != output_channels) { |
| 897 | SUBMIX_ALOGV("in_read(): %d output channels will be converted to %d " |
| 898 | "input channels", output_channels, input_channels); |
| 899 | // Only support 16-bit PCM channel conversion from mono to stereo or stereo to mono. |
| 900 | ALOG_ASSERT(rsxadev->config.common.format == AUDIO_FORMAT_PCM_16_BIT); |
| 901 | ALOG_ASSERT((input_channels == 1 && output_channels == 2) || |
| 902 | (input_channels == 2 && output_channels == 1)); |
| 903 | } |
| 904 | #endif // ENABLE_CHANNEL_CONVERSION |
| 905 | |
Jean-Michel Trivi | eafbfa4 | 2012-12-18 11:30:33 -0800 | [diff] [blame] | 906 | while ((remaining_frames > 0) && (attempts < MAX_READ_ATTEMPTS)) { |
Stewart Miles | e54c12c | 2014-05-01 09:03:27 -0700 | [diff] [blame^] | 907 | size_t read_frames = remaining_frames; |
| 908 | #if ENABLE_CHANNEL_CONVERSION |
| 909 | if (output_channels == 1 && input_channels == 2) { |
| 910 | // Need to read half the requested frames since the converted output |
| 911 | // data will take twice the space (mono->stereo). |
| 912 | read_frames /= 2; |
| 913 | } |
| 914 | #endif // ENABLE_CHANNEL_CONVERSION |
| 915 | |
| 916 | SUBMIX_ALOGV("in_read(): frames available to read %zd", source->availableToRead()); |
| 917 | |
| 918 | frames_read = source->read(buff, read_frames, AudioBufferProvider::kInvalidPTS); |
| 919 | |
| 920 | SUBMIX_ALOGV("in_read(): frames read %zd", frames_read); |
| 921 | |
| 922 | #if ENABLE_CHANNEL_CONVERSION |
| 923 | // Perform in-place channel conversion. |
| 924 | // NOTE: In the following "input stream" refers to the data returned by this function |
| 925 | // and "output stream" refers to the data read from the pipe. |
| 926 | if (input_channels != output_channels && frames_read > 0) { |
| 927 | int16_t *data = (int16_t*)buff; |
| 928 | if (output_channels == 2 && input_channels == 1) { |
| 929 | // Offset into the output stream data in samples. |
| 930 | ssize_t output_stream_offset = 0; |
| 931 | for (ssize_t input_stream_frame = 0; input_stream_frame < frames_read; |
| 932 | input_stream_frame++, output_stream_offset += 2) { |
| 933 | // Average the content from both channels. |
| 934 | data[input_stream_frame] = ((int32_t)data[output_stream_offset] + |
| 935 | (int32_t)data[output_stream_offset + 1]) / 2; |
| 936 | } |
| 937 | } else if (output_channels == 1 && input_channels == 2) { |
| 938 | // Offset into the input stream data in samples. |
| 939 | ssize_t input_stream_offset = (frames_read - 1) * 2; |
| 940 | for (ssize_t output_stream_frame = frames_read - 1; output_stream_frame >= 0; |
| 941 | output_stream_frame--, input_stream_offset -= 2) { |
| 942 | const short sample = data[output_stream_frame]; |
| 943 | data[input_stream_offset] = sample; |
| 944 | data[input_stream_offset + 1] = sample; |
| 945 | } |
| 946 | } |
| 947 | } |
| 948 | #endif // ENABLE_CHANNEL_CONVERSION |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 949 | |
Jean-Michel Trivi | eafbfa4 | 2012-12-18 11:30:33 -0800 | [diff] [blame] | 950 | if (frames_read > 0) { |
| 951 | remaining_frames -= frames_read; |
| 952 | buff += frames_read * frame_size; |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 953 | SUBMIX_ALOGV(" in_read (att=%d) got %zd frames, remaining=%zu", |
| 954 | attempts, frames_read, remaining_frames); |
Jean-Michel Trivi | eafbfa4 | 2012-12-18 11:30:33 -0800 | [diff] [blame] | 955 | } else { |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 956 | attempts++; |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 957 | SUBMIX_ALOGE(" in_read read returned %zd", frames_read); |
Jean-Michel Trivi | eafbfa4 | 2012-12-18 11:30:33 -0800 | [diff] [blame] | 958 | usleep(READ_ATTEMPT_SLEEP_MS * 1000); |
| 959 | } |
| 960 | } |
| 961 | // done using the source |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 962 | pthread_mutex_lock(&rsxadev->lock); |
Jean-Michel Trivi | eafbfa4 | 2012-12-18 11:30:33 -0800 | [diff] [blame] | 963 | source.clear(); |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 964 | pthread_mutex_unlock(&rsxadev->lock); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Jean-Michel Trivi | 6acd966 | 2012-09-11 19:19:08 -0700 | [diff] [blame] | 967 | if (remaining_frames > 0) { |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 968 | const size_t remaining_bytes = remaining_frames * frame_size; |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 969 | SUBMIX_ALOGV(" remaining_frames = %zu", remaining_frames); |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 970 | memset(((char*)buffer)+ bytes - remaining_bytes, 0, remaining_bytes); |
Jean-Michel Trivi | 6acd966 | 2012-09-11 19:19:08 -0700 | [diff] [blame] | 971 | } |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 972 | |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 973 | // compute how much we need to sleep after reading the data by comparing the wall clock with |
| 974 | // the projected time at which we should return. |
| 975 | struct timespec time_after_read;// wall clock after reading from the pipe |
| 976 | struct timespec record_duration;// observed record duration |
| 977 | int rc = clock_gettime(CLOCK_MONOTONIC, &time_after_read); |
| 978 | const uint32_t sample_rate = in_get_sample_rate(&stream->common); |
| 979 | if (rc == 0) { |
| 980 | // for how long have we been recording? |
| 981 | record_duration.tv_sec = time_after_read.tv_sec - in->record_start_time.tv_sec; |
| 982 | record_duration.tv_nsec = time_after_read.tv_nsec - in->record_start_time.tv_nsec; |
| 983 | if (record_duration.tv_nsec < 0) { |
| 984 | record_duration.tv_sec--; |
| 985 | record_duration.tv_nsec += 1000000000; |
| 986 | } |
| 987 | |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 988 | // read_counter_frames contains the number of frames that have been read since the |
| 989 | // beginning of recording (including this call): it's converted to usec and compared to |
| 990 | // how long we've been recording for, which gives us how long we must wait to sync the |
| 991 | // projected recording time, and the observed recording time. |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 992 | long projected_vs_observed_offset_us = |
| 993 | ((int64_t)(in->read_counter_frames |
| 994 | - (record_duration.tv_sec*sample_rate))) |
| 995 | * 1000000 / sample_rate |
| 996 | - (record_duration.tv_nsec / 1000); |
| 997 | |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 998 | SUBMIX_ALOGV(" record duration %5lds %3ldms, will wait: %7ldus", |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 999 | record_duration.tv_sec, record_duration.tv_nsec/1000000, |
| 1000 | projected_vs_observed_offset_us); |
| 1001 | if (projected_vs_observed_offset_us > 0) { |
| 1002 | usleep(projected_vs_observed_offset_us); |
| 1003 | } |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1004 | } |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 1005 | |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1006 | SUBMIX_ALOGV("in_read returns %zu", bytes); |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 1007 | return bytes; |
| 1008 | |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) |
| 1012 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1013 | (void)stream; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1014 | return 0; |
| 1015 | } |
| 1016 | |
| 1017 | static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 1018 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1019 | (void)stream; |
| 1020 | (void)effect; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 1025 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1026 | (void)stream; |
| 1027 | (void)effect; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1028 | return 0; |
| 1029 | } |
| 1030 | |
| 1031 | static int adev_open_output_stream(struct audio_hw_device *dev, |
| 1032 | audio_io_handle_t handle, |
| 1033 | audio_devices_t devices, |
| 1034 | audio_output_flags_t flags, |
| 1035 | struct audio_config *config, |
| 1036 | struct audio_stream_out **stream_out) |
| 1037 | { |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1038 | struct submix_audio_device * const rsxadev = audio_hw_device_get_submix_audio_device(dev); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1039 | ALOGV("adev_open_output_stream()"); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1040 | struct submix_stream_out *out; |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1041 | (void)handle; |
| 1042 | (void)devices; |
| 1043 | (void)flags; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1044 | |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1045 | *stream_out = NULL; |
| 1046 | |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1047 | // Make sure it's possible to open the device given the current audio config. |
| 1048 | submix_sanitize_config(config, false); |
| 1049 | if (!submix_open_validate(rsxadev, &rsxadev->lock, config, false)) { |
| 1050 | ALOGE("adev_open_output_stream(): Unable to open output stream."); |
| 1051 | return -EINVAL; |
| 1052 | } |
| 1053 | |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1054 | out = (struct submix_stream_out *)calloc(1, sizeof(struct submix_stream_out)); |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1055 | if (!out) return -ENOMEM; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1056 | |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1057 | // Initialize the function pointer tables (v-tables). |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1058 | out->stream.common.get_sample_rate = out_get_sample_rate; |
| 1059 | out->stream.common.set_sample_rate = out_set_sample_rate; |
| 1060 | out->stream.common.get_buffer_size = out_get_buffer_size; |
| 1061 | out->stream.common.get_channels = out_get_channels; |
| 1062 | out->stream.common.get_format = out_get_format; |
| 1063 | out->stream.common.set_format = out_set_format; |
| 1064 | out->stream.common.standby = out_standby; |
| 1065 | out->stream.common.dump = out_dump; |
| 1066 | out->stream.common.set_parameters = out_set_parameters; |
| 1067 | out->stream.common.get_parameters = out_get_parameters; |
| 1068 | out->stream.common.add_audio_effect = out_add_audio_effect; |
| 1069 | out->stream.common.remove_audio_effect = out_remove_audio_effect; |
| 1070 | out->stream.get_latency = out_get_latency; |
| 1071 | out->stream.set_volume = out_set_volume; |
| 1072 | out->stream.write = out_write; |
| 1073 | out->stream.get_render_position = out_get_render_position; |
| 1074 | out->stream.get_next_write_timestamp = out_get_next_write_timestamp; |
| 1075 | |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1076 | // If the sink has been shutdown, delete the pipe so that it's recreated. |
| 1077 | pthread_mutex_lock(&rsxadev->lock); |
| 1078 | if (rsxadev->rsxSink != NULL && rsxadev->rsxSink->isShutdown()) { |
| 1079 | submix_audio_device_release_pipe(rsxadev); |
| 1080 | } |
| 1081 | pthread_mutex_unlock(&rsxadev->lock); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1082 | |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1083 | // Store a pointer to the device from the output stream. |
| 1084 | out->dev = rsxadev; |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1085 | // Initialize the pipe. |
| 1086 | ALOGV("adev_open_output_stream(): Initializing pipe"); |
| 1087 | submix_audio_device_create_pipe(rsxadev, config, DEFAULT_PIPE_SIZE_IN_FRAMES, |
| 1088 | DEFAULT_PIPE_PERIOD_COUNT, NULL, out); |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1089 | // Return the output stream. |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1090 | *stream_out = &out->stream; |
| 1091 | |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1092 | return 0; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | static void adev_close_output_stream(struct audio_hw_device *dev, |
| 1096 | struct audio_stream_out *stream) |
| 1097 | { |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1098 | struct submix_stream_out * const out = audio_stream_out_get_submix_stream_out(stream); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1099 | ALOGV("adev_close_output_stream()"); |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1100 | submix_audio_device_destroy_pipe(audio_hw_device_get_submix_audio_device(dev), NULL, out); |
| 1101 | free(out); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) |
| 1105 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1106 | (void)dev; |
| 1107 | (void)kvpairs; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1108 | return -ENOSYS; |
| 1109 | } |
| 1110 | |
| 1111 | static char * adev_get_parameters(const struct audio_hw_device *dev, |
| 1112 | const char *keys) |
| 1113 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1114 | (void)dev; |
| 1115 | (void)keys; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1116 | return strdup("");; |
| 1117 | } |
| 1118 | |
| 1119 | static int adev_init_check(const struct audio_hw_device *dev) |
| 1120 | { |
| 1121 | ALOGI("adev_init_check()"); |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1122 | (void)dev; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1123 | return 0; |
| 1124 | } |
| 1125 | |
| 1126 | static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) |
| 1127 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1128 | (void)dev; |
| 1129 | (void)volume; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1130 | return -ENOSYS; |
| 1131 | } |
| 1132 | |
| 1133 | static int adev_set_master_volume(struct audio_hw_device *dev, float volume) |
| 1134 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1135 | (void)dev; |
| 1136 | (void)volume; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1137 | return -ENOSYS; |
| 1138 | } |
| 1139 | |
| 1140 | static int adev_get_master_volume(struct audio_hw_device *dev, float *volume) |
| 1141 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1142 | (void)dev; |
| 1143 | (void)volume; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1144 | return -ENOSYS; |
| 1145 | } |
| 1146 | |
| 1147 | static int adev_set_master_mute(struct audio_hw_device *dev, bool muted) |
| 1148 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1149 | (void)dev; |
| 1150 | (void)muted; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1151 | return -ENOSYS; |
| 1152 | } |
| 1153 | |
| 1154 | static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted) |
| 1155 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1156 | (void)dev; |
| 1157 | (void)muted; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1158 | return -ENOSYS; |
| 1159 | } |
| 1160 | |
| 1161 | static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) |
| 1162 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1163 | (void)dev; |
| 1164 | (void)mode; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1165 | return 0; |
| 1166 | } |
| 1167 | |
| 1168 | static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) |
| 1169 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1170 | (void)dev; |
| 1171 | (void)state; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1172 | return -ENOSYS; |
| 1173 | } |
| 1174 | |
| 1175 | static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) |
| 1176 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1177 | (void)dev; |
| 1178 | (void)state; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1179 | return -ENOSYS; |
| 1180 | } |
| 1181 | |
| 1182 | static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev, |
| 1183 | const struct audio_config *config) |
| 1184 | { |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1185 | if (audio_is_linear_pcm(config->format)) { |
| 1186 | const size_t buffer_period_size_frames = |
| 1187 | audio_hw_device_get_submix_audio_device(const_cast<struct audio_hw_device*>(dev))-> |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1188 | config.buffer_period_size_frames; |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1189 | const size_t frame_size_in_bytes = get_channel_count_from_mask(config->channel_mask) * |
| 1190 | audio_bytes_per_sample(config->format); |
| 1191 | const size_t buffer_size = buffer_period_size_frames * frame_size_in_bytes; |
| 1192 | SUBMIX_ALOGV("out_get_buffer_size() returns %zu bytes, %zu frames", |
| 1193 | buffer_size, buffer_period_size_frames); |
| 1194 | return buffer_size; |
| 1195 | } |
| 1196 | return 0; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | static int adev_open_input_stream(struct audio_hw_device *dev, |
| 1200 | audio_io_handle_t handle, |
| 1201 | audio_devices_t devices, |
| 1202 | struct audio_config *config, |
| 1203 | struct audio_stream_in **stream_in) |
| 1204 | { |
Stewart Miles | f645c5e | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1205 | struct submix_audio_device *rsxadev = audio_hw_device_get_submix_audio_device(dev); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1206 | struct submix_stream_in *in; |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1207 | ALOGI("adev_open_input_stream()"); |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1208 | (void)handle; |
| 1209 | (void)devices; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1210 | |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1211 | *stream_in = NULL; |
| 1212 | |
Stewart Miles | 7072684 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1213 | // Make sure it's possible to open the device given the current audio config. |
| 1214 | submix_sanitize_config(config, true); |
| 1215 | if (!submix_open_validate(rsxadev, &rsxadev->lock, config, true)) { |
| 1216 | ALOGE("adev_open_input_stream(): Unable to open input stream."); |
| 1217 | return -EINVAL; |
| 1218 | } |
| 1219 | |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1220 | #if ENABLE_LEGACY_INPUT_OPEN |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1221 | pthread_mutex_lock(&rsxadev->lock); |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1222 | in = rsxadev->input; |
| 1223 | if (in) { |
| 1224 | in->ref_count++; |
| 1225 | sp<MonoPipe> sink = rsxadev->rsxSink; |
| 1226 | ALOG_ASSERT(sink != NULL); |
| 1227 | // If the sink has been shutdown, delete the pipe. |
| 1228 | if (sink->isShutdown()) submix_audio_device_release_pipe(rsxadev); |
| 1229 | } |
| 1230 | pthread_mutex_unlock(&rsxadev->lock); |
| 1231 | #else |
| 1232 | in = NULL; |
| 1233 | #endif // ENABLE_LEGACY_INPUT_OPEN |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1234 | |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1235 | if (!in) { |
| 1236 | in = (struct submix_stream_in *)calloc(1, sizeof(struct submix_stream_in)); |
| 1237 | if (!in) return -ENOMEM; |
| 1238 | in->ref_count = 1; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1239 | |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1240 | // Initialize the function pointer tables (v-tables). |
| 1241 | in->stream.common.get_sample_rate = in_get_sample_rate; |
| 1242 | in->stream.common.set_sample_rate = in_set_sample_rate; |
| 1243 | in->stream.common.get_buffer_size = in_get_buffer_size; |
| 1244 | in->stream.common.get_channels = in_get_channels; |
| 1245 | in->stream.common.get_format = in_get_format; |
| 1246 | in->stream.common.set_format = in_set_format; |
| 1247 | in->stream.common.standby = in_standby; |
| 1248 | in->stream.common.dump = in_dump; |
| 1249 | in->stream.common.set_parameters = in_set_parameters; |
| 1250 | in->stream.common.get_parameters = in_get_parameters; |
| 1251 | in->stream.common.add_audio_effect = in_add_audio_effect; |
| 1252 | in->stream.common.remove_audio_effect = in_remove_audio_effect; |
| 1253 | in->stream.set_gain = in_set_gain; |
| 1254 | in->stream.read = in_read; |
| 1255 | in->stream.get_input_frames_lost = in_get_input_frames_lost; |
| 1256 | } |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1257 | |
Stewart Miles | 568e66f | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1258 | // Initialize the input stream. |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 1259 | in->read_counter_frames = 0; |
| 1260 | in->output_standby = rsxadev->output_standby; |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1261 | in->dev = rsxadev; |
| 1262 | // Initialize the pipe. |
| 1263 | submix_audio_device_create_pipe(rsxadev, config, DEFAULT_PIPE_SIZE_IN_FRAMES, |
| 1264 | DEFAULT_PIPE_PERIOD_COUNT, in, NULL); |
| 1265 | // Return the input stream. |
| 1266 | *stream_in = &in->stream; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1267 | |
| 1268 | return 0; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | static void adev_close_input_stream(struct audio_hw_device *dev, |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1272 | struct audio_stream_in *stream) |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1273 | { |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1274 | struct submix_stream_in * const in = audio_stream_in_get_submix_stream_in(stream); |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1275 | ALOGV("adev_close_input_stream()"); |
Stewart Miles | 3dd36f9 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1276 | submix_audio_device_destroy_pipe(audio_hw_device_get_submix_audio_device(dev), in, NULL); |
| 1277 | #if ENABLE_LEGACY_INPUT_OPEN |
| 1278 | if (in->ref_count == 0) free(in); |
| 1279 | #else |
| 1280 | free(in); |
| 1281 | #endif // ENABLE_LEGACY_INPUT_OPEN |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | static int adev_dump(const audio_hw_device_t *device, int fd) |
| 1285 | { |
Stewart Miles | c049a0a | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 1286 | (void)device; |
| 1287 | (void)fd; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1288 | return 0; |
| 1289 | } |
| 1290 | |
| 1291 | static int adev_close(hw_device_t *device) |
| 1292 | { |
| 1293 | ALOGI("adev_close()"); |
| 1294 | free(device); |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1298 | static int adev_open(const hw_module_t* module, const char* name, |
| 1299 | hw_device_t** device) |
| 1300 | { |
| 1301 | ALOGI("adev_open(name=%s)", name); |
| 1302 | struct submix_audio_device *rsxadev; |
| 1303 | |
| 1304 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) |
| 1305 | return -EINVAL; |
| 1306 | |
| 1307 | rsxadev = (submix_audio_device*) calloc(1, sizeof(struct submix_audio_device)); |
| 1308 | if (!rsxadev) |
| 1309 | return -ENOMEM; |
| 1310 | |
| 1311 | rsxadev->device.common.tag = HARDWARE_DEVICE_TAG; |
Eric Laurent | 5d85c53 | 2012-09-10 10:36:09 -0700 | [diff] [blame] | 1312 | rsxadev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0; |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1313 | rsxadev->device.common.module = (struct hw_module_t *) module; |
| 1314 | rsxadev->device.common.close = adev_close; |
| 1315 | |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1316 | rsxadev->device.init_check = adev_init_check; |
| 1317 | rsxadev->device.set_voice_volume = adev_set_voice_volume; |
| 1318 | rsxadev->device.set_master_volume = adev_set_master_volume; |
| 1319 | rsxadev->device.get_master_volume = adev_get_master_volume; |
| 1320 | rsxadev->device.set_master_mute = adev_set_master_mute; |
| 1321 | rsxadev->device.get_master_mute = adev_get_master_mute; |
| 1322 | rsxadev->device.set_mode = adev_set_mode; |
| 1323 | rsxadev->device.set_mic_mute = adev_set_mic_mute; |
| 1324 | rsxadev->device.get_mic_mute = adev_get_mic_mute; |
| 1325 | rsxadev->device.set_parameters = adev_set_parameters; |
| 1326 | rsxadev->device.get_parameters = adev_get_parameters; |
| 1327 | rsxadev->device.get_input_buffer_size = adev_get_input_buffer_size; |
| 1328 | rsxadev->device.open_output_stream = adev_open_output_stream; |
| 1329 | rsxadev->device.close_output_stream = adev_close_output_stream; |
| 1330 | rsxadev->device.open_input_stream = adev_open_input_stream; |
| 1331 | rsxadev->device.close_input_stream = adev_close_input_stream; |
| 1332 | rsxadev->device.dump = adev_dump; |
| 1333 | |
Jean-Michel Trivi | eec8770 | 2012-09-17 09:59:42 -0700 | [diff] [blame] | 1334 | rsxadev->input_standby = true; |
| 1335 | rsxadev->output_standby = true; |
| 1336 | |
Jean-Michel Trivi | 88b79cb | 2012-08-16 13:56:03 -0700 | [diff] [blame] | 1337 | *device = &rsxadev->device.common; |
| 1338 | |
| 1339 | return 0; |
| 1340 | } |
| 1341 | |
| 1342 | static struct hw_module_methods_t hal_module_methods = { |
| 1343 | /* open */ adev_open, |
| 1344 | }; |
| 1345 | |
| 1346 | struct audio_module HAL_MODULE_INFO_SYM = { |
| 1347 | /* common */ { |
| 1348 | /* tag */ HARDWARE_MODULE_TAG, |
| 1349 | /* module_api_version */ AUDIO_MODULE_API_VERSION_0_1, |
| 1350 | /* hal_api_version */ HARDWARE_HAL_API_VERSION, |
| 1351 | /* id */ AUDIO_HARDWARE_MODULE_ID, |
| 1352 | /* name */ "Wifi Display audio HAL", |
| 1353 | /* author */ "The Android Open Source Project", |
| 1354 | /* methods */ &hal_module_methods, |
| 1355 | /* dso */ NULL, |
| 1356 | /* reserved */ { 0 }, |
| 1357 | }, |
| 1358 | }; |
| 1359 | |
| 1360 | } //namespace android |
| 1361 | |
| 1362 | } //extern "C" |