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 #pragma once 19 20 #include <stdint.h> 21 #include <sys/types.h> 22 23 #include <system/audio.h> 24 25 #include "AudioStreamOut.h" 26 27 #include <afutils/NBAIO_Tee.h> 28 #include <audio_utils/spdif/SPDIFEncoder.h> 29 30 namespace android { 31 32 /** 33 * Stream that is a PCM data burst in the HAL but looks like an encoded stream 34 * to the AudioFlinger. Wraps encoded data in an SPDIF wrapper per IEC61973-3. 35 */ 36 class SpdifStreamOut : public AudioStreamOut { 37 public: 38 39 SpdifStreamOut(AudioHwDevice *dev, audio_format_t format); 40 41 status_t open( 42 audio_io_handle_t handle, 43 audio_devices_t devices, 44 struct audio_config *config, 45 audio_output_flags_t *flags, 46 const char *address, 47 const std::vector<playback_track_metadata_v7_t>& sourceMetadata) override; 48 49 /** 50 * Write audio buffer to driver. Returns number of bytes written, or a 51 * negative status_t. If at least one frame was written successfully prior to the error, 52 * it is suggested that the driver return that successful (short) byte count 53 * and then return an error in the subsequent call. 54 * 55 * If set_callback() has previously been called to enable non-blocking mode 56 * the write() is not allowed to block. It must write only the number of 57 * bytes that currently fit in the driver/hardware buffer and then return 58 * this byte count. If this is less than the requested write size the 59 * callback function must be called when more space is available in the 60 * driver/hardware buffer. 61 */ 62 ssize_t write(const void* buffer, size_t bytes) override; 63 64 /** 65 * @return frame size from the perspective of the application and the AudioFlinger. 66 */ getFrameSize()67 [[nodiscard]] size_t getFrameSize() const override { return sizeof(int8_t); } 68 69 /** 70 * @return audio_config_base_t from the perspective of the application and the AudioFlinger. 71 */ getAudioProperties()72 [[nodiscard]] audio_config_base_t getAudioProperties() const override { 73 return mApplicationConfig; 74 } 75 76 /** 77 * @return format from the perspective of the application and the AudioFlinger. 78 */ getFormat()79 [[nodiscard]] virtual audio_format_t getFormat() const { return mApplicationConfig.format; } 80 81 /** 82 * The HAL may be running at a higher sample rate if, for example, playing wrapped EAC3. 83 * @return sample rate from the perspective of the application and the AudioFlinger. 84 */ getSampleRate()85 [[nodiscard]] virtual uint32_t getSampleRate() const { return mApplicationConfig.sample_rate; } 86 87 /** 88 * The HAL is in stereo mode when playing multi-channel compressed audio over HDMI. 89 * @return channel mask from the perspective of the application and the AudioFlinger. 90 */ getChannelMask()91 [[nodiscard]] virtual audio_channel_mask_t getChannelMask() const { 92 return mApplicationConfig.channel_mask; 93 } 94 95 status_t flush() override; 96 status_t standby() override; 97 98 private: 99 100 class MySPDIFEncoder : public SPDIFEncoder 101 { 102 public: MySPDIFEncoder(SpdifStreamOut * spdifStreamOut,audio_format_t format)103 MySPDIFEncoder(SpdifStreamOut *spdifStreamOut, audio_format_t format) 104 : SPDIFEncoder(format) 105 , mSpdifStreamOut(spdifStreamOut) 106 { 107 } 108 writeOutput(const void * buffer,size_t bytes)109 ssize_t writeOutput(const void* buffer, size_t bytes) override 110 { 111 return mSpdifStreamOut->writeDataBurst(buffer, bytes); 112 } 113 protected: 114 SpdifStreamOut * const mSpdifStreamOut; 115 }; 116 117 MySPDIFEncoder mSpdifEncoder; 118 audio_config_base_t mApplicationConfig = AUDIO_CONFIG_BASE_INITIALIZER; 119 120 ssize_t writeDataBurst(const void* data, size_t bytes); 121 122 #ifdef TEE_SINK 123 NBAIO_Tee mTee; 124 #endif 125 126 }; 127 128 } // namespace android 129