Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 1 | /* |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 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 | */ |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 17 | |
| 18 | #define LOG_TAG "AudioFlinger" |
| 19 | //#define LOG_NDEBUG 0 |
Dean Wheatley | ace1eeb | 2023-11-06 21:58:14 +1100 | [diff] [blame] | 20 | #include "Configuration.h" |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 21 | #include <system/audio.h> |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 22 | #include <utils/Log.h> |
| 23 | |
| 24 | #include <audio_utils/spdif/SPDIFEncoder.h> |
| 25 | |
| 26 | #include "AudioHwDevice.h" |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 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 | */ |
Phil Burk | 23d8997 | 2015-04-06 16:22:23 -0700 | [diff] [blame] | 35 | SpdifStreamOut::SpdifStreamOut(AudioHwDevice *dev, |
Phil Burk | 23d8997 | 2015-04-06 16:22:23 -0700 | [diff] [blame] | 36 | audio_format_t format) |
Dean Wheatley | dfb67b8 | 2024-01-23 09:36:29 +1100 | [diff] [blame^] | 37 | : AudioStreamOut(dev) |
Phil Burk | 23d8997 | 2015-04-06 16:22:23 -0700 | [diff] [blame] | 38 | , mSpdifEncoder(this, format) |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
| 42 | status_t SpdifStreamOut::open( |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 43 | audio_io_handle_t handle, |
| 44 | audio_devices_t devices, |
| 45 | struct audio_config *config, |
Dean Wheatley | dfb67b8 | 2024-01-23 09:36:29 +1100 | [diff] [blame^] | 46 | audio_output_flags_t *flags, |
Haofan Wang | b75aa6a | 2024-07-09 23:06:58 -0700 | [diff] [blame] | 47 | const char *address, |
| 48 | const std::vector<playback_track_metadata_v7_t>& sourceMetadata) |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 49 | { |
| 50 | struct audio_config customConfig = *config; |
| 51 | |
Dean Wheatley | 60e8722 | 2023-11-15 20:56:14 +1100 | [diff] [blame] | 52 | mApplicationConfig.format = config->format; |
| 53 | mApplicationConfig.sample_rate = config->sample_rate; |
| 54 | mApplicationConfig.channel_mask = config->channel_mask; |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 55 | |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 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; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 60 | } |
| 61 | customConfig.sample_rate = config->sample_rate * mRateMultiplier; |
| 62 | |
Phil Burk | 23d8997 | 2015-04-06 16:22:23 -0700 | [diff] [blame] | 63 | customConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 64 | customConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
Dean Wheatley | dfb67b8 | 2024-01-23 09:36:29 +1100 | [diff] [blame^] | 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); |
Phil Burk | 23d8997 | 2015-04-06 16:22:23 -0700 | [diff] [blame] | 67 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 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. |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 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); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 75 | |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 76 | const status_t status = AudioStreamOut::open( |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 77 | handle, |
| 78 | devices, |
| 79 | &customConfig, |
Dean Wheatley | dfb67b8 | 2024-01-23 09:36:29 +1100 | [diff] [blame^] | 80 | flags, |
Haofan Wang | b75aa6a | 2024-07-09 23:06:58 -0700 | [diff] [blame] | 81 | address, |
| 82 | sourceMetadata); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 83 | |
| 84 | ALOGI("SpdifStreamOut::open() status = %d", status); |
| 85 | |
Dean Wheatley | ace1eeb | 2023-11-06 21:58:14 +1100 | [diff] [blame] | 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_out_mask(customConfig.channel_mask), |
| 91 | AUDIO_FORMAT_IEC61937, NBAIO_Tee::TEE_FLAG_OUTPUT_THREAD); |
| 92 | mTee.setId(std::string("_") + std::to_string(handle) + "_D"); |
| 93 | } |
| 94 | #endif |
| 95 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 96 | return status; |
| 97 | } |
| 98 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 99 | int SpdifStreamOut::flush() |
| 100 | { |
Phil Burk | 48e6ea9 | 2015-06-18 15:37:08 -0700 | [diff] [blame] | 101 | mSpdifEncoder.reset(); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 102 | return AudioStreamOut::flush(); |
| 103 | } |
| 104 | |
| 105 | int SpdifStreamOut::standby() |
| 106 | { |
Phil Burk | 48e6ea9 | 2015-06-18 15:37:08 -0700 | [diff] [blame] | 107 | mSpdifEncoder.reset(); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 108 | return AudioStreamOut::standby(); |
| 109 | } |
| 110 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 111 | ssize_t SpdifStreamOut::writeDataBurst(const void* buffer, size_t bytes) |
| 112 | { |
Dean Wheatley | ace1eeb | 2023-11-06 21:58:14 +1100 | [diff] [blame] | 113 | const ssize_t written = AudioStreamOut::write(buffer, bytes); |
| 114 | |
| 115 | #ifdef TEE_SINK |
| 116 | if (written > 0) { |
| 117 | mTee.write(reinterpret_cast<const char *>(buffer), |
| 118 | written / AudioStreamOut::getFrameSize()); |
| 119 | } |
| 120 | #endif |
| 121 | return written; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 124 | ssize_t SpdifStreamOut::write(const void* buffer, size_t numBytes) |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 125 | { |
| 126 | // Write to SPDIF wrapper. It will call back to writeDataBurst(). |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 127 | return mSpdifEncoder.write(buffer, numBytes); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | } // namespace android |