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