xref: /aosp_15_r20/frameworks/av/services/audioflinger/datapath/SpdifStreamOut.cpp (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1 /*
2  *
3  * Copyright 2015, 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/SPDIFEncoder.h>
25 
26 #include "AudioHwDevice.h"
27 #include "SpdifStreamOut.h"
28 
29 namespace android {
30 
31 /**
32  * If the AudioFlinger is processing encoded data and the HAL expects
33  * PCM then we need to wrap the data in an SPDIF wrapper.
34  */
SpdifStreamOut(AudioHwDevice * dev,audio_format_t format)35 SpdifStreamOut::SpdifStreamOut(AudioHwDevice *dev,
36             audio_format_t format)
37         : AudioStreamOut(dev)
38         , mSpdifEncoder(this, format)
39 {
40 }
41 
open(audio_io_handle_t handle,audio_devices_t devices,struct audio_config * config,audio_output_flags_t * flags,const char * address,const std::vector<playback_track_metadata_v7_t> & sourceMetadata)42 status_t SpdifStreamOut::open(
43         audio_io_handle_t handle,
44         audio_devices_t devices,
45         struct audio_config *config,
46         audio_output_flags_t *flags,
47         const char *address,
48         const std::vector<playback_track_metadata_v7_t>& sourceMetadata)
49 {
50     struct audio_config customConfig = *config;
51 
52     mApplicationConfig.format = config->format;
53     mApplicationConfig.sample_rate = config->sample_rate;
54     mApplicationConfig.channel_mask = config->channel_mask;
55 
56     mRateMultiplier = spdif_rate_multiplier(config->format);
57     if (mRateMultiplier <= 0) {
58         ALOGE("ERROR SpdifStreamOut::open() unrecognized format 0x%08X\n", config->format);
59         return BAD_VALUE;
60     }
61     customConfig.sample_rate = config->sample_rate * mRateMultiplier;
62 
63     customConfig.format = AUDIO_FORMAT_PCM_16_BIT;
64     customConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
65     // Tell the HAL that the data will be compressed audio wrapped in a data burst.
66     *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO);
67 
68     // Always print this because otherwise it could be very confusing if the
69     // HAL and AudioFlinger are using different formats.
70     // Print before open() because HAL may modify customConfig.
71     ALOGI("SpdifStreamOut::open() AudioFlinger requested sampleRate %d, format %#x,"
72             " channelMask %#x", config->sample_rate, config->format, config->channel_mask);
73     ALOGI("SpdifStreamOut::open() HAL configured for sampleRate %d, format %#x, channelMask %#x",
74             customConfig.sample_rate, customConfig.format, customConfig.channel_mask);
75 
76     const status_t status = AudioStreamOut::open(
77             handle,
78             devices,
79             &customConfig,
80             flags,
81             address,
82             sourceMetadata);
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("SpdifStreamOut::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_out_mask(customConfig.channel_mask),
96                 AUDIO_FORMAT_IEC61937, NBAIO_Tee::TEE_FLAG_OUTPUT_THREAD);
97         mTee.setId(std::string("_") + std::to_string(handle) + "_D");
98     }
99 #endif
100 
101     return status;
102 }
103 
flush()104 int SpdifStreamOut::flush()
105 {
106     mSpdifEncoder.reset();
107     return AudioStreamOut::flush();
108 }
109 
standby()110 int SpdifStreamOut::standby()
111 {
112     mSpdifEncoder.reset();
113     return AudioStreamOut::standby();
114 }
115 
writeDataBurst(const void * buffer,size_t bytes)116 ssize_t SpdifStreamOut::writeDataBurst(const void* buffer, size_t bytes)
117 {
118     const ssize_t written = AudioStreamOut::write(buffer, bytes);
119 
120 #ifdef TEE_SINK
121     if (written > 0) {
122         mTee.write(reinterpret_cast<const char *>(buffer),
123                 written / AudioStreamOut::getFrameSize());
124     }
125 #endif
126     return written;
127 }
128 
write(const void * buffer,size_t numBytes)129 ssize_t SpdifStreamOut::write(const void* buffer, size_t numBytes)
130 {
131     // Write to SPDIF wrapper. It will call back to writeDataBurst().
132     return mSpdifEncoder.write(buffer, numBytes);
133 }
134 
135 } // namespace android
136