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 |
| 20 | #include <hardware/audio.h> |
| 21 | #include <utils/Log.h> |
| 22 | |
| 23 | #include <audio_utils/spdif/SPDIFEncoder.h> |
| 24 | |
| 25 | #include "AudioHwDevice.h" |
| 26 | #include "AudioStreamOut.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 | */ |
| 35 | SpdifStreamOut::SpdifStreamOut(AudioHwDevice *dev, audio_output_flags_t flags) |
| 36 | : AudioStreamOut(dev,flags) |
| 37 | , mRateMultiplier(1) |
| 38 | , mSpdifEncoder(this) |
| 39 | , mRenderPositionHal(0) |
| 40 | , mPreviousHalPosition32(0) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | status_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 | |
| 52 | customConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 53 | customConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 54 | |
| 55 | // Some data bursts run at a higher sample rate. |
| 56 | switch(config->format) { |
| 57 | case AUDIO_FORMAT_E_AC3: |
| 58 | mRateMultiplier = 4; |
| 59 | break; |
| 60 | case AUDIO_FORMAT_AC3: |
| 61 | mRateMultiplier = 1; |
| 62 | break; |
| 63 | default: |
| 64 | ALOGE("ERROR SpdifStreamOut::open() unrecognized format 0x%08X\n", |
| 65 | config->format); |
| 66 | return BAD_VALUE; |
| 67 | } |
| 68 | customConfig.sample_rate = config->sample_rate * mRateMultiplier; |
| 69 | |
| 70 | // Always print this because otherwise it could be very confusing if the |
| 71 | // HAL and AudioFlinger are using different formats. |
| 72 | // Print before open() because HAL may modify customConfig. |
| 73 | ALOGI("SpdifStreamOut::open() AudioFlinger requested" |
| 74 | " sampleRate %d, format %#x, channelMask %#x", |
| 75 | config->sample_rate, |
| 76 | config->format, |
| 77 | config->channel_mask); |
| 78 | ALOGI("SpdifStreamOut::open() HAL configured for" |
| 79 | " sampleRate %d, format %#x, channelMask %#x", |
| 80 | customConfig.sample_rate, |
| 81 | customConfig.format, |
| 82 | customConfig.channel_mask); |
| 83 | |
| 84 | status_t status = AudioStreamOut::open( |
| 85 | handle, |
| 86 | devices, |
| 87 | &customConfig, |
| 88 | address); |
| 89 | |
| 90 | ALOGI("SpdifStreamOut::open() status = %d", status); |
| 91 | |
| 92 | return status; |
| 93 | } |
| 94 | |
| 95 | // Account for possibly higher sample rate. |
| 96 | status_t SpdifStreamOut::getRenderPosition(uint32_t *frames) |
| 97 | { |
| 98 | uint32_t halPosition = 0; |
| 99 | status_t status = AudioStreamOut::getRenderPosition(&halPosition); |
| 100 | if (status != NO_ERROR) { |
| 101 | return status; |
| 102 | } |
| 103 | |
| 104 | // Accumulate a 64-bit position so that we wrap at the right place. |
| 105 | if (mRateMultiplier != 1) { |
| 106 | // Maintain a 64-bit render position. |
| 107 | int32_t deltaHalPosition = (int32_t)(halPosition - mPreviousHalPosition32); |
| 108 | mPreviousHalPosition32 = halPosition; |
| 109 | mRenderPositionHal += deltaHalPosition; |
| 110 | |
| 111 | // Scale from device sample rate to application rate. |
| 112 | uint64_t renderPositionApp = mRenderPositionHal / mRateMultiplier; |
| 113 | ALOGV("SpdifStreamOut::getRenderPosition() " |
| 114 | "renderPositionAppRate = %llu = %llu / %u\n", |
| 115 | renderPositionApp, mRenderPositionHal, mRateMultiplier); |
| 116 | |
| 117 | *frames = (uint32_t)renderPositionApp; |
| 118 | } else { |
| 119 | *frames = halPosition; |
| 120 | } |
| 121 | return status; |
| 122 | } |
| 123 | |
| 124 | int SpdifStreamOut::flush() |
| 125 | { |
| 126 | // FIXME Is there an issue here with flush being asynchronous? |
| 127 | mRenderPositionHal = 0; |
| 128 | mPreviousHalPosition32 = 0; |
| 129 | return AudioStreamOut::flush(); |
| 130 | } |
| 131 | |
| 132 | int SpdifStreamOut::standby() |
| 133 | { |
| 134 | mRenderPositionHal = 0; |
| 135 | mPreviousHalPosition32 = 0; |
| 136 | return AudioStreamOut::standby(); |
| 137 | } |
| 138 | |
| 139 | // Account for possibly higher sample rate. |
| 140 | // This is much easier when all the values are 64-bit. |
| 141 | status_t SpdifStreamOut::getPresentationPosition(uint64_t *frames, |
| 142 | struct timespec *timestamp) |
| 143 | { |
| 144 | uint64_t halFrames = 0; |
| 145 | status_t status = AudioStreamOut::getPresentationPosition(&halFrames, timestamp); |
| 146 | *frames = halFrames / mRateMultiplier; |
| 147 | return status; |
| 148 | } |
| 149 | |
| 150 | size_t SpdifStreamOut::getFrameSize() |
| 151 | { |
| 152 | return sizeof(int8_t); |
| 153 | } |
| 154 | |
| 155 | ssize_t SpdifStreamOut::writeDataBurst(const void* buffer, size_t bytes) |
| 156 | { |
| 157 | return AudioStreamOut::write(buffer, bytes); |
| 158 | } |
| 159 | |
| 160 | ssize_t SpdifStreamOut::write(const void* buffer, size_t bytes) |
| 161 | { |
| 162 | // Write to SPDIF wrapper. It will call back to writeDataBurst(). |
| 163 | return mSpdifEncoder.write(buffer, bytes); |
| 164 | } |
| 165 | |
| 166 | } // namespace android |