xref: /aosp_15_r20/frameworks/av/services/audioflinger/datapath/AudioStreamOut.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
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 #pragma once
19 
20 #include <stdint.h>
21 #include <sys/types.h>
22 
23 #include <system/audio.h>
24 #include <utils/Errors.h>
25 #include <utils/RefBase.h>
26 
27 namespace android {
28 
29 class AudioHwDevice;
30 class DeviceHalInterface;
31 class StreamOutHalInterface;
32 
33 /**
34  * Managed access to a HAL output stream.
35  */
36 class AudioStreamOut {
37 public:
38     AudioHwDevice * const audioHwDev;
39     sp<StreamOutHalInterface> stream;
40     audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE;
41 
42     [[nodiscard]] sp<DeviceHalInterface> hwDev() const;
43 
44     explicit AudioStreamOut(AudioHwDevice *dev);
45 
46     virtual status_t open(
47             audio_io_handle_t handle,
48             audio_devices_t deviceType,
49             struct audio_config *config,
50             audio_output_flags_t *flagsPtr,
51             const char *address,
52             const std::vector<playback_track_metadata_v7_t>& sourceMetadata);
53 
54     virtual ~AudioStreamOut();
55 
56     virtual status_t getRenderPosition(uint64_t *frames);
57 
58     virtual status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp);
59 
60     /**
61     * Write audio buffer to driver. Returns number of bytes written, or a
62     * negative status_t. If at least one frame was written successfully prior to the error,
63     * it is suggested that the driver return that successful (short) byte count
64     * and then return an error in the subsequent call.
65     *
66     * If set_callback() has previously been called to enable non-blocking mode
67     * the write() is not allowed to block. It must write only the number of
68     * bytes that currently fit in the driver/hardware buffer and then return
69     * this byte count. If this is less than the requested write size the
70     * callback function must be called when more space is available in the
71     * driver/hardware buffer.
72     */
73     virtual ssize_t write(const void *buffer, size_t bytes);
74 
75     /**
76      * @return frame size from the perspective of the application and the AudioFlinger.
77      */
getFrameSize()78     [[nodiscard]] virtual size_t getFrameSize() const { return mHalFrameSize; }
79 
80     /**
81      * @return audio stream configuration: channel mask, format, sample rate:
82      *   - channel mask from the perspective of the application and the AudioFlinger,
83      *     The HAL is in stereo mode when playing multi-channel compressed audio over HDMI;
84      *   - format from the perspective of the application and the AudioFlinger;
85      *   - sample rate from the perspective of the application and the AudioFlinger,
86      *     The HAL may be running at a higher sample rate if, for example, playing wrapped EAC3.
87      */
88     [[nodiscard]] virtual audio_config_base_t getAudioProperties() const;
89 
90     virtual status_t flush();
91     virtual status_t standby();
92 
93     virtual void presentationComplete();
94 
95 protected:
96     uint64_t mFramesWritten = 0; // reset by flush
97     uint64_t mFramesWrittenAtStandby = 0;
98     int mRateMultiplier = 1;
99     bool mHalFormatHasProportionalFrames = false;
100     size_t mHalFrameSize = 0;
101 };
102 
103 } // namespace android
104