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