blob: 0c6a5a13e4d74d596cfe1eeb9c8ac88094199a2a [file] [log] [blame]
Phil Burk062e67a2015-02-11 13:40:50 -08001/*
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
Dean Wheatleyace1eeb2023-11-06 21:58:14 +110020#include "Configuration.h"
Mikhail Naganovcbc8f612016-10-11 18:05:13 -070021#include <system/audio.h>
Phil Burk062e67a2015-02-11 13:40:50 -080022#include <utils/Log.h>
23
24#include <audio_utils/spdif/SPDIFEncoder.h>
25
26#include "AudioHwDevice.h"
Phil Burk062e67a2015-02-11 13:40:50 -080027#include "SpdifStreamOut.h"
28
29namespace 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 */
Phil Burk23d89972015-04-06 16:22:23 -070035SpdifStreamOut::SpdifStreamOut(AudioHwDevice *dev,
36 audio_output_flags_t flags,
37 audio_format_t format)
Phil Burk41ae1d62015-08-04 11:11:28 -070038 // Tell the HAL that the data will be compressed audio wrapped in a data burst.
39 : AudioStreamOut(dev, (audio_output_flags_t) (flags | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO))
Phil Burk23d89972015-04-06 16:22:23 -070040 , mSpdifEncoder(this, format)
Phil Burk062e67a2015-02-11 13:40:50 -080041{
42}
43
44status_t SpdifStreamOut::open(
45 audio_io_handle_t handle,
46 audio_devices_t devices,
47 struct audio_config *config,
48 const char *address)
49{
50 struct audio_config customConfig = *config;
51
Dean Wheatley60e87222023-11-15 20:56:14 +110052 mApplicationConfig.format = config->format;
53 mApplicationConfig.sample_rate = config->sample_rate;
54 mApplicationConfig.channel_mask = config->channel_mask;
Phil Burkca5e6142015-07-14 09:42:29 -070055
Phil Burk062e67a2015-02-11 13:40:50 -080056 // Some data bursts run at a higher sample rate.
Phil Burk23d89972015-04-06 16:22:23 -070057 // TODO Move this into the audio_utils as a static method.
Phil Burk062e67a2015-02-11 13:40:50 -080058 switch(config->format) {
59 case AUDIO_FORMAT_E_AC3:
Dean Wheatleyf14e77f2017-03-23 15:52:01 +110060 case AUDIO_FORMAT_E_AC3_JOC:
Phil Burk062e67a2015-02-11 13:40:50 -080061 mRateMultiplier = 4;
62 break;
63 case AUDIO_FORMAT_AC3:
Phil Burk23d89972015-04-06 16:22:23 -070064 case AUDIO_FORMAT_DTS:
65 case AUDIO_FORMAT_DTS_HD:
Phil Burk062e67a2015-02-11 13:40:50 -080066 mRateMultiplier = 1;
67 break;
68 default:
69 ALOGE("ERROR SpdifStreamOut::open() unrecognized format 0x%08X\n",
70 config->format);
71 return BAD_VALUE;
72 }
73 customConfig.sample_rate = config->sample_rate * mRateMultiplier;
74
Phil Burk23d89972015-04-06 16:22:23 -070075 customConfig.format = AUDIO_FORMAT_PCM_16_BIT;
76 customConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
77
Phil Burk062e67a2015-02-11 13:40:50 -080078 // Always print this because otherwise it could be very confusing if the
79 // HAL and AudioFlinger are using different formats.
80 // Print before open() because HAL may modify customConfig.
81 ALOGI("SpdifStreamOut::open() AudioFlinger requested"
82 " sampleRate %d, format %#x, channelMask %#x",
83 config->sample_rate,
84 config->format,
85 config->channel_mask);
86 ALOGI("SpdifStreamOut::open() HAL configured for"
87 " sampleRate %d, format %#x, channelMask %#x",
88 customConfig.sample_rate,
89 customConfig.format,
90 customConfig.channel_mask);
91
Andy Hung1ef77382023-06-15 14:50:18 -070092 const status_t status = AudioStreamOut::open(
Phil Burk062e67a2015-02-11 13:40:50 -080093 handle,
94 devices,
95 &customConfig,
96 address);
97
98 ALOGI("SpdifStreamOut::open() status = %d", status);
99
Dean Wheatleyace1eeb2023-11-06 21:58:14 +1100100#ifdef TEE_SINK
101 if (status == OK) {
102 // Don't use PCM 16-bit format to avoid WAV encoding IEC61937 data.
103 mTee.set(customConfig.sample_rate,
104 audio_channel_count_from_out_mask(customConfig.channel_mask),
105 AUDIO_FORMAT_IEC61937, NBAIO_Tee::TEE_FLAG_OUTPUT_THREAD);
106 mTee.setId(std::string("_") + std::to_string(handle) + "_D");
107 }
108#endif
109
Phil Burk062e67a2015-02-11 13:40:50 -0800110 return status;
111}
112
Phil Burk062e67a2015-02-11 13:40:50 -0800113int SpdifStreamOut::flush()
114{
Phil Burk48e6ea92015-06-18 15:37:08 -0700115 mSpdifEncoder.reset();
Phil Burk062e67a2015-02-11 13:40:50 -0800116 return AudioStreamOut::flush();
117}
118
119int SpdifStreamOut::standby()
120{
Phil Burk48e6ea92015-06-18 15:37:08 -0700121 mSpdifEncoder.reset();
Phil Burk062e67a2015-02-11 13:40:50 -0800122 return AudioStreamOut::standby();
123}
124
Phil Burk062e67a2015-02-11 13:40:50 -0800125ssize_t SpdifStreamOut::writeDataBurst(const void* buffer, size_t bytes)
126{
Dean Wheatleyace1eeb2023-11-06 21:58:14 +1100127 const ssize_t written = AudioStreamOut::write(buffer, bytes);
128
129#ifdef TEE_SINK
130 if (written > 0) {
131 mTee.write(reinterpret_cast<const char *>(buffer),
132 written / AudioStreamOut::getFrameSize());
133 }
134#endif
135 return written;
Phil Burk062e67a2015-02-11 13:40:50 -0800136}
137
Phil Burkca5e6142015-07-14 09:42:29 -0700138ssize_t SpdifStreamOut::write(const void* buffer, size_t numBytes)
Phil Burk062e67a2015-02-11 13:40:50 -0800139{
140 // Write to SPDIF wrapper. It will call back to writeDataBurst().
Phil Burkca5e6142015-07-14 09:42:29 -0700141 return mSpdifEncoder.write(buffer, numBytes);
Phil Burk062e67a2015-02-11 13:40:50 -0800142}
143
144} // namespace android