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 | */ |
Phil Burk | 23d8997 | 2015-04-06 16:22:23 -0700 | [diff] [blame] | 35 | SpdifStreamOut::SpdifStreamOut(AudioHwDevice *dev, |
| 36 | audio_output_flags_t flags, |
| 37 | audio_format_t format) |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 38 | : AudioStreamOut(dev,flags) |
Phil Burk | 23d8997 | 2015-04-06 16:22:23 -0700 | [diff] [blame] | 39 | , mSpdifEncoder(this, format) |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 40 | { |
| 41 | } |
| 42 | |
| 43 | status_t SpdifStreamOut::open( |
| 44 | audio_io_handle_t handle, |
| 45 | audio_devices_t devices, |
| 46 | struct audio_config *config, |
| 47 | const char *address) |
| 48 | { |
| 49 | struct audio_config customConfig = *config; |
| 50 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 51 | // Some data bursts run at a higher sample rate. |
Phil Burk | 23d8997 | 2015-04-06 16:22:23 -0700 | [diff] [blame] | 52 | // TODO Move this into the audio_utils as a static method. |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 53 | switch(config->format) { |
| 54 | case AUDIO_FORMAT_E_AC3: |
| 55 | mRateMultiplier = 4; |
| 56 | break; |
| 57 | case AUDIO_FORMAT_AC3: |
Phil Burk | 23d8997 | 2015-04-06 16:22:23 -0700 | [diff] [blame] | 58 | case AUDIO_FORMAT_DTS: |
| 59 | case AUDIO_FORMAT_DTS_HD: |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 60 | mRateMultiplier = 1; |
| 61 | break; |
| 62 | default: |
| 63 | ALOGE("ERROR SpdifStreamOut::open() unrecognized format 0x%08X\n", |
| 64 | config->format); |
| 65 | return BAD_VALUE; |
| 66 | } |
| 67 | customConfig.sample_rate = config->sample_rate * mRateMultiplier; |
| 68 | |
Phil Burk | 23d8997 | 2015-04-06 16:22:23 -0700 | [diff] [blame] | 69 | customConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 70 | customConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 71 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 72 | // Always print this because otherwise it could be very confusing if the |
| 73 | // HAL and AudioFlinger are using different formats. |
| 74 | // Print before open() because HAL may modify customConfig. |
| 75 | ALOGI("SpdifStreamOut::open() AudioFlinger requested" |
| 76 | " sampleRate %d, format %#x, channelMask %#x", |
| 77 | config->sample_rate, |
| 78 | config->format, |
| 79 | config->channel_mask); |
| 80 | ALOGI("SpdifStreamOut::open() HAL configured for" |
| 81 | " sampleRate %d, format %#x, channelMask %#x", |
| 82 | customConfig.sample_rate, |
| 83 | customConfig.format, |
| 84 | customConfig.channel_mask); |
| 85 | |
| 86 | status_t status = AudioStreamOut::open( |
| 87 | handle, |
| 88 | devices, |
| 89 | &customConfig, |
| 90 | address); |
| 91 | |
| 92 | ALOGI("SpdifStreamOut::open() status = %d", status); |
| 93 | |
| 94 | return status; |
| 95 | } |
| 96 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 97 | int SpdifStreamOut::flush() |
| 98 | { |
Phil Burk | 48e6ea9 | 2015-06-18 15:37:08 -0700 | [diff] [blame] | 99 | mSpdifEncoder.reset(); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 100 | return AudioStreamOut::flush(); |
| 101 | } |
| 102 | |
| 103 | int SpdifStreamOut::standby() |
| 104 | { |
Phil Burk | 48e6ea9 | 2015-06-18 15:37:08 -0700 | [diff] [blame] | 105 | mSpdifEncoder.reset(); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 106 | return AudioStreamOut::standby(); |
| 107 | } |
| 108 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 109 | size_t SpdifStreamOut::getFrameSize() |
| 110 | { |
| 111 | return sizeof(int8_t); |
| 112 | } |
| 113 | |
| 114 | ssize_t SpdifStreamOut::writeDataBurst(const void* buffer, size_t bytes) |
| 115 | { |
| 116 | return AudioStreamOut::write(buffer, bytes); |
| 117 | } |
| 118 | |
| 119 | ssize_t SpdifStreamOut::write(const void* buffer, size_t bytes) |
| 120 | { |
| 121 | // Write to SPDIF wrapper. It will call back to writeDataBurst(). |
| 122 | return mSpdifEncoder.write(buffer, bytes); |
| 123 | } |
| 124 | |
| 125 | } // namespace android |