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