Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 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 |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 20 | #include <system/audio.h> |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 21 | #include <utils/Log.h> |
| 22 | |
| 23 | #include <audio_utils/spdif/SPDIFEncoder.h> |
| 24 | |
| 25 | #include "AudioHwDevice.h" |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 26 | #include "SpdifStreamOut.h" |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | /** |
| 31 | * If the AudioFlinger is processing encoded data and the HAL expects |
| 32 | * PCM then we need to wrap the data in an SPDIF wrapper. |
| 33 | */ |
Phil Burk | 23d8997 | 2015-04-06 16:22:23 -0700 | [diff] [blame] | 34 | SpdifStreamOut::SpdifStreamOut(AudioHwDevice *dev, |
| 35 | audio_output_flags_t flags, |
| 36 | audio_format_t format) |
Phil Burk | 41ae1d6 | 2015-08-04 11:11:28 -0700 | [diff] [blame] | 37 | // Tell the HAL that the data will be compressed audio wrapped in a data burst. |
| 38 | : AudioStreamOut(dev, (audio_output_flags_t) (flags | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO)) |
Phil Burk | 23d8997 | 2015-04-06 16:22:23 -0700 | [diff] [blame] | 39 | , mSpdifEncoder(this, format) |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 40 | , mApplicationFormat(AUDIO_FORMAT_DEFAULT) |
| 41 | , mApplicationSampleRate(0) |
| 42 | , mApplicationChannelMask(0) |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
| 46 | status_t SpdifStreamOut::open( |
| 47 | audio_io_handle_t handle, |
| 48 | audio_devices_t devices, |
| 49 | struct audio_config *config, |
| 50 | const char *address) |
| 51 | { |
| 52 | struct audio_config customConfig = *config; |
| 53 | |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 54 | mApplicationFormat = config->format; |
| 55 | mApplicationSampleRate = config->sample_rate; |
| 56 | mApplicationChannelMask = config->channel_mask; |
| 57 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 58 | // Some data bursts run at a higher sample rate. |
Phil Burk | 23d8997 | 2015-04-06 16:22:23 -0700 | [diff] [blame] | 59 | // TODO Move this into the audio_utils as a static method. |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 60 | switch(config->format) { |
| 61 | case AUDIO_FORMAT_E_AC3: |
Dean Wheatley | f14e77f | 2017-03-23 15:52:01 +1100 | [diff] [blame] | 62 | case AUDIO_FORMAT_E_AC3_JOC: |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 63 | mRateMultiplier = 4; |
| 64 | break; |
| 65 | case AUDIO_FORMAT_AC3: |
Phil Burk | 23d8997 | 2015-04-06 16:22:23 -0700 | [diff] [blame] | 66 | case AUDIO_FORMAT_DTS: |
| 67 | case AUDIO_FORMAT_DTS_HD: |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 68 | mRateMultiplier = 1; |
| 69 | break; |
| 70 | default: |
| 71 | ALOGE("ERROR SpdifStreamOut::open() unrecognized format 0x%08X\n", |
| 72 | config->format); |
| 73 | return BAD_VALUE; |
| 74 | } |
| 75 | customConfig.sample_rate = config->sample_rate * mRateMultiplier; |
| 76 | |
Phil Burk | 23d8997 | 2015-04-06 16:22:23 -0700 | [diff] [blame] | 77 | customConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 78 | customConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 79 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 80 | // Always print this because otherwise it could be very confusing if the |
| 81 | // HAL and AudioFlinger are using different formats. |
| 82 | // Print before open() because HAL may modify customConfig. |
| 83 | ALOGI("SpdifStreamOut::open() AudioFlinger requested" |
| 84 | " sampleRate %d, format %#x, channelMask %#x", |
| 85 | config->sample_rate, |
| 86 | config->format, |
| 87 | config->channel_mask); |
| 88 | ALOGI("SpdifStreamOut::open() HAL configured for" |
| 89 | " sampleRate %d, format %#x, channelMask %#x", |
| 90 | customConfig.sample_rate, |
| 91 | customConfig.format, |
| 92 | customConfig.channel_mask); |
| 93 | |
| 94 | status_t status = AudioStreamOut::open( |
| 95 | handle, |
| 96 | devices, |
| 97 | &customConfig, |
| 98 | address); |
| 99 | |
| 100 | ALOGI("SpdifStreamOut::open() status = %d", status); |
| 101 | |
| 102 | return status; |
| 103 | } |
| 104 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 105 | int SpdifStreamOut::flush() |
| 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::flush(); |
| 109 | } |
| 110 | |
| 111 | int SpdifStreamOut::standby() |
| 112 | { |
Phil Burk | 48e6ea9 | 2015-06-18 15:37:08 -0700 | [diff] [blame] | 113 | mSpdifEncoder.reset(); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 114 | return AudioStreamOut::standby(); |
| 115 | } |
| 116 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 117 | ssize_t SpdifStreamOut::writeDataBurst(const void* buffer, size_t bytes) |
| 118 | { |
| 119 | return AudioStreamOut::write(buffer, bytes); |
| 120 | } |
| 121 | |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 122 | ssize_t SpdifStreamOut::write(const void* buffer, size_t numBytes) |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 123 | { |
| 124 | // Write to SPDIF wrapper. It will call back to writeDataBurst(). |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 125 | return mSpdifEncoder.write(buffer, numBytes); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | } // namespace android |