1 /*
2 *
3 * Copyright 2023, The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #define LOG_TAG "AudioFlinger"
19 //#define LOG_NDEBUG 0
20 #include "Configuration.h"
21 #include <system/audio.h>
22 #include <utils/Log.h>
23
24 #include <audio_utils/spdif/SPDIFDecoder.h>
25
26 #include "AudioHwDevice.h"
27 #include "SpdifStreamIn.h"
28
29 namespace android {
30
31 /**
32 * If the HAL is generating IEC61937 data and AudioFlinger expects elementary stream then we need to
33 * extract the data using an SPDIF decoder.
34 */
SpdifStreamIn(AudioHwDevice * dev,audio_input_flags_t flags,audio_format_t format)35 SpdifStreamIn::SpdifStreamIn(AudioHwDevice *dev,
36 audio_input_flags_t flags,
37 audio_format_t format)
38 : AudioStreamIn(dev, flags)
39 , mSpdifDecoder(this, format)
40 {
41 }
42
open(audio_io_handle_t handle,audio_devices_t devices,struct audio_config * config,const char * address,audio_source_t source,audio_devices_t outputDevice,const char * outputDeviceAddress)43 status_t SpdifStreamIn::open(
44 audio_io_handle_t handle,
45 audio_devices_t devices,
46 struct audio_config *config,
47 const char *address,
48 audio_source_t source,
49 audio_devices_t outputDevice,
50 const char* outputDeviceAddress)
51 {
52 struct audio_config customConfig = *config;
53
54 mApplicationConfig.format = config->format;
55 mApplicationConfig.sample_rate = config->sample_rate;
56 mApplicationConfig.channel_mask = config->channel_mask;
57
58 mRateMultiplier = spdif_rate_multiplier(config->format);
59 if (mRateMultiplier <= 0) {
60 ALOGE("ERROR SpdifStreamIn::open() unrecognized format 0x%08X\n", config->format);
61 return BAD_VALUE;
62 }
63 customConfig.sample_rate = config->sample_rate * mRateMultiplier;
64 customConfig.format = AUDIO_FORMAT_PCM_16_BIT;
65 customConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
66
67 // Always print this because otherwise it could be very confusing if the
68 // HAL and AudioFlinger are using different formats.
69 // Print before open() because HAL may modify customConfig.
70 ALOGI("SpdifStreamIn::open() AudioFlinger requested sampleRate %d, format %#x, channelMask %#x",
71 config->sample_rate, config->format, config->channel_mask);
72 ALOGI("SpdifStreamIn::open() HAL configured for sampleRate %d, format %#x, channelMask %#x",
73 customConfig.sample_rate, customConfig.format, customConfig.channel_mask);
74
75 const status_t status = AudioStreamIn::open(
76 handle,
77 devices,
78 &customConfig,
79 address,
80 source,
81 outputDevice,
82 outputDeviceAddress);
83
84 // reset config back to whatever is returned by HAL
85 config->sample_rate = customConfig.sample_rate;
86 config->format = customConfig.format;
87 config->channel_mask = customConfig.channel_mask;
88
89 ALOGI("SpdifStreamIn::open() status = %d", status);
90
91 #ifdef TEE_SINK
92 if (status == OK) {
93 // Don't use PCM 16-bit format to avoid WAV encoding IEC61937 data.
94 mTee.set(customConfig.sample_rate,
95 audio_channel_count_from_in_mask(customConfig.channel_mask),
96 AUDIO_FORMAT_IEC61937, NBAIO_Tee::TEE_FLAG_INPUT_THREAD);
97 mTee.setId(std::string("_") + std::to_string(handle) + "_C");
98 }
99 #endif
100
101 return status;
102 }
103
standby()104 int SpdifStreamIn::standby()
105 {
106 mSpdifDecoder.reset();
107 return AudioStreamIn::standby();
108 }
109
readDataBurst(void * buffer,size_t bytes,size_t * read)110 status_t SpdifStreamIn::readDataBurst(void* buffer, size_t bytes, size_t* read)
111 {
112 status_t status = AudioStreamIn::read(buffer, bytes, read);
113
114 #ifdef TEE_SINK
115 if (*read > 0) {
116 mTee.write(reinterpret_cast<const char *>(buffer), *read / AudioStreamIn::getFrameSize());
117 }
118 #endif
119 return status;
120 }
121
read(void * buffer,size_t numBytes,size_t * read)122 status_t SpdifStreamIn::read(void* buffer, size_t numBytes, size_t* read)
123 {
124 // Read from SPDIF extractor. It will call back to readDataBurst().
125 const auto bytesRead = mSpdifDecoder.read(buffer, numBytes);
126 if (bytesRead >= 0) {
127 *read = bytesRead;
128 return OK;
129 }
130 return NOT_ENOUGH_DATA;
131 }
132
133 } // namespace android
134