blob: 60a064943c50c5d64ceaeada1097d8ea28b72cbd [file] [log] [blame]
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "r_submix"
Jean-Michel Trivi35a2c162012-09-17 10:13:26 -070018//#define LOG_NDEBUG 0
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070019
20#include <errno.h>
21#include <pthread.h>
22#include <stdint.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070023#include <stdlib.h>
Stewart Milesc049a0a2014-05-01 09:03:27 -070024#include <sys/param.h>
25#include <sys/time.h>
Stewart Milese54c12c2014-05-01 09:03:27 -070026#include <sys/limits.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070027
28#include <cutils/log.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070029#include <cutils/properties.h>
Stewart Milesc049a0a2014-05-01 09:03:27 -070030#include <cutils/str_parms.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070031
Stewart Milesc049a0a2014-05-01 09:03:27 -070032#include <hardware/audio.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070033#include <hardware/hardware.h>
34#include <system/audio.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070035
Stewart Milesc049a0a2014-05-01 09:03:27 -070036#include <media/AudioParameter.h>
37#include <media/AudioBufferProvider.h>
Jean-Michel Trivieec87702012-09-17 09:59:42 -070038#include <media/nbaio/MonoPipe.h>
39#include <media/nbaio/MonoPipeReader.h>
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070040
Jean-Michel Trivid4413032012-09-30 11:08:06 -070041#include <utils/String8.h>
Jean-Michel Trivid4413032012-09-30 11:08:06 -070042
Stewart Miles92854f52014-05-01 09:03:27 -070043#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
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -070050extern "C" {
51
52namespace android {
53
Stewart Milesc049a0a2014-05-01 09:03:27 -070054// 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
Stewart Miles3dd36f92014-05-01 09:03:27 -070064// 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
Jean-Michel Trivieec87702012-09-17 09:59:42 -070070// 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
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -070074#define MAX_READ_ATTEMPTS 3
Jean-Michel Trivieec87702012-09-17 09:59:42 -070075#define READ_ATTEMPT_SLEEP_MS 5 // 5ms between two read attempts when pipe is empty
Stewart Miles568e66f2014-05-01 09:03:27 -070076#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
Stewart Miles3dd36f92014-05-01 09:03:27 -070079// 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
Stewart Milese54c12c2014-05-01 09:03:27 -070085// Whether channel conversion (16-bit signed PCM mono->stereo, stereo->mono) is enabled.
86#define ENABLE_CHANNEL_CONVERSION 1
Stewart Miles92854f52014-05-01 09:03:27 -070087#if LOG_STREAMS_TO_FILES
88// Folder to save stream log files to.
89#define LOG_STREAM_FOLDER "/data/misc/media"
90// Log filenames for input and output streams.
91#define LOG_STREAM_OUT_FILENAME LOG_STREAM_FOLDER "/r_submix_out.raw"
92#define LOG_STREAM_IN_FILENAME LOG_STREAM_FOLDER "/r_submix_in.raw"
93// File permissions for stream log files.
94#define LOG_STREAM_FILE_PERMISSIONS (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
95#endif // LOG_STREAMS_TO_FILES
Stewart Miles3dd36f92014-05-01 09:03:27 -070096
97// Common limits macros.
98#ifndef min
99#define min(a, b) ((a) < (b) ? (a) : (b))
100#endif // min
Stewart Milese54c12c2014-05-01 09:03:27 -0700101#ifndef max
102#define max(a, b) ((a) > (b) ? (a) : (b))
103#endif // max
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700104
Stewart Miles70726842014-05-01 09:03:27 -0700105// Set *result_variable_ptr to true if value_to_find is present in the array array_to_search,
106// otherwise set *result_variable_ptr to false.
107#define SUBMIX_VALUE_IN_SET(value_to_find, array_to_search, result_variable_ptr) \
108 { \
109 size_t i; \
110 *(result_variable_ptr) = false; \
111 for (i = 0; i < sizeof(array_to_search) / sizeof((array_to_search)[0]); i++) { \
112 if ((value_to_find) == (array_to_search)[i]) { \
113 *(result_variable_ptr) = true; \
114 break; \
115 } \
116 } \
117 }
118
Stewart Miles568e66f2014-05-01 09:03:27 -0700119// Configuration of the submix pipe.
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700120struct submix_config {
Stewart Miles70726842014-05-01 09:03:27 -0700121 // Channel mask field in this data structure is set to either input_channel_mask or
122 // output_channel_mask depending upon the last stream to be opened on this device.
123 struct audio_config common;
124 // Input stream and output stream channel masks. This is required since input and output
125 // channel bitfields are not equivalent.
126 audio_channel_mask_t input_channel_mask;
127 audio_channel_mask_t output_channel_mask;
Stewart Milese54c12c2014-05-01 09:03:27 -0700128 size_t pipe_frame_size; // Number of bytes in each audio frame in the pipe.
Stewart Miles3dd36f92014-05-01 09:03:27 -0700129 size_t buffer_size_frames; // Size of the audio pipe in frames.
130 // Maximum number of frames buffered by the input and output streams.
131 size_t buffer_period_size_frames;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700132};
133
134struct submix_audio_device {
135 struct audio_hw_device device;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700136 bool input_standby;
Stewart Miles70726842014-05-01 09:03:27 -0700137 bool output_standby;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700138 submix_config config;
139 // Pipe variables: they handle the ring buffer that "pipes" audio:
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700140 // - from the submix virtual audio output == what needs to be played
141 // remotely, seen as an output for AudioFlinger
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700142 // - to the virtual audio source == what is captured by the component
143 // which "records" the submix / virtual audio source, and handles it as needed.
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700144 // A usecase example is one where the component capturing the audio is then sending it over
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700145 // Wifi for presentation on a remote Wifi Display device (e.g. a dongle attached to a TV, or a
146 // TV with Wifi Display capabilities), or to a wireless audio player.
Stewart Miles568e66f2014-05-01 09:03:27 -0700147 sp<MonoPipe> rsxSink;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700148 sp<MonoPipeReader> rsxSource;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700149
Stewart Miles3dd36f92014-05-01 09:03:27 -0700150 // Pointers to the current input and output stream instances. rsxSink and rsxSource are
151 // destroyed if both and input and output streams are destroyed.
152 struct submix_stream_out *output;
153 struct submix_stream_in *input;
154
Stewart Miles568e66f2014-05-01 09:03:27 -0700155 // Device lock, also used to protect access to submix_audio_device from the input and output
156 // streams.
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700157 pthread_mutex_t lock;
158};
159
160struct submix_stream_out {
161 struct audio_stream_out stream;
162 struct submix_audio_device *dev;
Stewart Miles92854f52014-05-01 09:03:27 -0700163#if LOG_STREAMS_TO_FILES
164 int log_fd;
165#endif // LOG_STREAMS_TO_FILES
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700166};
167
168struct submix_stream_in {
169 struct audio_stream_in stream;
170 struct submix_audio_device *dev;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700171 bool output_standby; // output standby state as seen from record thread
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700172
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700173 // wall clock when recording starts
174 struct timespec record_start_time;
175 // how many frames have been requested to be read
176 int64_t read_counter_frames;
Stewart Miles3dd36f92014-05-01 09:03:27 -0700177
178#if ENABLE_LEGACY_INPUT_OPEN
179 // Number of references to this input stream.
180 volatile int32_t ref_count;
181#endif // ENABLE_LEGACY_INPUT_OPEN
Stewart Miles92854f52014-05-01 09:03:27 -0700182#if LOG_STREAMS_TO_FILES
183 int log_fd;
184#endif // LOG_STREAMS_TO_FILES
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700185};
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700186
Stewart Miles70726842014-05-01 09:03:27 -0700187// Determine whether the specified sample rate is supported by the submix module.
188static bool sample_rate_supported(const uint32_t sample_rate)
189{
190 // Set of sample rates supported by Format_from_SR_C() frameworks/av/media/libnbaio/NAIO.cpp.
191 static const unsigned int supported_sample_rates[] = {
192 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
193 };
194 bool return_value;
195 SUBMIX_VALUE_IN_SET(sample_rate, supported_sample_rates, &return_value);
196 return return_value;
197}
198
199// Determine whether the specified sample rate is supported, if it is return the specified sample
200// rate, otherwise return the default sample rate for the submix module.
201static uint32_t get_supported_sample_rate(uint32_t sample_rate)
202{
203 return sample_rate_supported(sample_rate) ? sample_rate : DEFAULT_SAMPLE_RATE_HZ;
204}
205
206// Determine whether the specified channel in mask is supported by the submix module.
207static bool channel_in_mask_supported(const audio_channel_mask_t channel_in_mask)
208{
209 // Set of channel in masks supported by Format_from_SR_C()
210 // frameworks/av/media/libnbaio/NAIO.cpp.
211 static const audio_channel_mask_t supported_channel_in_masks[] = {
212 AUDIO_CHANNEL_IN_MONO, AUDIO_CHANNEL_IN_STEREO,
213 };
214 bool return_value;
215 SUBMIX_VALUE_IN_SET(channel_in_mask, supported_channel_in_masks, &return_value);
216 return return_value;
217}
218
219// Determine whether the specified channel in mask is supported, if it is return the specified
220// channel in mask, otherwise return the default channel in mask for the submix module.
221static audio_channel_mask_t get_supported_channel_in_mask(
222 const audio_channel_mask_t channel_in_mask)
223{
224 return channel_in_mask_supported(channel_in_mask) ? channel_in_mask :
225 static_cast<audio_channel_mask_t>(AUDIO_CHANNEL_IN_STEREO);
226}
227
228// Determine whether the specified channel out mask is supported by the submix module.
229static bool channel_out_mask_supported(const audio_channel_mask_t channel_out_mask)
230{
231 // Set of channel out masks supported by Format_from_SR_C()
232 // frameworks/av/media/libnbaio/NAIO.cpp.
233 static const audio_channel_mask_t supported_channel_out_masks[] = {
234 AUDIO_CHANNEL_OUT_MONO, AUDIO_CHANNEL_OUT_STEREO,
235 };
236 bool return_value;
237 SUBMIX_VALUE_IN_SET(channel_out_mask, supported_channel_out_masks, &return_value);
238 return return_value;
239}
240
241// Determine whether the specified channel out mask is supported, if it is return the specified
242// channel out mask, otherwise return the default channel out mask for the submix module.
243static audio_channel_mask_t get_supported_channel_out_mask(
244 const audio_channel_mask_t channel_out_mask)
245{
246 return channel_out_mask_supported(channel_out_mask) ? channel_out_mask :
247 static_cast<audio_channel_mask_t>(AUDIO_CHANNEL_OUT_STEREO);
248}
249
Stewart Milesf645c5e2014-05-01 09:03:27 -0700250// Get a pointer to submix_stream_out given an audio_stream_out that is embedded within the
251// structure.
252static struct submix_stream_out * audio_stream_out_get_submix_stream_out(
253 struct audio_stream_out * const stream)
254{
255 ALOG_ASSERT(stream);
256 return reinterpret_cast<struct submix_stream_out *>(reinterpret_cast<uint8_t *>(stream) -
257 offsetof(struct submix_stream_out, stream));
258}
259
260// Get a pointer to submix_stream_out given an audio_stream that is embedded within the structure.
261static struct submix_stream_out * audio_stream_get_submix_stream_out(
262 struct audio_stream * const stream)
263{
264 ALOG_ASSERT(stream);
265 return audio_stream_out_get_submix_stream_out(
266 reinterpret_cast<struct audio_stream_out *>(stream));
267}
268
269// Get a pointer to submix_stream_in given an audio_stream_in that is embedded within the
270// structure.
271static struct submix_stream_in * audio_stream_in_get_submix_stream_in(
272 struct audio_stream_in * const stream)
273{
274 ALOG_ASSERT(stream);
275 return reinterpret_cast<struct submix_stream_in *>(reinterpret_cast<uint8_t *>(stream) -
276 offsetof(struct submix_stream_in, stream));
277}
278
279// Get a pointer to submix_stream_in given an audio_stream that is embedded within the structure.
280static struct submix_stream_in * audio_stream_get_submix_stream_in(
281 struct audio_stream * const stream)
282{
283 ALOG_ASSERT(stream);
284 return audio_stream_in_get_submix_stream_in(
285 reinterpret_cast<struct audio_stream_in *>(stream));
286}
287
288// Get a pointer to submix_audio_device given a pointer to an audio_device that is embedded within
289// the structure.
290static struct submix_audio_device * audio_hw_device_get_submix_audio_device(
291 struct audio_hw_device *device)
292{
293 ALOG_ASSERT(device);
294 return reinterpret_cast<struct submix_audio_device *>(reinterpret_cast<uint8_t *>(device) -
295 offsetof(struct submix_audio_device, device));
296}
297
Stewart Miles568e66f2014-05-01 09:03:27 -0700298// Get the number of channels referenced by the specified channel_mask. The channel_mask can
299// reference either input or output channels.
300uint32_t get_channel_count_from_mask(const audio_channel_mask_t channel_mask) {
301 if (audio_is_input_channel(channel_mask)) {
302 return popcount(channel_mask & AUDIO_CHANNEL_IN_ALL);
303 } else if (audio_is_output_channel(channel_mask)) {
304 return popcount(channel_mask & AUDIO_CHANNEL_OUT_ALL);
305 }
306 ALOGE("get_channel_count(): No channels specified in channel mask %x", channel_mask);
307 return 0;
308}
309
Stewart Miles70726842014-05-01 09:03:27 -0700310// Compare an audio_config with input channel mask and an audio_config with output channel mask
311// returning false if they do *not* match, true otherwise.
312static bool audio_config_compare(const audio_config * const input_config,
313 const audio_config * const output_config)
314{
Stewart Milese54c12c2014-05-01 09:03:27 -0700315#if !ENABLE_CHANNEL_CONVERSION
Stewart Miles3dd36f92014-05-01 09:03:27 -0700316 const uint32_t input_channels = get_channel_count_from_mask(input_config->channel_mask);
317 const uint32_t output_channels = get_channel_count_from_mask(output_config->channel_mask);
318 if (input_channels != output_channels) {
319 ALOGE("audio_config_compare() channel count mismatch input=%d vs. output=%d",
320 input_channels, output_channels);
Stewart Miles70726842014-05-01 09:03:27 -0700321 return false;
322 }
Stewart Milese54c12c2014-05-01 09:03:27 -0700323#endif // !ENABLE_CHANNEL_CONVERSION
Stewart Miles70726842014-05-01 09:03:27 -0700324 if (input_config->sample_rate != output_config->sample_rate) {
325 ALOGE("audio_config_compare() sample rate mismatch %ul vs. %ul",
326 input_config->sample_rate, output_config->sample_rate);
327 return false;
328 }
329 if (input_config->format != output_config->format) {
330 ALOGE("audio_config_compare() format mismatch %x vs. %x",
331 input_config->format, output_config->format);
332 return false;
333 }
334 // This purposely ignores offload_info as it's not required for the submix device.
335 return true;
336}
337
Stewart Miles3dd36f92014-05-01 09:03:27 -0700338// If one doesn't exist, create a pipe for the submix audio device rsxadev of size
339// buffer_size_frames and optionally associate "in" or "out" with the submix audio device.
340static void submix_audio_device_create_pipe(struct submix_audio_device * const rsxadev,
341 const struct audio_config * const config,
342 const size_t buffer_size_frames,
343 const uint32_t buffer_period_count,
344 struct submix_stream_in * const in,
345 struct submix_stream_out * const out)
346{
347 ALOG_ASSERT(in || out);
348 ALOGV("submix_audio_device_create_pipe()");
349 pthread_mutex_lock(&rsxadev->lock);
350 // Save a reference to the specified input or output stream and the associated channel
351 // mask.
352 if (in) {
353 rsxadev->input = in;
354 rsxadev->config.input_channel_mask = config->channel_mask;
355 }
356 if (out) {
357 rsxadev->output = out;
358 rsxadev->config.output_channel_mask = config->channel_mask;
359 }
360 // If a pipe isn't associated with the device, create one.
361 if (rsxadev->rsxSink == NULL || rsxadev->rsxSource == NULL) {
362 struct submix_config * const device_config = &rsxadev->config;
363 const NBAIO_Format format = Format_from_SR_C(config->sample_rate,
364 get_channel_count_from_mask(config->channel_mask), config->format);
365 const NBAIO_Format offers[1] = {format};
366 size_t numCounterOffers = 0;
367 // Create a MonoPipe with optional blocking set to true.
368 MonoPipe* sink = new MonoPipe(buffer_size_frames, format, true /*writeCanBlock*/);
369 // Negotiation between the source and sink cannot fail as the device open operation
370 // creates both ends of the pipe using the same audio format.
371 ssize_t index = sink->negotiate(offers, 1, NULL, numCounterOffers);
372 ALOG_ASSERT(index == 0);
373 MonoPipeReader* source = new MonoPipeReader(sink);
374 numCounterOffers = 0;
375 index = source->negotiate(offers, 1, NULL, numCounterOffers);
376 ALOG_ASSERT(index == 0);
377 ALOGV("submix_audio_device_create_pipe(): created pipe");
378
379 // Save references to the source and sink.
380 ALOG_ASSERT(rsxadev->rsxSink == NULL);
381 ALOG_ASSERT(rsxadev->rsxSource == NULL);
382 rsxadev->rsxSink = sink;
383 rsxadev->rsxSource = source;
384 // Store the sanitized audio format in the device so that it's possible to determine
385 // the format of the pipe source when opening the input device.
386 memcpy(&device_config->common, config, sizeof(device_config->common));
387 device_config->buffer_size_frames = sink->maxFrames();
388 device_config->buffer_period_size_frames = device_config->buffer_size_frames /
389 buffer_period_count;
Stewart Milese54c12c2014-05-01 09:03:27 -0700390 if (in) device_config->pipe_frame_size = audio_stream_frame_size(&in->stream.common);
391 if (out) device_config->pipe_frame_size = audio_stream_frame_size(&out->stream.common);
392 SUBMIX_ALOGV("submix_audio_device_create_pipe(): pipe frame size %zd, pipe size %zd, "
393 "period size %zd", device_config->pipe_frame_size,
394 device_config->buffer_size_frames, device_config->buffer_period_size_frames);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700395 }
396 pthread_mutex_unlock(&rsxadev->lock);
397}
398
399// Release references to the sink and source. Input and output threads may maintain references
400// to these objects via StrongPointer (sp<MonoPipe> and sp<MonoPipeReader>) which they can use
401// before they shutdown.
402static void submix_audio_device_release_pipe(struct submix_audio_device * const rsxadev)
403{
404 ALOGV("submix_audio_device_release_pipe()");
405 rsxadev->rsxSink.clear();
406 rsxadev->rsxSource.clear();
407}
408
409// Remove references to the specified input and output streams. When the device no longer
410// references input and output streams destroy the associated pipe.
411static void submix_audio_device_destroy_pipe(struct submix_audio_device * const rsxadev,
412 const struct submix_stream_in * const in,
413 const struct submix_stream_out * const out)
414{
415 MonoPipe* sink;
416 pthread_mutex_lock(&rsxadev->lock);
417 ALOGV("submix_audio_device_destroy_pipe()");
418 ALOG_ASSERT(in == NULL || rsxadev->input == in);
419 ALOG_ASSERT(out == NULL || rsxadev->output == out);
420 if (in != NULL) {
421#if ENABLE_LEGACY_INPUT_OPEN
422 const_cast<struct submix_stream_in*>(in)->ref_count--;
423 if (in->ref_count == 0) {
424 rsxadev->input = NULL;
425 }
426 ALOGV("submix_audio_device_destroy_pipe(): input ref_count %d", in->ref_count);
427#else
428 rsxadev->input = NULL;
429#endif // ENABLE_LEGACY_INPUT_OPEN
430 }
431 if (out != NULL) rsxadev->output = NULL;
432 if (rsxadev->input != NULL && rsxadev->output != NULL) {
433 submix_audio_device_release_pipe(rsxadev);
434 ALOGV("submix_audio_device_destroy_pipe(): pipe destroyed");
435 }
436 pthread_mutex_unlock(&rsxadev->lock);
437}
438
Stewart Miles70726842014-05-01 09:03:27 -0700439// Sanitize the user specified audio config for a submix input / output stream.
440static void submix_sanitize_config(struct audio_config * const config, const bool is_input_format)
441{
442 config->channel_mask = is_input_format ? get_supported_channel_in_mask(config->channel_mask) :
443 get_supported_channel_out_mask(config->channel_mask);
444 config->sample_rate = get_supported_sample_rate(config->sample_rate);
445 config->format = DEFAULT_FORMAT;
446}
447
448// Verify a submix input or output stream can be opened.
449static bool submix_open_validate(const struct submix_audio_device * const rsxadev,
450 pthread_mutex_t * const lock,
451 const struct audio_config * const config,
452 const bool opening_input)
453{
Stewart Miles3dd36f92014-05-01 09:03:27 -0700454 bool input_open;
455 bool output_open;
Stewart Miles70726842014-05-01 09:03:27 -0700456 audio_config pipe_config;
457
458 // Query the device for the current audio config and whether input and output streams are open.
459 pthread_mutex_lock(lock);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700460 output_open = rsxadev->output != NULL;
461 input_open = rsxadev->input != NULL;
Stewart Miles70726842014-05-01 09:03:27 -0700462 memcpy(&pipe_config, &rsxadev->config.common, sizeof(pipe_config));
463 pthread_mutex_unlock(lock);
464
Stewart Miles3dd36f92014-05-01 09:03:27 -0700465 // If the stream is already open, don't open it again.
466 if (opening_input ? !ENABLE_LEGACY_INPUT_OPEN && input_open : output_open) {
467 ALOGE("submix_open_validate(): %s stream already open.", opening_input ? "Input" :
468 "Output");
469 return false;
470 }
471
472 SUBMIX_ALOGV("submix_open_validate(): sample rate=%d format=%x "
473 "%s_channel_mask=%x", config->sample_rate, config->format,
474 opening_input ? "in" : "out", config->channel_mask);
475
476 // If either stream is open, verify the existing audio config the pipe matches the user
Stewart Miles70726842014-05-01 09:03:27 -0700477 // specified config.
Stewart Miles3dd36f92014-05-01 09:03:27 -0700478 if (input_open || output_open) {
Stewart Miles70726842014-05-01 09:03:27 -0700479 const audio_config * const input_config = opening_input ? config : &pipe_config;
480 const audio_config * const output_config = opening_input ? &pipe_config : config;
481 // Get the channel mask of the open device.
482 pipe_config.channel_mask =
483 opening_input ? rsxadev->config.output_channel_mask :
484 rsxadev->config.input_channel_mask;
485 if (!audio_config_compare(input_config, output_config)) {
486 ALOGE("submix_open_validate(): Unsupported format.");
Stewart Miles3dd36f92014-05-01 09:03:27 -0700487 return false;
Stewart Miles70726842014-05-01 09:03:27 -0700488 }
489 }
490 return true;
491}
492
Stewart Milese54c12c2014-05-01 09:03:27 -0700493// Calculate the maximum size of the pipe buffer in frames for the specified stream.
494static size_t calculate_stream_pipe_size_in_frames(const struct audio_stream *stream,
495 const struct submix_config *config,
496 const size_t pipe_frames)
497{
498 const size_t stream_frame_size = audio_stream_frame_size(stream);
499 const size_t pipe_frame_size = config->pipe_frame_size;
500 const size_t max_frame_size = max(stream_frame_size, pipe_frame_size);
501 return (pipe_frames * config->pipe_frame_size) / max_frame_size;
502}
503
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700504/* audio HAL functions */
505
506static uint32_t out_get_sample_rate(const struct audio_stream *stream)
507{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700508 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
509 const_cast<struct audio_stream *>(stream));
Stewart Miles70726842014-05-01 09:03:27 -0700510 const uint32_t out_rate = out->dev->config.common.sample_rate;
Stewart Milesc049a0a2014-05-01 09:03:27 -0700511 SUBMIX_ALOGV("out_get_sample_rate() returns %u", out_rate);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700512 return out_rate;
513}
514
515static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
516{
Stewart Miles70726842014-05-01 09:03:27 -0700517 if (!sample_rate_supported(rate)) {
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700518 ALOGE("out_set_sample_rate(rate=%u) rate unsupported", rate);
519 return -ENOSYS;
520 }
Stewart Milesf645c5e2014-05-01 09:03:27 -0700521 struct submix_stream_out * const out = audio_stream_get_submix_stream_out(stream);
Stewart Milesc049a0a2014-05-01 09:03:27 -0700522 SUBMIX_ALOGV("out_set_sample_rate(rate=%u)", rate);
Stewart Miles70726842014-05-01 09:03:27 -0700523 out->dev->config.common.sample_rate = rate;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700524 return 0;
525}
526
527static size_t out_get_buffer_size(const struct audio_stream *stream)
528{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700529 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
530 const_cast<struct audio_stream *>(stream));
Stewart Miles568e66f2014-05-01 09:03:27 -0700531 const struct submix_config * const config = &out->dev->config;
Stewart Milese54c12c2014-05-01 09:03:27 -0700532 const size_t buffer_size_frames = calculate_stream_pipe_size_in_frames(
533 stream, config, config->buffer_period_size_frames);
534 const size_t buffer_size_bytes = buffer_size_frames * audio_stream_frame_size(stream);
Stewart Miles568e66f2014-05-01 09:03:27 -0700535 SUBMIX_ALOGV("out_get_buffer_size() returns %zu bytes, %zu frames",
Stewart Milese54c12c2014-05-01 09:03:27 -0700536 buffer_size_bytes, buffer_size_frames);
537 return buffer_size_bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700538}
539
540static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
541{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700542 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
543 const_cast<struct audio_stream *>(stream));
Stewart Miles70726842014-05-01 09:03:27 -0700544 uint32_t channel_mask = out->dev->config.output_channel_mask;
Stewart Miles568e66f2014-05-01 09:03:27 -0700545 SUBMIX_ALOGV("out_get_channels() returns %08x", channel_mask);
546 return channel_mask;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700547}
548
549static audio_format_t out_get_format(const struct audio_stream *stream)
550{
Stewart Miles568e66f2014-05-01 09:03:27 -0700551 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
552 const_cast<struct audio_stream *>(stream));
Stewart Miles70726842014-05-01 09:03:27 -0700553 const audio_format_t format = out->dev->config.common.format;
Stewart Miles568e66f2014-05-01 09:03:27 -0700554 SUBMIX_ALOGV("out_get_format() returns %x", format);
555 return format;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700556}
557
558static int out_set_format(struct audio_stream *stream, audio_format_t format)
559{
Stewart Miles568e66f2014-05-01 09:03:27 -0700560 const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(stream);
Stewart Miles70726842014-05-01 09:03:27 -0700561 if (format != out->dev->config.common.format) {
Stewart Milesc049a0a2014-05-01 09:03:27 -0700562 ALOGE("out_set_format(format=%x) format unsupported", format);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700563 return -ENOSYS;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700564 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700565 SUBMIX_ALOGV("out_set_format(format=%x)", format);
566 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700567}
568
569static int out_standby(struct audio_stream *stream)
570{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700571 struct submix_audio_device * const rsxadev = audio_stream_get_submix_stream_out(stream)->dev;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700572 ALOGI("out_standby()");
573
Stewart Milesf645c5e2014-05-01 09:03:27 -0700574 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700575
Stewart Milesf645c5e2014-05-01 09:03:27 -0700576 rsxadev->output_standby = true;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700577
Stewart Milesf645c5e2014-05-01 09:03:27 -0700578 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700579
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700580 return 0;
581}
582
583static int out_dump(const struct audio_stream *stream, int fd)
584{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700585 (void)stream;
586 (void)fd;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700587 return 0;
588}
589
590static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
591{
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700592 int exiting = -1;
593 AudioParameter parms = AudioParameter(String8(kvpairs));
Stewart Milesc049a0a2014-05-01 09:03:27 -0700594 SUBMIX_ALOGV("out_set_parameters() kvpairs='%s'", kvpairs);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700595
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700596 // FIXME this is using hard-coded strings but in the future, this functionality will be
597 // converted to use audio HAL extensions required to support tunneling
598 if ((parms.getInt(String8("exiting"), exiting) == NO_ERROR) && (exiting > 0)) {
Stewart Milesf645c5e2014-05-01 09:03:27 -0700599 struct submix_audio_device * const rsxadev =
600 audio_stream_get_submix_stream_out(stream)->dev;
601 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800602 { // using the sink
Stewart Miles3dd36f92014-05-01 09:03:27 -0700603 sp<MonoPipe> sink = rsxadev->rsxSink;
Stewart Milesf645c5e2014-05-01 09:03:27 -0700604 if (sink == NULL) {
605 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800606 return 0;
607 }
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700608
Stewart Milesc049a0a2014-05-01 09:03:27 -0700609 ALOGI("out_set_parameters(): shutdown");
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800610 sink->shutdown(true);
611 } // done using the sink
Stewart Milesf645c5e2014-05-01 09:03:27 -0700612 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivid4413032012-09-30 11:08:06 -0700613 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700614 return 0;
615}
616
617static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
618{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700619 (void)stream;
620 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700621 return strdup("");
622}
623
624static uint32_t out_get_latency(const struct audio_stream_out *stream)
625{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700626 const struct submix_stream_out * const out = audio_stream_out_get_submix_stream_out(
627 const_cast<struct audio_stream_out *>(stream));
Stewart Miles568e66f2014-05-01 09:03:27 -0700628 const struct submix_config * const config = &out->dev->config;
Stewart Milese54c12c2014-05-01 09:03:27 -0700629 const size_t buffer_size_frames = calculate_stream_pipe_size_in_frames(
630 &stream->common, config, config->buffer_size_frames);
631 const uint32_t latency_ms = (buffer_size_frames * 1000) / config->common.sample_rate;
632 SUBMIX_ALOGV("out_get_latency() returns %u ms, size in frames %zu, sample rate %u",
633 latency_ms, buffer_size_frames, config->common.sample_rate);
Stewart Miles568e66f2014-05-01 09:03:27 -0700634 return latency_ms;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700635}
636
637static int out_set_volume(struct audio_stream_out *stream, float left,
638 float right)
639{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700640 (void)stream;
641 (void)left;
642 (void)right;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700643 return -ENOSYS;
644}
645
646static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
647 size_t bytes)
648{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700649 SUBMIX_ALOGV("out_write(bytes=%zd)", bytes);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700650 ssize_t written_frames = 0;
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700651 const size_t frame_size = audio_stream_frame_size(&stream->common);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700652 struct submix_stream_out * const out = audio_stream_out_get_submix_stream_out(stream);
653 struct submix_audio_device * const rsxadev = out->dev;
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700654 const size_t frames = bytes / frame_size;
655
Stewart Milesf645c5e2014-05-01 09:03:27 -0700656 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700657
Stewart Milesf645c5e2014-05-01 09:03:27 -0700658 rsxadev->output_standby = false;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700659
Stewart Miles3dd36f92014-05-01 09:03:27 -0700660 sp<MonoPipe> sink = rsxadev->rsxSink;
Stewart Milesf645c5e2014-05-01 09:03:27 -0700661 if (sink != NULL) {
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700662 if (sink->isShutdown()) {
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800663 sink.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -0700664 pthread_mutex_unlock(&rsxadev->lock);
Stewart Milesc049a0a2014-05-01 09:03:27 -0700665 SUBMIX_ALOGV("out_write(): pipe shutdown, ignoring the write.");
Jean-Michel Trivi90b0fbd2012-10-30 19:03:22 -0700666 // the pipe has already been shutdown, this buffer will be lost but we must
667 // simulate timing so we don't drain the output faster than realtime
668 usleep(frames * 1000000 / out_get_sample_rate(&stream->common));
669 return bytes;
670 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700671 } else {
Stewart Milesf645c5e2014-05-01 09:03:27 -0700672 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700673 ALOGE("out_write without a pipe!");
674 ALOG_ASSERT("out_write without a pipe!");
675 return 0;
676 }
677
Stewart Miles2d199fe2014-05-01 09:03:27 -0700678 // If the write to the sink would block when no input stream is present, flush enough frames
679 // from the pipe to make space to write the most recent data.
680 {
681 const size_t availableToWrite = sink->availableToWrite();
682 sp<MonoPipeReader> source = rsxadev->rsxSource;
683 if (rsxadev->input == NULL && availableToWrite < frames) {
684 static uint8_t flush_buffer[64];
685 const size_t flushBufferSizeFrames = sizeof(flush_buffer) / frame_size;
686 size_t frames_to_flush_from_source = frames - availableToWrite;
687 SUBMIX_ALOGV("out_write(): flushing %d frames from the pipe to avoid blocking",
688 frames_to_flush_from_source);
689 while (frames_to_flush_from_source) {
690 const size_t flush_size = min(frames_to_flush_from_source, flushBufferSizeFrames);
691 frames_to_flush_from_source -= flush_size;
692 source->read(flush_buffer, flush_size, AudioBufferProvider::kInvalidPTS);
693 }
694 }
695 }
696
Stewart Milesf645c5e2014-05-01 09:03:27 -0700697 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700698
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700699 written_frames = sink->write(buffer, frames);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800700
Stewart Miles92854f52014-05-01 09:03:27 -0700701#if LOG_STREAMS_TO_FILES
702 if (out->log_fd >= 0) write(out->log_fd, buffer, written_frames * frame_size);
703#endif // LOG_STREAMS_TO_FILES
704
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700705 if (written_frames < 0) {
706 if (written_frames == (ssize_t)NEGOTIATE) {
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700707 ALOGE("out_write() write to pipe returned NEGOTIATE");
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700708
Stewart Milesf645c5e2014-05-01 09:03:27 -0700709 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800710 sink.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -0700711 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700712
713 written_frames = 0;
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700714 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700715 } else {
716 // write() returned UNDERRUN or WOULD_BLOCK, retry
Colin Cross5685a082014-04-18 15:45:42 -0700717 ALOGE("out_write() write to pipe returned unexpected %zd", written_frames);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700718 written_frames = sink->write(buffer, frames);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700719 }
720 }
721
Stewart Milesf645c5e2014-05-01 09:03:27 -0700722 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800723 sink.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -0700724 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700725
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700726 if (written_frames < 0) {
Colin Cross5685a082014-04-18 15:45:42 -0700727 ALOGE("out_write() failed writing to pipe with %zd", written_frames);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700728 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700729 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700730 const ssize_t written_bytes = written_frames * frame_size;
731 SUBMIX_ALOGV("out_write() wrote %zd bytes %zd frames)", written_bytes, written_frames);
732 return written_bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700733}
734
735static int out_get_render_position(const struct audio_stream_out *stream,
736 uint32_t *dsp_frames)
737{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700738 (void)stream;
739 (void)dsp_frames;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700740 return -EINVAL;
741}
742
743static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
744{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700745 (void)stream;
746 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700747 return 0;
748}
749
750static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
751{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700752 (void)stream;
753 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700754 return 0;
755}
756
757static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
758 int64_t *timestamp)
759{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700760 (void)stream;
761 (void)timestamp;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700762 return -EINVAL;
763}
764
765/** audio_stream_in implementation **/
766static uint32_t in_get_sample_rate(const struct audio_stream *stream)
767{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700768 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
769 const_cast<struct audio_stream*>(stream));
Stewart Miles70726842014-05-01 09:03:27 -0700770 SUBMIX_ALOGV("in_get_sample_rate() returns %u", in->dev->config.common.sample_rate);
771 return in->dev->config.common.sample_rate;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700772}
773
774static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
775{
Stewart Miles568e66f2014-05-01 09:03:27 -0700776 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(stream);
Stewart Miles70726842014-05-01 09:03:27 -0700777 if (!sample_rate_supported(rate)) {
778 ALOGE("in_set_sample_rate(rate=%u) rate unsupported", rate);
779 return -ENOSYS;
780 }
781 in->dev->config.common.sample_rate = rate;
Stewart Miles568e66f2014-05-01 09:03:27 -0700782 SUBMIX_ALOGV("in_set_sample_rate() set %u", rate);
783 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700784}
785
786static size_t in_get_buffer_size(const struct audio_stream *stream)
787{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700788 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
789 const_cast<struct audio_stream*>(stream));
Stewart Milese54c12c2014-05-01 09:03:27 -0700790 const struct submix_config * const config = &in->dev->config;
791 const size_t buffer_size_frames = calculate_stream_pipe_size_in_frames(
792 stream, config, config->buffer_period_size_frames);
793 const size_t buffer_size_bytes = buffer_size_frames * audio_stream_frame_size(stream);
794 SUBMIX_ALOGV("in_get_buffer_size() returns %zu bytes, %zu frames", buffer_size_bytes,
795 buffer_size_frames);
796 return buffer_size_bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700797}
798
799static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
800{
Stewart Miles70726842014-05-01 09:03:27 -0700801 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
802 const_cast<struct audio_stream*>(stream));
803 const audio_channel_mask_t channel_mask = in->dev->config.input_channel_mask;
804 SUBMIX_ALOGV("in_get_channels() returns %x", channel_mask);
805 return channel_mask;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700806}
807
808static audio_format_t in_get_format(const struct audio_stream *stream)
809{
Stewart Miles568e66f2014-05-01 09:03:27 -0700810 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
Stewart Miles70726842014-05-01 09:03:27 -0700811 const_cast<struct audio_stream*>(stream));
812 const audio_format_t format = in->dev->config.common.format;
Stewart Miles568e66f2014-05-01 09:03:27 -0700813 SUBMIX_ALOGV("in_get_format() returns %x", format);
814 return format;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700815}
816
817static int in_set_format(struct audio_stream *stream, audio_format_t format)
818{
Stewart Miles568e66f2014-05-01 09:03:27 -0700819 const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(stream);
Stewart Miles70726842014-05-01 09:03:27 -0700820 if (format != in->dev->config.common.format) {
Stewart Milesc049a0a2014-05-01 09:03:27 -0700821 ALOGE("in_set_format(format=%x) format unsupported", format);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700822 return -ENOSYS;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700823 }
Stewart Milesc049a0a2014-05-01 09:03:27 -0700824 SUBMIX_ALOGV("in_set_format(format=%x)", format);
825 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700826}
827
828static int in_standby(struct audio_stream *stream)
829{
Stewart Milesf645c5e2014-05-01 09:03:27 -0700830 struct submix_audio_device * const rsxadev = audio_stream_get_submix_stream_in(stream)->dev;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700831 ALOGI("in_standby()");
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700832
Stewart Milesf645c5e2014-05-01 09:03:27 -0700833 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700834
Stewart Milesf645c5e2014-05-01 09:03:27 -0700835 rsxadev->input_standby = true;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700836
Stewart Milesf645c5e2014-05-01 09:03:27 -0700837 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700838
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700839 return 0;
840}
841
842static int in_dump(const struct audio_stream *stream, int fd)
843{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700844 (void)stream;
845 (void)fd;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700846 return 0;
847}
848
849static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
850{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700851 (void)stream;
852 (void)kvpairs;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700853 return 0;
854}
855
856static char * in_get_parameters(const struct audio_stream *stream,
857 const char *keys)
858{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700859 (void)stream;
860 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700861 return strdup("");
862}
863
864static int in_set_gain(struct audio_stream_in *stream, float gain)
865{
Stewart Milesc049a0a2014-05-01 09:03:27 -0700866 (void)stream;
867 (void)gain;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700868 return 0;
869}
870
871static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
872 size_t bytes)
873{
874 ssize_t frames_read = -1977;
Stewart Milesf645c5e2014-05-01 09:03:27 -0700875 struct submix_stream_in * const in = audio_stream_in_get_submix_stream_in(stream);
876 struct submix_audio_device * const rsxadev = in->dev;
Stewart Milese54c12c2014-05-01 09:03:27 -0700877 struct audio_config *format;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700878 const size_t frame_size = audio_stream_frame_size(&stream->common);
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700879 const size_t frames_to_read = bytes / frame_size;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700880
Stewart Milesc049a0a2014-05-01 09:03:27 -0700881 SUBMIX_ALOGV("in_read bytes=%zu", bytes);
Stewart Milesf645c5e2014-05-01 09:03:27 -0700882 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700883
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700884 const bool output_standby_transition = (in->output_standby != in->dev->output_standby);
Stewart Milesf645c5e2014-05-01 09:03:27 -0700885 in->output_standby = rsxadev->output_standby;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700886
Stewart Milesf645c5e2014-05-01 09:03:27 -0700887 if (rsxadev->input_standby || output_standby_transition) {
888 rsxadev->input_standby = false;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700889 // keep track of when we exit input standby (== first read == start "real recording")
890 // or when we start recording silence, and reset projected time
891 int rc = clock_gettime(CLOCK_MONOTONIC, &in->record_start_time);
892 if (rc == 0) {
893 in->read_counter_frames = 0;
894 }
895 }
896
897 in->read_counter_frames += frames_to_read;
Jean-Michel Trivieec87702012-09-17 09:59:42 -0700898 size_t remaining_frames = frames_to_read;
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800899
900 {
901 // about to read from audio source
Stewart Milesf645c5e2014-05-01 09:03:27 -0700902 sp<MonoPipeReader> source = rsxadev->rsxSource;
903 if (source == NULL) {
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800904 ALOGE("no audio pipe yet we're trying to read!");
Stewart Milesf645c5e2014-05-01 09:03:27 -0700905 pthread_mutex_unlock(&rsxadev->lock);
Stewart Miles3dd36f92014-05-01 09:03:27 -0700906 usleep(frames_to_read * 1000000 / in_get_sample_rate(&stream->common));
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800907 memset(buffer, 0, bytes);
908 return bytes;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700909 }
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800910
Stewart Milesf645c5e2014-05-01 09:03:27 -0700911 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800912
913 // read the data from the pipe (it's non blocking)
914 int attempts = 0;
915 char* buff = (char*)buffer;
Stewart Milese54c12c2014-05-01 09:03:27 -0700916#if ENABLE_CHANNEL_CONVERSION
917 // Determine whether channel conversion is required.
918 const uint32_t input_channels = get_channel_count_from_mask(
919 rsxadev->config.input_channel_mask);
920 const uint32_t output_channels = get_channel_count_from_mask(
921 rsxadev->config.output_channel_mask);
922 if (input_channels != output_channels) {
923 SUBMIX_ALOGV("in_read(): %d output channels will be converted to %d "
924 "input channels", output_channels, input_channels);
925 // Only support 16-bit PCM channel conversion from mono to stereo or stereo to mono.
926 ALOG_ASSERT(rsxadev->config.common.format == AUDIO_FORMAT_PCM_16_BIT);
927 ALOG_ASSERT((input_channels == 1 && output_channels == 2) ||
928 (input_channels == 2 && output_channels == 1));
929 }
930#endif // ENABLE_CHANNEL_CONVERSION
931
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800932 while ((remaining_frames > 0) && (attempts < MAX_READ_ATTEMPTS)) {
Stewart Milese54c12c2014-05-01 09:03:27 -0700933 size_t read_frames = remaining_frames;
934#if ENABLE_CHANNEL_CONVERSION
935 if (output_channels == 1 && input_channels == 2) {
936 // Need to read half the requested frames since the converted output
937 // data will take twice the space (mono->stereo).
938 read_frames /= 2;
939 }
940#endif // ENABLE_CHANNEL_CONVERSION
941
942 SUBMIX_ALOGV("in_read(): frames available to read %zd", source->availableToRead());
943
944 frames_read = source->read(buff, read_frames, AudioBufferProvider::kInvalidPTS);
945
946 SUBMIX_ALOGV("in_read(): frames read %zd", frames_read);
947
948#if ENABLE_CHANNEL_CONVERSION
949 // Perform in-place channel conversion.
950 // NOTE: In the following "input stream" refers to the data returned by this function
951 // and "output stream" refers to the data read from the pipe.
952 if (input_channels != output_channels && frames_read > 0) {
953 int16_t *data = (int16_t*)buff;
954 if (output_channels == 2 && input_channels == 1) {
955 // Offset into the output stream data in samples.
956 ssize_t output_stream_offset = 0;
957 for (ssize_t input_stream_frame = 0; input_stream_frame < frames_read;
958 input_stream_frame++, output_stream_offset += 2) {
959 // Average the content from both channels.
960 data[input_stream_frame] = ((int32_t)data[output_stream_offset] +
961 (int32_t)data[output_stream_offset + 1]) / 2;
962 }
963 } else if (output_channels == 1 && input_channels == 2) {
964 // Offset into the input stream data in samples.
965 ssize_t input_stream_offset = (frames_read - 1) * 2;
966 for (ssize_t output_stream_frame = frames_read - 1; output_stream_frame >= 0;
967 output_stream_frame--, input_stream_offset -= 2) {
968 const short sample = data[output_stream_frame];
969 data[input_stream_offset] = sample;
970 data[input_stream_offset + 1] = sample;
971 }
972 }
973 }
974#endif // ENABLE_CHANNEL_CONVERSION
Stewart Miles3dd36f92014-05-01 09:03:27 -0700975
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800976 if (frames_read > 0) {
Stewart Miles92854f52014-05-01 09:03:27 -0700977#if LOG_STREAMS_TO_FILES
978 if (in->log_fd >= 0) write(in->log_fd, buff, frames_read * frame_size);
979#endif // LOG_STREAMS_TO_FILES
980
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800981 remaining_frames -= frames_read;
982 buff += frames_read * frame_size;
Stewart Milesc049a0a2014-05-01 09:03:27 -0700983 SUBMIX_ALOGV(" in_read (att=%d) got %zd frames, remaining=%zu",
984 attempts, frames_read, remaining_frames);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800985 } else {
Stewart Miles3dd36f92014-05-01 09:03:27 -0700986 attempts++;
Stewart Milesc049a0a2014-05-01 09:03:27 -0700987 SUBMIX_ALOGE(" in_read read returned %zd", frames_read);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800988 usleep(READ_ATTEMPT_SLEEP_MS * 1000);
989 }
990 }
991 // done using the source
Stewart Milesf645c5e2014-05-01 09:03:27 -0700992 pthread_mutex_lock(&rsxadev->lock);
Jean-Michel Trivieafbfa42012-12-18 11:30:33 -0800993 source.clear();
Stewart Milesf645c5e2014-05-01 09:03:27 -0700994 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -0700995 }
996
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -0700997 if (remaining_frames > 0) {
Stewart Miles3dd36f92014-05-01 09:03:27 -0700998 const size_t remaining_bytes = remaining_frames * frame_size;
Stewart Milesc049a0a2014-05-01 09:03:27 -0700999 SUBMIX_ALOGV(" remaining_frames = %zu", remaining_frames);
Stewart Miles3dd36f92014-05-01 09:03:27 -07001000 memset(((char*)buffer)+ bytes - remaining_bytes, 0, remaining_bytes);
Jean-Michel Trivi6acd9662012-09-11 19:19:08 -07001001 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001002
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001003 // compute how much we need to sleep after reading the data by comparing the wall clock with
1004 // the projected time at which we should return.
1005 struct timespec time_after_read;// wall clock after reading from the pipe
1006 struct timespec record_duration;// observed record duration
1007 int rc = clock_gettime(CLOCK_MONOTONIC, &time_after_read);
1008 const uint32_t sample_rate = in_get_sample_rate(&stream->common);
1009 if (rc == 0) {
1010 // for how long have we been recording?
1011 record_duration.tv_sec = time_after_read.tv_sec - in->record_start_time.tv_sec;
1012 record_duration.tv_nsec = time_after_read.tv_nsec - in->record_start_time.tv_nsec;
1013 if (record_duration.tv_nsec < 0) {
1014 record_duration.tv_sec--;
1015 record_duration.tv_nsec += 1000000000;
1016 }
1017
Stewart Milesf645c5e2014-05-01 09:03:27 -07001018 // read_counter_frames contains the number of frames that have been read since the
1019 // beginning of recording (including this call): it's converted to usec and compared to
1020 // how long we've been recording for, which gives us how long we must wait to sync the
1021 // projected recording time, and the observed recording time.
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001022 long projected_vs_observed_offset_us =
1023 ((int64_t)(in->read_counter_frames
1024 - (record_duration.tv_sec*sample_rate)))
1025 * 1000000 / sample_rate
1026 - (record_duration.tv_nsec / 1000);
1027
Stewart Milesc049a0a2014-05-01 09:03:27 -07001028 SUBMIX_ALOGV(" record duration %5lds %3ldms, will wait: %7ldus",
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001029 record_duration.tv_sec, record_duration.tv_nsec/1000000,
1030 projected_vs_observed_offset_us);
1031 if (projected_vs_observed_offset_us > 0) {
1032 usleep(projected_vs_observed_offset_us);
1033 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001034 }
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001035
Stewart Milesc049a0a2014-05-01 09:03:27 -07001036 SUBMIX_ALOGV("in_read returns %zu", bytes);
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001037 return bytes;
1038
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001039}
1040
1041static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1042{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001043 (void)stream;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001044 return 0;
1045}
1046
1047static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1048{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001049 (void)stream;
1050 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001051 return 0;
1052}
1053
1054static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1055{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001056 (void)stream;
1057 (void)effect;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001058 return 0;
1059}
1060
1061static int adev_open_output_stream(struct audio_hw_device *dev,
1062 audio_io_handle_t handle,
1063 audio_devices_t devices,
1064 audio_output_flags_t flags,
1065 struct audio_config *config,
1066 struct audio_stream_out **stream_out)
1067{
Stewart Milesf645c5e2014-05-01 09:03:27 -07001068 struct submix_audio_device * const rsxadev = audio_hw_device_get_submix_audio_device(dev);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001069 ALOGV("adev_open_output_stream()");
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001070 struct submix_stream_out *out;
Stewart Milesc049a0a2014-05-01 09:03:27 -07001071 (void)handle;
1072 (void)devices;
1073 (void)flags;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001074
Stewart Miles3dd36f92014-05-01 09:03:27 -07001075 *stream_out = NULL;
1076
Stewart Miles70726842014-05-01 09:03:27 -07001077 // Make sure it's possible to open the device given the current audio config.
1078 submix_sanitize_config(config, false);
1079 if (!submix_open_validate(rsxadev, &rsxadev->lock, config, false)) {
1080 ALOGE("adev_open_output_stream(): Unable to open output stream.");
1081 return -EINVAL;
1082 }
1083
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001084 out = (struct submix_stream_out *)calloc(1, sizeof(struct submix_stream_out));
Stewart Miles3dd36f92014-05-01 09:03:27 -07001085 if (!out) return -ENOMEM;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001086
Stewart Miles568e66f2014-05-01 09:03:27 -07001087 // Initialize the function pointer tables (v-tables).
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001088 out->stream.common.get_sample_rate = out_get_sample_rate;
1089 out->stream.common.set_sample_rate = out_set_sample_rate;
1090 out->stream.common.get_buffer_size = out_get_buffer_size;
1091 out->stream.common.get_channels = out_get_channels;
1092 out->stream.common.get_format = out_get_format;
1093 out->stream.common.set_format = out_set_format;
1094 out->stream.common.standby = out_standby;
1095 out->stream.common.dump = out_dump;
1096 out->stream.common.set_parameters = out_set_parameters;
1097 out->stream.common.get_parameters = out_get_parameters;
1098 out->stream.common.add_audio_effect = out_add_audio_effect;
1099 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1100 out->stream.get_latency = out_get_latency;
1101 out->stream.set_volume = out_set_volume;
1102 out->stream.write = out_write;
1103 out->stream.get_render_position = out_get_render_position;
1104 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1105
Stewart Miles3dd36f92014-05-01 09:03:27 -07001106 // If the sink has been shutdown, delete the pipe so that it's recreated.
1107 pthread_mutex_lock(&rsxadev->lock);
1108 if (rsxadev->rsxSink != NULL && rsxadev->rsxSink->isShutdown()) {
1109 submix_audio_device_release_pipe(rsxadev);
1110 }
1111 pthread_mutex_unlock(&rsxadev->lock);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001112
Stewart Miles568e66f2014-05-01 09:03:27 -07001113 // Store a pointer to the device from the output stream.
1114 out->dev = rsxadev;
Stewart Miles3dd36f92014-05-01 09:03:27 -07001115 // Initialize the pipe.
1116 ALOGV("adev_open_output_stream(): Initializing pipe");
1117 submix_audio_device_create_pipe(rsxadev, config, DEFAULT_PIPE_SIZE_IN_FRAMES,
1118 DEFAULT_PIPE_PERIOD_COUNT, NULL, out);
Stewart Miles92854f52014-05-01 09:03:27 -07001119#if LOG_STREAMS_TO_FILES
1120 out->log_fd = open(LOG_STREAM_OUT_FILENAME, O_CREAT | O_TRUNC | O_WRONLY,
1121 LOG_STREAM_FILE_PERMISSIONS);
1122 ALOGE_IF(out->log_fd < 0, "adev_open_output_stream(): log file open failed %s",
1123 strerror(errno));
1124 ALOGV("adev_open_output_stream(): log_fd = %d", out->log_fd);
1125#endif // LOG_STREAMS_TO_FILES
Stewart Miles568e66f2014-05-01 09:03:27 -07001126 // Return the output stream.
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001127 *stream_out = &out->stream;
1128
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001129 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001130}
1131
1132static void adev_close_output_stream(struct audio_hw_device *dev,
1133 struct audio_stream_out *stream)
1134{
Stewart Miles3dd36f92014-05-01 09:03:27 -07001135 struct submix_stream_out * const out = audio_stream_out_get_submix_stream_out(stream);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001136 ALOGV("adev_close_output_stream()");
Stewart Miles3dd36f92014-05-01 09:03:27 -07001137 submix_audio_device_destroy_pipe(audio_hw_device_get_submix_audio_device(dev), NULL, out);
Stewart Miles92854f52014-05-01 09:03:27 -07001138#if LOG_STREAMS_TO_FILES
1139 if (out->log_fd >= 0) close(out->log_fd);
1140#endif // LOG_STREAMS_TO_FILES
Stewart Miles3dd36f92014-05-01 09:03:27 -07001141 free(out);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001142}
1143
1144static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1145{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001146 (void)dev;
1147 (void)kvpairs;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001148 return -ENOSYS;
1149}
1150
1151static char * adev_get_parameters(const struct audio_hw_device *dev,
1152 const char *keys)
1153{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001154 (void)dev;
1155 (void)keys;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001156 return strdup("");;
1157}
1158
1159static int adev_init_check(const struct audio_hw_device *dev)
1160{
1161 ALOGI("adev_init_check()");
Stewart Milesc049a0a2014-05-01 09:03:27 -07001162 (void)dev;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001163 return 0;
1164}
1165
1166static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1167{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001168 (void)dev;
1169 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001170 return -ENOSYS;
1171}
1172
1173static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1174{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001175 (void)dev;
1176 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001177 return -ENOSYS;
1178}
1179
1180static int adev_get_master_volume(struct audio_hw_device *dev, float *volume)
1181{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001182 (void)dev;
1183 (void)volume;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001184 return -ENOSYS;
1185}
1186
1187static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
1188{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001189 (void)dev;
1190 (void)muted;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001191 return -ENOSYS;
1192}
1193
1194static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
1195{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001196 (void)dev;
1197 (void)muted;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001198 return -ENOSYS;
1199}
1200
1201static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1202{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001203 (void)dev;
1204 (void)mode;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001205 return 0;
1206}
1207
1208static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1209{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001210 (void)dev;
1211 (void)state;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001212 return -ENOSYS;
1213}
1214
1215static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1216{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001217 (void)dev;
1218 (void)state;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001219 return -ENOSYS;
1220}
1221
1222static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
1223 const struct audio_config *config)
1224{
Stewart Miles568e66f2014-05-01 09:03:27 -07001225 if (audio_is_linear_pcm(config->format)) {
1226 const size_t buffer_period_size_frames =
1227 audio_hw_device_get_submix_audio_device(const_cast<struct audio_hw_device*>(dev))->
Stewart Miles3dd36f92014-05-01 09:03:27 -07001228 config.buffer_period_size_frames;
Stewart Miles568e66f2014-05-01 09:03:27 -07001229 const size_t frame_size_in_bytes = get_channel_count_from_mask(config->channel_mask) *
1230 audio_bytes_per_sample(config->format);
1231 const size_t buffer_size = buffer_period_size_frames * frame_size_in_bytes;
1232 SUBMIX_ALOGV("out_get_buffer_size() returns %zu bytes, %zu frames",
1233 buffer_size, buffer_period_size_frames);
1234 return buffer_size;
1235 }
1236 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001237}
1238
1239static int adev_open_input_stream(struct audio_hw_device *dev,
1240 audio_io_handle_t handle,
1241 audio_devices_t devices,
1242 struct audio_config *config,
1243 struct audio_stream_in **stream_in)
1244{
Stewart Milesf645c5e2014-05-01 09:03:27 -07001245 struct submix_audio_device *rsxadev = audio_hw_device_get_submix_audio_device(dev);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001246 struct submix_stream_in *in;
Stewart Miles568e66f2014-05-01 09:03:27 -07001247 ALOGI("adev_open_input_stream()");
Stewart Milesc049a0a2014-05-01 09:03:27 -07001248 (void)handle;
1249 (void)devices;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001250
Stewart Miles3dd36f92014-05-01 09:03:27 -07001251 *stream_in = NULL;
1252
Stewart Miles70726842014-05-01 09:03:27 -07001253 // Make sure it's possible to open the device given the current audio config.
1254 submix_sanitize_config(config, true);
1255 if (!submix_open_validate(rsxadev, &rsxadev->lock, config, true)) {
1256 ALOGE("adev_open_input_stream(): Unable to open input stream.");
1257 return -EINVAL;
1258 }
1259
Stewart Miles3dd36f92014-05-01 09:03:27 -07001260#if ENABLE_LEGACY_INPUT_OPEN
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001261 pthread_mutex_lock(&rsxadev->lock);
Stewart Miles3dd36f92014-05-01 09:03:27 -07001262 in = rsxadev->input;
1263 if (in) {
1264 in->ref_count++;
1265 sp<MonoPipe> sink = rsxadev->rsxSink;
1266 ALOG_ASSERT(sink != NULL);
1267 // If the sink has been shutdown, delete the pipe.
1268 if (sink->isShutdown()) submix_audio_device_release_pipe(rsxadev);
1269 }
1270 pthread_mutex_unlock(&rsxadev->lock);
1271#else
1272 in = NULL;
1273#endif // ENABLE_LEGACY_INPUT_OPEN
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001274
Stewart Miles3dd36f92014-05-01 09:03:27 -07001275 if (!in) {
1276 in = (struct submix_stream_in *)calloc(1, sizeof(struct submix_stream_in));
1277 if (!in) return -ENOMEM;
1278 in->ref_count = 1;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001279
Stewart Miles3dd36f92014-05-01 09:03:27 -07001280 // Initialize the function pointer tables (v-tables).
1281 in->stream.common.get_sample_rate = in_get_sample_rate;
1282 in->stream.common.set_sample_rate = in_set_sample_rate;
1283 in->stream.common.get_buffer_size = in_get_buffer_size;
1284 in->stream.common.get_channels = in_get_channels;
1285 in->stream.common.get_format = in_get_format;
1286 in->stream.common.set_format = in_set_format;
1287 in->stream.common.standby = in_standby;
1288 in->stream.common.dump = in_dump;
1289 in->stream.common.set_parameters = in_set_parameters;
1290 in->stream.common.get_parameters = in_get_parameters;
1291 in->stream.common.add_audio_effect = in_add_audio_effect;
1292 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1293 in->stream.set_gain = in_set_gain;
1294 in->stream.read = in_read;
1295 in->stream.get_input_frames_lost = in_get_input_frames_lost;
1296 }
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001297
Stewart Miles568e66f2014-05-01 09:03:27 -07001298 // Initialize the input stream.
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001299 in->read_counter_frames = 0;
1300 in->output_standby = rsxadev->output_standby;
Stewart Miles3dd36f92014-05-01 09:03:27 -07001301 in->dev = rsxadev;
1302 // Initialize the pipe.
1303 submix_audio_device_create_pipe(rsxadev, config, DEFAULT_PIPE_SIZE_IN_FRAMES,
1304 DEFAULT_PIPE_PERIOD_COUNT, in, NULL);
Stewart Miles92854f52014-05-01 09:03:27 -07001305#if LOG_STREAMS_TO_FILES
1306 in->log_fd = open(LOG_STREAM_IN_FILENAME, O_CREAT | O_TRUNC | O_WRONLY,
1307 LOG_STREAM_FILE_PERMISSIONS);
1308 ALOGE_IF(in->log_fd < 0, "adev_open_input_stream(): log file open failed %s",
1309 strerror(errno));
1310 ALOGV("adev_open_input_stream(): log_fd = %d", in->log_fd);
1311#endif // LOG_STREAMS_TO_FILES
Stewart Miles3dd36f92014-05-01 09:03:27 -07001312 // Return the input stream.
1313 *stream_in = &in->stream;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001314
1315 return 0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001316}
1317
1318static void adev_close_input_stream(struct audio_hw_device *dev,
Stewart Milesc049a0a2014-05-01 09:03:27 -07001319 struct audio_stream_in *stream)
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001320{
Stewart Miles3dd36f92014-05-01 09:03:27 -07001321 struct submix_stream_in * const in = audio_stream_in_get_submix_stream_in(stream);
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001322 ALOGV("adev_close_input_stream()");
Stewart Miles3dd36f92014-05-01 09:03:27 -07001323 submix_audio_device_destroy_pipe(audio_hw_device_get_submix_audio_device(dev), in, NULL);
Stewart Miles92854f52014-05-01 09:03:27 -07001324#if LOG_STREAMS_TO_FILES
1325 if (in->log_fd >= 0) close(in->log_fd);
1326#endif // LOG_STREAMS_TO_FILES
Stewart Miles3dd36f92014-05-01 09:03:27 -07001327#if ENABLE_LEGACY_INPUT_OPEN
1328 if (in->ref_count == 0) free(in);
1329#else
1330 free(in);
1331#endif // ENABLE_LEGACY_INPUT_OPEN
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001332}
1333
1334static int adev_dump(const audio_hw_device_t *device, int fd)
1335{
Stewart Milesc049a0a2014-05-01 09:03:27 -07001336 (void)device;
1337 (void)fd;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001338 return 0;
1339}
1340
1341static int adev_close(hw_device_t *device)
1342{
1343 ALOGI("adev_close()");
1344 free(device);
1345 return 0;
1346}
1347
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001348static int adev_open(const hw_module_t* module, const char* name,
1349 hw_device_t** device)
1350{
1351 ALOGI("adev_open(name=%s)", name);
1352 struct submix_audio_device *rsxadev;
1353
1354 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1355 return -EINVAL;
1356
1357 rsxadev = (submix_audio_device*) calloc(1, sizeof(struct submix_audio_device));
1358 if (!rsxadev)
1359 return -ENOMEM;
1360
1361 rsxadev->device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent5d85c532012-09-10 10:36:09 -07001362 rsxadev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001363 rsxadev->device.common.module = (struct hw_module_t *) module;
1364 rsxadev->device.common.close = adev_close;
1365
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001366 rsxadev->device.init_check = adev_init_check;
1367 rsxadev->device.set_voice_volume = adev_set_voice_volume;
1368 rsxadev->device.set_master_volume = adev_set_master_volume;
1369 rsxadev->device.get_master_volume = adev_get_master_volume;
1370 rsxadev->device.set_master_mute = adev_set_master_mute;
1371 rsxadev->device.get_master_mute = adev_get_master_mute;
1372 rsxadev->device.set_mode = adev_set_mode;
1373 rsxadev->device.set_mic_mute = adev_set_mic_mute;
1374 rsxadev->device.get_mic_mute = adev_get_mic_mute;
1375 rsxadev->device.set_parameters = adev_set_parameters;
1376 rsxadev->device.get_parameters = adev_get_parameters;
1377 rsxadev->device.get_input_buffer_size = adev_get_input_buffer_size;
1378 rsxadev->device.open_output_stream = adev_open_output_stream;
1379 rsxadev->device.close_output_stream = adev_close_output_stream;
1380 rsxadev->device.open_input_stream = adev_open_input_stream;
1381 rsxadev->device.close_input_stream = adev_close_input_stream;
1382 rsxadev->device.dump = adev_dump;
1383
Jean-Michel Trivieec87702012-09-17 09:59:42 -07001384 rsxadev->input_standby = true;
1385 rsxadev->output_standby = true;
1386
Jean-Michel Trivi88b79cb2012-08-16 13:56:03 -07001387 *device = &rsxadev->device.common;
1388
1389 return 0;
1390}
1391
1392static struct hw_module_methods_t hal_module_methods = {
1393 /* open */ adev_open,
1394};
1395
1396struct audio_module HAL_MODULE_INFO_SYM = {
1397 /* common */ {
1398 /* tag */ HARDWARE_MODULE_TAG,
1399 /* module_api_version */ AUDIO_MODULE_API_VERSION_0_1,
1400 /* hal_api_version */ HARDWARE_HAL_API_VERSION,
1401 /* id */ AUDIO_HARDWARE_MODULE_ID,
1402 /* name */ "Wifi Display audio HAL",
1403 /* author */ "The Android Open Source Project",
1404 /* methods */ &hal_module_methods,
1405 /* dso */ NULL,
1406 /* reserved */ { 0 },
1407 },
1408};
1409
1410} //namespace android
1411
1412} //extern "C"