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 "AudioStreamOut.h"
27
28#include <audio_utils/spdif/SPDIFEncoder.h>
29
30namespace 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 */
36class SpdifStreamOut : public AudioStreamOut {
37public:
38
39    SpdifStreamOut(AudioHwDevice *dev, audio_output_flags_t flags,
40            audio_format_t format);
41
42    virtual ~SpdifStreamOut() { }
43
44    virtual status_t open(
45            audio_io_handle_t handle,
46            audio_devices_t devices,
47            struct audio_config *config,
48            const char *address);
49
50    /**
51    * Write audio buffer to driver. Returns number of bytes written, or a
52    * negative status_t. If at least one frame was written 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    virtual ssize_t write(const void* buffer, size_t bytes);
64
65    /**
66     * @return frame size from the perspective of the application and the AudioFlinger.
67     */
68    virtual size_t getFrameSize() const { return sizeof(int8_t); }
69
70    /**
71     * @return format from the perspective of the application and the AudioFlinger.
72     */
73    virtual audio_format_t getFormat() const { return mApplicationFormat; }
74
75    /**
76     * The HAL may be running at a higher sample rate if, for example, playing wrapped EAC3.
77     * @return sample rate from the perspective of the application and the AudioFlinger.
78     */
79    virtual uint32_t getSampleRate() const { return mApplicationSampleRate; }
80
81    /**
82     * The HAL is in stereo mode when playing multi-channel compressed audio over HDMI.
83     * @return channel mask from the perspective of the application and the AudioFlinger.
84     */
85    virtual audio_channel_mask_t getChannelMask() const { return mApplicationChannelMask; }
86
87    virtual status_t flush();
88    virtual status_t standby();
89
90private:
91
92    class MySPDIFEncoder : public SPDIFEncoder
93    {
94    public:
95        MySPDIFEncoder(SpdifStreamOut *spdifStreamOut, audio_format_t format)
96          :  SPDIFEncoder(format)
97          , mSpdifStreamOut(spdifStreamOut)
98        {
99        }
100
101        virtual ssize_t writeOutput(const void* buffer, size_t bytes)
102        {
103            return mSpdifStreamOut->writeDataBurst(buffer, bytes);
104        }
105    protected:
106        SpdifStreamOut * const mSpdifStreamOut;
107    };
108
109    MySPDIFEncoder       mSpdifEncoder;
110    audio_format_t       mApplicationFormat;
111    uint32_t             mApplicationSampleRate;
112    audio_channel_mask_t mApplicationChannelMask;
113
114    ssize_t  writeDataBurst(const void* data, size_t bytes);
115    ssize_t  writeInternal(const void* buffer, size_t bytes);
116
117};
118
119} // namespace android
120
121#endif // ANDROID_SPDIF_STREAM_OUT_H
122