• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     ALOGI("SpdifStreamIn::open() status = %d", status);
85 
86 #ifdef TEE_SINK
87     if (status == OK) {
88         // Don't use PCM 16-bit format to avoid WAV encoding IEC61937 data.
89         mTee.set(customConfig.sample_rate,
90                 audio_channel_count_from_in_mask(customConfig.channel_mask),
91                 AUDIO_FORMAT_IEC61937, NBAIO_Tee::TEE_FLAG_INPUT_THREAD);
92         mTee.setId(std::string("_") + std::to_string(handle) + "_C");
93     }
94 #endif
95 
96     return status;
97 }
98 
standby()99 int SpdifStreamIn::standby()
100 {
101     mSpdifDecoder.reset();
102     return AudioStreamIn::standby();
103 }
104 
readDataBurst(void * buffer,size_t bytes,size_t * read)105 status_t SpdifStreamIn::readDataBurst(void* buffer, size_t bytes, size_t* read)
106 {
107     status_t status = AudioStreamIn::read(buffer, bytes, read);
108 
109 #ifdef TEE_SINK
110     if (*read > 0) {
111         mTee.write(reinterpret_cast<const char *>(buffer), *read / AudioStreamIn::getFrameSize());
112     }
113 #endif
114     return status;
115 }
116 
read(void * buffer,size_t numBytes,size_t * read)117 status_t SpdifStreamIn::read(void* buffer, size_t numBytes, size_t* read)
118 {
119     // Read from SPDIF extractor. It will call back to readDataBurst().
120     const auto bytesRead = mSpdifDecoder.read(buffer, numBytes);
121     if (bytesRead >= 0) {
122         *read = bytesRead;
123         return OK;
124     }
125     return NOT_ENOUGH_DATA;
126 }
127 
128 } // namespace android
129