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 <system/audio.h> 21 #include <utils/Log.h> 22 23 #include <audio_utils/spdif/SPDIFEncoder.h> 24 25 #include "AudioHwDevice.h" 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 */ 34 SpdifStreamOut::SpdifStreamOut(AudioHwDevice *dev, 35 audio_output_flags_t flags, 36 audio_format_t format) 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)) 39 , mSpdifEncoder(this, format) 40 , mApplicationFormat(AUDIO_FORMAT_DEFAULT) 41 , mApplicationSampleRate(0) 42 , mApplicationChannelMask(0) 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 54 mApplicationFormat = config->format; 55 mApplicationSampleRate = config->sample_rate; 56 mApplicationChannelMask = config->channel_mask; 57 58 // Some data bursts run at a higher sample rate. 59 // TODO Move this into the audio_utils as a static method. 60 switch(config->format) { 61 case AUDIO_FORMAT_E_AC3: 62 mRateMultiplier = 4; 63 break; 64 case AUDIO_FORMAT_AC3: 65 case AUDIO_FORMAT_DTS: 66 case AUDIO_FORMAT_DTS_HD: 67 mRateMultiplier = 1; 68 break; 69 default: 70 ALOGE("ERROR SpdifStreamOut::open() unrecognized format 0x%08X\n", 71 config->format); 72 return BAD_VALUE; 73 } 74 customConfig.sample_rate = config->sample_rate * mRateMultiplier; 75 76 customConfig.format = AUDIO_FORMAT_PCM_16_BIT; 77 customConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; 78 79 // Always print this because otherwise it could be very confusing if the 80 // HAL and AudioFlinger are using different formats. 81 // Print before open() because HAL may modify customConfig. 82 ALOGI("SpdifStreamOut::open() AudioFlinger requested" 83 " sampleRate %d, format %#x, channelMask %#x", 84 config->sample_rate, 85 config->format, 86 config->channel_mask); 87 ALOGI("SpdifStreamOut::open() HAL configured for" 88 " sampleRate %d, format %#x, channelMask %#x", 89 customConfig.sample_rate, 90 customConfig.format, 91 customConfig.channel_mask); 92 93 status_t status = AudioStreamOut::open( 94 handle, 95 devices, 96 &customConfig, 97 address); 98 99 ALOGI("SpdifStreamOut::open() status = %d", status); 100 101 return status; 102 } 103 104 int SpdifStreamOut::flush() 105 { 106 mSpdifEncoder.reset(); 107 return AudioStreamOut::flush(); 108 } 109 110 int SpdifStreamOut::standby() 111 { 112 mSpdifEncoder.reset(); 113 return AudioStreamOut::standby(); 114 } 115 116 ssize_t SpdifStreamOut::writeDataBurst(const void* buffer, size_t bytes) 117 { 118 return AudioStreamOut::write(buffer, bytes); 119 } 120 121 ssize_t SpdifStreamOut::write(const void* buffer, size_t numBytes) 122 { 123 // Write to SPDIF wrapper. It will call back to writeDataBurst(). 124 return mSpdifEncoder.write(buffer, numBytes); 125 } 126 127 } // namespace android 128