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