blob: 1e31b6ac264ac5080cb07ae8823bd29bfde1363d [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_AUDIO_STREAM_OUT_H
19#define ANDROID_AUDIO_STREAM_OUT_H
20
21#include <stdint.h>
22#include <sys/types.h>
23
24#include <system/audio.h>
25
Phil Burk062e67a2015-02-11 13:40:50 -080026namespace android {
27
28class AudioHwDevice;
29
30/**
31 * Managed access to a HAL output stream.
32 */
33class AudioStreamOut {
34public:
35// AudioStreamOut is immutable, so its fields are const.
36// For emphasis, we could also make all pointers to them be "const *",
37// but that would clutter the code unnecessarily.
38 AudioHwDevice * const audioHwDev;
39 audio_stream_out_t *stream;
40 const audio_output_flags_t flags;
41
42 audio_hw_device_t *hwDev() const;
43
44 AudioStreamOut(AudioHwDevice *dev, audio_output_flags_t flags);
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 virtual ~AudioStreamOut() { }
53
Phil Burk90eea762015-07-06 16:24:14 -070054 // Get the bottom 32-bits of the 64-bit render position.
55 status_t getRenderPosition(uint32_t *frames);
56
57 virtual status_t getRenderPosition(uint64_t *frames);
Phil Burk062e67a2015-02-11 13:40:50 -080058
59 virtual status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp);
60
61 /**
62 * Write audio buffer to driver. Returns number of bytes written, or a
63 * negative status_t. If at least one frame was written successfully prior to the error,
64 * it is suggested that the driver return that successful (short) byte count
65 * and then return an error in the subsequent call.
66 *
67 * If set_callback() has previously been called to enable non-blocking mode
68 * the write() is not allowed to block. It must write only the number of
69 * bytes that currently fit in the driver/hardware buffer and then return
70 * this byte count. If this is less than the requested write size the
71 * callback function must be called when more space is available in the
72 * driver/hardware buffer.
73 */
74 virtual ssize_t write(const void *buffer, size_t bytes);
75
Phil Burkca5e6142015-07-14 09:42:29 -070076 /**
77 * @return frame size from the perspective of the application and the AudioFlinger.
78 */
79 virtual size_t getFrameSize() const { return mHalFrameSize; }
80
81 /**
82 * @return format from the perspective of the application and the AudioFlinger.
83 */
84 virtual audio_format_t getFormat() const;
85
86 /**
87 * The HAL may be running at a higher sample rate if, for example, playing wrapped EAC3.
88 * @return sample rate from the perspective of the application and the AudioFlinger.
89 */
90 virtual uint32_t getSampleRate() const;
91
92 /**
93 * The HAL is in stereo mode when playing multi-channel compressed audio over HDMI.
94 * @return channel mask from the perspective of the application and the AudioFlinger.
95 */
96 virtual audio_channel_mask_t getChannelMask() const;
97
Phil Burk062e67a2015-02-11 13:40:50 -080098
99 virtual status_t flush();
100 virtual status_t standby();
Phil Burk90eea762015-07-06 16:24:14 -0700101
102protected:
103 uint64_t mFramesWritten; // reset by flush
104 uint64_t mFramesWrittenAtStandby;
105 uint64_t mRenderPosition; // reset by flush or standby
106 int mRateMultiplier;
Phil Burkfdb3c072016-02-09 10:47:02 -0800107 bool mHalFormatHasProportionalFrames;
Phil Burk90eea762015-07-06 16:24:14 -0700108 size_t mHalFrameSize;
Phil Burk062e67a2015-02-11 13:40:50 -0800109};
110
111} // namespace android
112
113#endif // ANDROID_AUDIO_STREAM_OUT_H