• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2023, 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 "AudioStreamIn.h"
26 
27 #include <audio_utils/spdif/SPDIFDecoder.h>
28 #include <afutils/NBAIO_Tee.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 SpdifStreamIn : public AudioStreamIn {
37 public:
38 
39     SpdifStreamIn(AudioHwDevice *dev, audio_input_flags_t flags,
40             audio_format_t format);
41 
42     status_t open(
43             audio_io_handle_t handle,
44             audio_devices_t devices,
45             struct audio_config *config,
46             const char *address,
47             audio_source_t source,
48             audio_devices_t outputDevice,
49             const char* outputDeviceAddress) override;
50 
51     /**
52     * Read audio buffer from driver. If at least one frame was read successfully prior to the error,
53     * it is suggested that the driver return that successful (short) byte count
54     * and then return an error in the subsequent call.
55     *
56     * If set_callback() has previously been called to enable non-blocking mode
57     * the write() is not allowed to block. It must write only the number of
58     * bytes that currently fit in the driver/hardware buffer and then return
59     * this byte count. If this is less than the requested write size the
60     * callback function must be called when more space is available in the
61     * driver/hardware buffer.
62     */
63     status_t read(void* buffer, size_t bytes, size_t* read) override;
64 
65     /**
66      * @return frame size from the perspective of the application and the AudioFlinger.
67      */
getFrameSize()68     [[nodiscard]] size_t getFrameSize() const override { return sizeof(int8_t); }
69 
70     /**
71      * @return audio_config_base_t from the perspective of the application and the AudioFlinger.
72      */
getAudioProperties()73     [[nodiscard]] audio_config_base_t getAudioProperties() const override {
74         return mApplicationConfig;
75     }
76 
77     /**
78      * @return format from the perspective of the application and the AudioFlinger.
79      */
getFormat()80     [[nodiscard]] virtual audio_format_t getFormat() const { return mApplicationConfig.format; }
81 
82     /**
83      * The HAL may be running at a higher sample rate if, for example, reading wrapped EAC3.
84      * @return sample rate from the perspective of the application and the AudioFlinger.
85      */
getSampleRate()86     [[nodiscard]] virtual uint32_t getSampleRate() const { return mApplicationConfig.sample_rate; }
87 
88     /**
89      * The HAL is in stereo mode when reading multi-channel compressed audio.
90      * @return channel mask from the perspective of the application and the AudioFlinger.
91      */
getChannelMask()92     [[nodiscard]] virtual audio_channel_mask_t getChannelMask() const {
93         return mApplicationConfig.channel_mask;
94     }
95 
96     status_t standby() override;
97 
98 private:
99 
100     class MySPDIFDecoder : public SPDIFDecoder
101     {
102     public:
MySPDIFDecoder(SpdifStreamIn * spdifStreamIn,audio_format_t format)103         MySPDIFDecoder(SpdifStreamIn *spdifStreamIn, audio_format_t format)
104           :  SPDIFDecoder(format)
105           , mSpdifStreamIn(spdifStreamIn)
106         {
107         }
108 
readInput(void * buffer,size_t bytes)109         ssize_t readInput(void* buffer, size_t bytes) override
110         {
111             size_t bytesRead = 0;
112             const auto result = mSpdifStreamIn->readDataBurst(buffer, bytes, &bytesRead);
113             if (result < 0) {
114                 return result;
115             }
116             return bytesRead;
117         }
118 
119     protected:
120         SpdifStreamIn * const mSpdifStreamIn;
121     };
122 
123     MySPDIFDecoder mSpdifDecoder;
124     audio_config_base_t mApplicationConfig = AUDIO_CONFIG_BASE_INITIALIZER;
125 
126     status_t readDataBurst(void* data, size_t bytes, size_t* read);
127 
128 #ifdef TEE_SINK
129     NBAIO_Tee mTee;
130 #endif
131 
132 };
133 
134 } // namespace android
135