Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 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 | #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 Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 26 | namespace android { |
| 27 | |
| 28 | class AudioHwDevice; |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 29 | class DeviceHalInterface; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 30 | class StreamOutHalInterface; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Managed access to a HAL output stream. |
| 34 | */ |
| 35 | class AudioStreamOut { |
| 36 | public: |
| 37 | // AudioStreamOut is immutable, so its fields are const. |
| 38 | // For emphasis, we could also make all pointers to them be "const *", |
| 39 | // but that would clutter the code unnecessarily. |
| 40 | AudioHwDevice * const audioHwDev; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 41 | sp<StreamOutHalInterface> stream; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 42 | const audio_output_flags_t flags; |
| 43 | |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 44 | sp<DeviceHalInterface> hwDev() const; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 45 | |
| 46 | AudioStreamOut(AudioHwDevice *dev, audio_output_flags_t flags); |
| 47 | |
| 48 | virtual status_t open( |
| 49 | audio_io_handle_t handle, |
jiabin | c010683 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 50 | audio_devices_t deviceType, |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 51 | struct audio_config *config, |
| 52 | const char *address); |
| 53 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 54 | virtual ~AudioStreamOut(); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 55 | |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 56 | // Get the bottom 32-bits of the 64-bit render position. |
| 57 | status_t getRenderPosition(uint32_t *frames); |
| 58 | |
| 59 | virtual status_t getRenderPosition(uint64_t *frames); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 60 | |
| 61 | virtual status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp); |
| 62 | |
| 63 | /** |
| 64 | * Write audio buffer to driver. Returns number of bytes written, or a |
| 65 | * negative status_t. If at least one frame was written successfully prior to the error, |
| 66 | * it is suggested that the driver return that successful (short) byte count |
| 67 | * and then return an error in the subsequent call. |
| 68 | * |
| 69 | * If set_callback() has previously been called to enable non-blocking mode |
| 70 | * the write() is not allowed to block. It must write only the number of |
| 71 | * bytes that currently fit in the driver/hardware buffer and then return |
| 72 | * this byte count. If this is less than the requested write size the |
| 73 | * callback function must be called when more space is available in the |
| 74 | * driver/hardware buffer. |
| 75 | */ |
| 76 | virtual ssize_t write(const void *buffer, size_t bytes); |
| 77 | |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 78 | /** |
| 79 | * @return frame size from the perspective of the application and the AudioFlinger. |
| 80 | */ |
| 81 | virtual size_t getFrameSize() const { return mHalFrameSize; } |
| 82 | |
| 83 | /** |
Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame^] | 84 | * @return audio stream configuration: channel mask, format, sample rate: |
| 85 | * - channel mask from the perspective of the application and the AudioFlinger, |
| 86 | * The HAL is in stereo mode when playing multi-channel compressed audio over HDMI; |
| 87 | * - format from the perspective of the application and the AudioFlinger; |
| 88 | * - sample rate from the perspective of the application and the AudioFlinger, |
| 89 | * 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] | 90 | */ |
Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame^] | 91 | virtual audio_config_base_t getAudioProperties() const; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 92 | |
| 93 | virtual status_t flush(); |
| 94 | virtual status_t standby(); |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 95 | |
| 96 | protected: |
| 97 | uint64_t mFramesWritten; // reset by flush |
| 98 | uint64_t mFramesWrittenAtStandby; |
| 99 | uint64_t mRenderPosition; // reset by flush or standby |
| 100 | int mRateMultiplier; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 101 | bool mHalFormatHasProportionalFrames; |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 102 | size_t mHalFrameSize; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | } // namespace android |
| 106 | |
| 107 | #endif // ANDROID_AUDIO_STREAM_OUT_H |