1062e67a26e0553dd142be622821f493df541f0c6Phil Burk/*
2062e67a26e0553dd142be622821f493df541f0c6Phil Burk**
3062e67a26e0553dd142be622821f493df541f0c6Phil Burk** Copyright 2015, The Android Open Source Project
4062e67a26e0553dd142be622821f493df541f0c6Phil Burk**
5062e67a26e0553dd142be622821f493df541f0c6Phil Burk** Licensed under the Apache License, Version 2.0 (the "License");
6062e67a26e0553dd142be622821f493df541f0c6Phil Burk** you may not use this file except in compliance with the License.
7062e67a26e0553dd142be622821f493df541f0c6Phil Burk** You may obtain a copy of the License at
8062e67a26e0553dd142be622821f493df541f0c6Phil Burk**
9062e67a26e0553dd142be622821f493df541f0c6Phil Burk**     http://www.apache.org/licenses/LICENSE-2.0
10062e67a26e0553dd142be622821f493df541f0c6Phil Burk**
11062e67a26e0553dd142be622821f493df541f0c6Phil Burk** Unless required by applicable law or agreed to in writing, software
12062e67a26e0553dd142be622821f493df541f0c6Phil Burk** distributed under the License is distributed on an "AS IS" BASIS,
13062e67a26e0553dd142be622821f493df541f0c6Phil Burk** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14062e67a26e0553dd142be622821f493df541f0c6Phil Burk** See the License for the specific language governing permissions and
15062e67a26e0553dd142be622821f493df541f0c6Phil Burk** limitations under the License.
16062e67a26e0553dd142be622821f493df541f0c6Phil Burk*/
17062e67a26e0553dd142be622821f493df541f0c6Phil Burk
18062e67a26e0553dd142be622821f493df541f0c6Phil Burk#ifndef ANDROID_SPDIF_STREAM_OUT_H
19062e67a26e0553dd142be622821f493df541f0c6Phil Burk#define ANDROID_SPDIF_STREAM_OUT_H
20062e67a26e0553dd142be622821f493df541f0c6Phil Burk
21062e67a26e0553dd142be622821f493df541f0c6Phil Burk#include <stdint.h>
22062e67a26e0553dd142be622821f493df541f0c6Phil Burk#include <sys/types.h>
23062e67a26e0553dd142be622821f493df541f0c6Phil Burk
24062e67a26e0553dd142be622821f493df541f0c6Phil Burk#include <system/audio.h>
25062e67a26e0553dd142be622821f493df541f0c6Phil Burk
26062e67a26e0553dd142be622821f493df541f0c6Phil Burk#include "AudioHwDevice.h"
27062e67a26e0553dd142be622821f493df541f0c6Phil Burk#include "AudioStreamOut.h"
28062e67a26e0553dd142be622821f493df541f0c6Phil Burk#include "SpdifStreamOut.h"
29062e67a26e0553dd142be622821f493df541f0c6Phil Burk
30062e67a26e0553dd142be622821f493df541f0c6Phil Burk#include <audio_utils/spdif/SPDIFEncoder.h>
31062e67a26e0553dd142be622821f493df541f0c6Phil Burk
32062e67a26e0553dd142be622821f493df541f0c6Phil Burknamespace android {
33062e67a26e0553dd142be622821f493df541f0c6Phil Burk
34062e67a26e0553dd142be622821f493df541f0c6Phil Burk/**
35062e67a26e0553dd142be622821f493df541f0c6Phil Burk * Stream that is a PCM data burst in the HAL but looks like an encoded stream
36062e67a26e0553dd142be622821f493df541f0c6Phil Burk * to the AudioFlinger. Wraps encoded data in an SPDIF wrapper per IEC61973-3.
37062e67a26e0553dd142be622821f493df541f0c6Phil Burk */
38062e67a26e0553dd142be622821f493df541f0c6Phil Burkclass SpdifStreamOut : public AudioStreamOut {
39062e67a26e0553dd142be622821f493df541f0c6Phil Burkpublic:
40062e67a26e0553dd142be622821f493df541f0c6Phil Burk
4123d8997f58bb9c59fa3a1b9a6b2edbf1b2b0f4c6Phil Burk    SpdifStreamOut(AudioHwDevice *dev, audio_output_flags_t flags,
4223d8997f58bb9c59fa3a1b9a6b2edbf1b2b0f4c6Phil Burk            audio_format_t format);
43062e67a26e0553dd142be622821f493df541f0c6Phil Burk
44062e67a26e0553dd142be622821f493df541f0c6Phil Burk    virtual ~SpdifStreamOut() { }
45062e67a26e0553dd142be622821f493df541f0c6Phil Burk
46062e67a26e0553dd142be622821f493df541f0c6Phil Burk    virtual status_t open(
47062e67a26e0553dd142be622821f493df541f0c6Phil Burk            audio_io_handle_t handle,
48062e67a26e0553dd142be622821f493df541f0c6Phil Burk            audio_devices_t devices,
49062e67a26e0553dd142be622821f493df541f0c6Phil Burk            struct audio_config *config,
50062e67a26e0553dd142be622821f493df541f0c6Phil Burk            const char *address);
51062e67a26e0553dd142be622821f493df541f0c6Phil Burk
52062e67a26e0553dd142be622821f493df541f0c6Phil Burk    /**
53062e67a26e0553dd142be622821f493df541f0c6Phil Burk    * Write audio buffer to driver. Returns number of bytes written, or a
54062e67a26e0553dd142be622821f493df541f0c6Phil Burk    * negative status_t. If at least one frame was written successfully prior to the error,
55062e67a26e0553dd142be622821f493df541f0c6Phil Burk    * it is suggested that the driver return that successful (short) byte count
56062e67a26e0553dd142be622821f493df541f0c6Phil Burk    * and then return an error in the subsequent call.
57062e67a26e0553dd142be622821f493df541f0c6Phil Burk    *
58062e67a26e0553dd142be622821f493df541f0c6Phil Burk    * If set_callback() has previously been called to enable non-blocking mode
59062e67a26e0553dd142be622821f493df541f0c6Phil Burk    * the write() is not allowed to block. It must write only the number of
60062e67a26e0553dd142be622821f493df541f0c6Phil Burk    * bytes that currently fit in the driver/hardware buffer and then return
61062e67a26e0553dd142be622821f493df541f0c6Phil Burk    * this byte count. If this is less than the requested write size the
62062e67a26e0553dd142be622821f493df541f0c6Phil Burk    * callback function must be called when more space is available in the
63062e67a26e0553dd142be622821f493df541f0c6Phil Burk    * driver/hardware buffer.
64062e67a26e0553dd142be622821f493df541f0c6Phil Burk    */
65062e67a26e0553dd142be622821f493df541f0c6Phil Burk    virtual ssize_t write(const void* buffer, size_t bytes);
66062e67a26e0553dd142be622821f493df541f0c6Phil Burk
67ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk    /**
68ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk     * @return frame size from the perspective of the application and the AudioFlinger.
69ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk     */
70ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk    virtual size_t getFrameSize() const { return sizeof(int8_t); }
71ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk
72ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk    /**
73ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk     * @return format from the perspective of the application and the AudioFlinger.
74ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk     */
75ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk    virtual audio_format_t getFormat() const { return mApplicationFormat; }
76ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk
77ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk    /**
78ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk     * The HAL may be running at a higher sample rate if, for example, playing wrapped EAC3.
79ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk     * @return sample rate from the perspective of the application and the AudioFlinger.
80ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk     */
81ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk    virtual uint32_t getSampleRate() const { return mApplicationSampleRate; }
82ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk
83ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk    /**
84ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk     * The HAL is in stereo mode when playing multi-channel compressed audio over HDMI.
85ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk     * @return channel mask from the perspective of the application and the AudioFlinger.
86ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk     */
87ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk    virtual audio_channel_mask_t getChannelMask() const { return mApplicationChannelMask; }
88062e67a26e0553dd142be622821f493df541f0c6Phil Burk
89062e67a26e0553dd142be622821f493df541f0c6Phil Burk    virtual status_t flush();
90062e67a26e0553dd142be622821f493df541f0c6Phil Burk    virtual status_t standby();
91062e67a26e0553dd142be622821f493df541f0c6Phil Burk
92062e67a26e0553dd142be622821f493df541f0c6Phil Burkprivate:
93062e67a26e0553dd142be622821f493df541f0c6Phil Burk
94062e67a26e0553dd142be622821f493df541f0c6Phil Burk    class MySPDIFEncoder : public SPDIFEncoder
95062e67a26e0553dd142be622821f493df541f0c6Phil Burk    {
96062e67a26e0553dd142be622821f493df541f0c6Phil Burk    public:
9723d8997f58bb9c59fa3a1b9a6b2edbf1b2b0f4c6Phil Burk        MySPDIFEncoder(SpdifStreamOut *spdifStreamOut, audio_format_t format)
9823d8997f58bb9c59fa3a1b9a6b2edbf1b2b0f4c6Phil Burk          :  SPDIFEncoder(format)
9923d8997f58bb9c59fa3a1b9a6b2edbf1b2b0f4c6Phil Burk          , mSpdifStreamOut(spdifStreamOut)
100062e67a26e0553dd142be622821f493df541f0c6Phil Burk        {
101062e67a26e0553dd142be622821f493df541f0c6Phil Burk        }
102062e67a26e0553dd142be622821f493df541f0c6Phil Burk
103062e67a26e0553dd142be622821f493df541f0c6Phil Burk        virtual ssize_t writeOutput(const void* buffer, size_t bytes)
104062e67a26e0553dd142be622821f493df541f0c6Phil Burk        {
105062e67a26e0553dd142be622821f493df541f0c6Phil Burk            return mSpdifStreamOut->writeDataBurst(buffer, bytes);
106062e67a26e0553dd142be622821f493df541f0c6Phil Burk        }
107062e67a26e0553dd142be622821f493df541f0c6Phil Burk    protected:
108062e67a26e0553dd142be622821f493df541f0c6Phil Burk        SpdifStreamOut * const mSpdifStreamOut;
109062e67a26e0553dd142be622821f493df541f0c6Phil Burk    };
110062e67a26e0553dd142be622821f493df541f0c6Phil Burk
111062e67a26e0553dd142be622821f493df541f0c6Phil Burk    MySPDIFEncoder       mSpdifEncoder;
112ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk    audio_format_t       mApplicationFormat;
113ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk    uint32_t             mApplicationSampleRate;
114ca5e6143740299c877d69e97f7968cd04476d32cPhil Burk    audio_channel_mask_t mApplicationChannelMask;
115062e67a26e0553dd142be622821f493df541f0c6Phil Burk
116062e67a26e0553dd142be622821f493df541f0c6Phil Burk    ssize_t  writeDataBurst(const void* data, size_t bytes);
117062e67a26e0553dd142be622821f493df541f0c6Phil Burk    ssize_t  writeInternal(const void* buffer, size_t bytes);
118062e67a26e0553dd142be622821f493df541f0c6Phil Burk
119062e67a26e0553dd142be622821f493df541f0c6Phil Burk};
120062e67a26e0553dd142be622821f493df541f0c6Phil Burk
121062e67a26e0553dd142be622821f493df541f0c6Phil Burk} // namespace android
122062e67a26e0553dd142be622821f493df541f0c6Phil Burk
123062e67a26e0553dd142be622821f493df541f0c6Phil Burk#endif // ANDROID_SPDIF_STREAM_OUT_H
124