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