Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 1 | /* |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 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 | */ |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 17 | |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 18 | #pragma once |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | #include <system/audio.h> |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 24 | #include <utils/Errors.h> |
| 25 | #include <utils/RefBase.h> |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 26 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 27 | namespace android { |
| 28 | |
| 29 | class AudioHwDevice; |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 30 | class DeviceHalInterface; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 31 | class StreamOutHalInterface; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Managed access to a HAL output stream. |
| 35 | */ |
| 36 | class AudioStreamOut { |
| 37 | public: |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 38 | AudioHwDevice * const audioHwDev; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 39 | sp<StreamOutHalInterface> stream; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 40 | const audio_output_flags_t flags; |
| 41 | |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 42 | [[nodiscard]] sp<DeviceHalInterface> hwDev() const; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 43 | |
| 44 | AudioStreamOut(AudioHwDevice *dev, audio_output_flags_t flags); |
| 45 | |
| 46 | virtual status_t open( |
| 47 | audio_io_handle_t handle, |
jiabin | c010683 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 48 | audio_devices_t deviceType, |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 49 | struct audio_config *config, |
| 50 | const char *address); |
| 51 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 52 | virtual ~AudioStreamOut(); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 53 | |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 54 | // 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 Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 58 | |
| 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 Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 76 | /** |
| 77 | * @return frame size from the perspective of the application and the AudioFlinger. |
| 78 | */ |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 79 | [[nodiscard]] virtual size_t getFrameSize() const { return mHalFrameSize; } |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame] | 82 | * @return audio stream configuration: channel mask, format, sample rate: |
| 83 | * - channel mask from the perspective of the application and the AudioFlinger, |
| 84 | * The HAL is in stereo mode when playing multi-channel compressed audio over HDMI; |
| 85 | * - format from the perspective of the application and the AudioFlinger; |
| 86 | * - sample rate from the perspective of the application and the AudioFlinger, |
| 87 | * The HAL may be running at a higher sample rate if, for example, playing wrapped EAC3. |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 88 | */ |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 89 | [[nodiscard]] virtual audio_config_base_t getAudioProperties() const; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 90 | |
| 91 | virtual status_t flush(); |
| 92 | virtual status_t standby(); |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 93 | |
Atneya Nair | 0cae043 | 2022-05-10 18:12:12 -0400 | [diff] [blame] | 94 | // Avoid suppressing retrograde motion in mRenderPosition for gapless offload/direct when |
| 95 | // transitioning between tracks. |
| 96 | // The HAL resets the frame position without flush/stop being called, but calls back prior to |
| 97 | // this event. So, on the next occurrence of retrograde motion, we permit backwards movement of |
| 98 | // mRenderPosition. |
| 99 | virtual void presentationComplete() { mExpectRetrograde = true; } |
| 100 | |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 101 | protected: |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 102 | uint64_t mFramesWritten = 0; // reset by flush |
| 103 | uint64_t mFramesWrittenAtStandby = 0; |
| 104 | uint64_t mRenderPosition = 0; // reset by flush, standby, or presentation complete |
| 105 | int mRateMultiplier = 1; |
| 106 | bool mHalFormatHasProportionalFrames = false; |
| 107 | size_t mHalFrameSize = 0; |
| 108 | bool mExpectRetrograde = false; // see presentationComplete |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | } // namespace android |