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