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