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