blob: 2c9fb3ead6adcb73c55ce0768be5189cc7cd31b1 [file] [log] [blame]
Phil Burk062e67a2015-02-11 13:40:50 -08001/*
Dean Wheatley6c009512023-10-23 09:34:14 +11002 *
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 Burk062e67a2015-02-11 13:40:50 -080017
Dean Wheatley6c009512023-10-23 09:34:14 +110018#pragma once
Phil Burk062e67a2015-02-11 13:40:50 -080019
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <system/audio.h>
Dean Wheatley6c009512023-10-23 09:34:14 +110024#include <utils/Errors.h>
25#include <utils/RefBase.h>
Phil Burk062e67a2015-02-11 13:40:50 -080026
Phil Burk062e67a2015-02-11 13:40:50 -080027namespace android {
28
29class AudioHwDevice;
Mikhail Naganove4f1f632016-08-31 11:35:10 -070030class DeviceHalInterface;
Mikhail Naganov1dc98672016-08-18 17:50:29 -070031class StreamOutHalInterface;
Phil Burk062e67a2015-02-11 13:40:50 -080032
33/**
34 * Managed access to a HAL output stream.
35 */
36class AudioStreamOut {
37public:
Phil Burk062e67a2015-02-11 13:40:50 -080038 AudioHwDevice * const audioHwDev;
Mikhail Naganov1dc98672016-08-18 17:50:29 -070039 sp<StreamOutHalInterface> stream;
Phil Burk062e67a2015-02-11 13:40:50 -080040 const audio_output_flags_t flags;
41
Andy Hung1ef77382023-06-15 14:50:18 -070042 [[nodiscard]] sp<DeviceHalInterface> hwDev() const;
Phil Burk062e67a2015-02-11 13:40:50 -080043
44 AudioStreamOut(AudioHwDevice *dev, audio_output_flags_t flags);
45
46 virtual status_t open(
47 audio_io_handle_t handle,
jiabinc0106832019-10-24 14:58:31 -070048 audio_devices_t deviceType,
Phil Burk062e67a2015-02-11 13:40:50 -080049 struct audio_config *config,
50 const char *address);
51
Mikhail Naganov1dc98672016-08-18 17:50:29 -070052 virtual ~AudioStreamOut();
Phil Burk062e67a2015-02-11 13:40:50 -080053
Phil Burk90eea762015-07-06 16:24:14 -070054 virtual status_t getRenderPosition(uint64_t *frames);
Phil Burk062e67a2015-02-11 13:40:50 -080055
56 virtual status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp);
57
58 /**
59 * Write audio buffer to driver. Returns number of bytes written, or a
60 * negative status_t. If at least one frame was written successfully prior to the error,
61 * it is suggested that the driver return that successful (short) byte count
62 * and then return an error in the subsequent call.
63 *
64 * If set_callback() has previously been called to enable non-blocking mode
65 * the write() is not allowed to block. It must write only the number of
66 * bytes that currently fit in the driver/hardware buffer and then return
67 * this byte count. If this is less than the requested write size the
68 * callback function must be called when more space is available in the
69 * driver/hardware buffer.
70 */
71 virtual ssize_t write(const void *buffer, size_t bytes);
72
Phil Burkca5e6142015-07-14 09:42:29 -070073 /**
74 * @return frame size from the perspective of the application and the AudioFlinger.
75 */
Andy Hung1ef77382023-06-15 14:50:18 -070076 [[nodiscard]] virtual size_t getFrameSize() const { return mHalFrameSize; }
Phil Burkca5e6142015-07-14 09:42:29 -070077
78 /**
Mikhail Naganov560637e2021-03-31 22:40:13 +000079 * @return audio stream configuration: channel mask, format, sample rate:
80 * - channel mask from the perspective of the application and the AudioFlinger,
81 * The HAL is in stereo mode when playing multi-channel compressed audio over HDMI;
82 * - format from the perspective of the application and the AudioFlinger;
83 * - sample rate from the perspective of the application and the AudioFlinger,
84 * The HAL may be running at a higher sample rate if, for example, playing wrapped EAC3.
Phil Burkca5e6142015-07-14 09:42:29 -070085 */
Andy Hung1ef77382023-06-15 14:50:18 -070086 [[nodiscard]] virtual audio_config_base_t getAudioProperties() const;
Phil Burk062e67a2015-02-11 13:40:50 -080087
88 virtual status_t flush();
89 virtual status_t standby();
Phil Burk90eea762015-07-06 16:24:14 -070090
Mikhail Naganov0ea58fe2024-05-10 13:30:40 -070091 virtual void presentationComplete();
Atneya Nair0cae0432022-05-10 18:12:12 -040092
Phil Burk90eea762015-07-06 16:24:14 -070093protected:
Dean Wheatley6c009512023-10-23 09:34:14 +110094 uint64_t mFramesWritten = 0; // reset by flush
95 uint64_t mFramesWrittenAtStandby = 0;
Dean Wheatley6c009512023-10-23 09:34:14 +110096 int mRateMultiplier = 1;
97 bool mHalFormatHasProportionalFrames = false;
98 size_t mHalFrameSize = 0;
Phil Burk062e67a2015-02-11 13:40:50 -080099};
100
101} // namespace android