blob: 3d293351006f18cac7f2157ccb579880c003fbe4 [file] [log] [blame]
Eric Laurentfc235202016-12-20 18:48:17 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_AUDIO_MMAP_STREAM_INTERFACE_H
18#define ANDROID_AUDIO_MMAP_STREAM_INTERFACE_H
19
20#include <system/audio.h>
Eric Laurenta54f1282017-07-01 19:39:32 -070021#include <media/AudioClient.h>
Robert Wuaeb1d002024-10-30 23:19:44 +000022#include <media/AudioContainers.h>
Eric Laurentfc235202016-12-20 18:48:17 -080023#include <utils/Errors.h>
24#include <utils/RefBase.h>
25
jiabinb7d8c5a2020-08-26 17:24:52 -070026#include <time.h>
27
Eric Laurentfc235202016-12-20 18:48:17 -080028namespace android {
29
30class MmapStreamCallback;
31
32class MmapStreamInterface : public virtual RefBase
33{
34 public:
35
36 /**
37 * Values for direction argument passed to openMmapStream()
38 */
39 typedef enum {
40 DIRECTION_OUTPUT = 0, /**< open a playback mmap stream */
41 DIRECTION_INPUT, /**< open a capture mmap stream */
42 } stream_direction_t;
43
Eric Laurentfc235202016-12-20 18:48:17 -080044 /**
45 * Open a playback or capture stream in MMAP mode at the audio HAL.
46 *
47 * \note This method is implemented by AudioFlinger
48 *
49 * \param[in] direction open a playback or capture stream.
50 * \param[in] attr audio attributes defining the main use case for this stream
51 * \param[in,out] config audio parameters (sampling rate, format ...) for the stream.
52 * Requested parameters as input,
53 * Actual parameters as output
Eric Laurenta54f1282017-07-01 19:39:32 -070054 * \param[in] client a AudioClient struct describing the first client using this stream.
Robert Wuaeb1d002024-10-30 23:19:44 +000055 * \param[in,out] deviceIds audio devices the stream should preferably be routed to/from.
56 * Leave empty if there are no preferred devices.
57 * Requested as input,
58 * Actual as output
Phil Burk4e1af9f2018-01-03 15:54:35 -080059 * \param[in,out] sessionId audio sessionId for the stream
60 * Requested as input, may be AUDIO_SESSION_ALLOCATE
61 * Actual as output
Eric Laurentfc235202016-12-20 18:48:17 -080062 * \param[in] callback the MmapStreamCallback interface used by AudioFlinger to notify
63 * condition changes affecting the stream operation
64 * \param[out] interface the MmapStreamInterface interface controlling the created stream
Eric Laurenta54f1282017-07-01 19:39:32 -070065 * \param[out] same unique handle as the one used for the first client stream started.
Eric Laurentfc235202016-12-20 18:48:17 -080066 * \return OK if the stream was successfully created.
67 * NO_INIT if AudioFlinger is not properly initialized
68 * BAD_VALUE if the stream cannot be opened because of invalid arguments
69 * INVALID_OPERATION if the stream cannot be opened because of platform limitations
70 */
71 static status_t openMmapStream(stream_direction_t direction,
72 const audio_attributes_t *attr,
73 audio_config_base_t *config,
Eric Laurenta54f1282017-07-01 19:39:32 -070074 const AudioClient& client,
Robert Wuaeb1d002024-10-30 23:19:44 +000075 DeviceIdVector *deviceIds,
Phil Burk4e1af9f2018-01-03 15:54:35 -080076 audio_session_t *sessionId,
Eric Laurentfc235202016-12-20 18:48:17 -080077 const sp<MmapStreamCallback>& callback,
Eric Laurenta54f1282017-07-01 19:39:32 -070078 sp<MmapStreamInterface>& interface,
79 audio_port_handle_t *handle);
Eric Laurentfc235202016-12-20 18:48:17 -080080
81 /**
82 * Retrieve information on the mmap buffer used for audio samples transfer.
Eric Laurent18b57012017-02-13 16:23:52 -080083 * Must be called before any other method after opening the stream or entering standby.
Eric Laurentfc235202016-12-20 18:48:17 -080084 *
85 * \param[in] min_size_frames minimum buffer size requested. The actual buffer
86 * size returned in struct audio_mmap_buffer_info can be larger.
87 * \param[out] info address at which the mmap buffer information should be returned.
88 *
89 * \return OK if the buffer was allocated.
90 * NO_INIT in case of initialization error
91 * BAD_VALUE if the requested buffer size is too large
92 * INVALID_OPERATION if called out of sequence (e.g. buffer already allocated)
93 */
94 virtual status_t createMmapBuffer(int32_t minSizeFrames,
95 struct audio_mmap_buffer_info *info) = 0;
96
97 /**
98 * Read current read/write position in the mmap buffer with associated time stamp.
99 *
100 * \param[out] position address at which the mmap read/write position should be returned.
101 *
102 * \return OK if the position is successfully returned.
Eric Laurent18b57012017-02-13 16:23:52 -0800103 * NO_INIT in case of initialization error
Eric Laurentfc235202016-12-20 18:48:17 -0800104 * NOT_ENOUGH_DATA if the position cannot be retrieved
105 * INVALID_OPERATION if called before createMmapBuffer()
106 */
107 virtual status_t getMmapPosition(struct audio_mmap_position *position) = 0;
108
109 /**
jiabinb7d8c5a2020-08-26 17:24:52 -0700110 * Get a recent count of the number of audio frames presented/received to/from an
111 * external observer.
112 *
113 * \param[out] position count of presented audio frames
114 * \param[out] timeNanos associated clock time
115 *
116 * \return OK if the external position is set correctly.
117 * NO_INIT in case of initialization error
118 * INVALID_OPERATION if the interface is not implemented
119 */
120 virtual status_t getExternalPosition(uint64_t* position, int64_t* timeNanos) = 0;
121
122 /**
Eric Laurentfc235202016-12-20 18:48:17 -0800123 * Start a stream operating in mmap mode.
124 * createMmapBuffer() must be called before calling start()
125 *
Eric Laurenta54f1282017-07-01 19:39:32 -0700126 * \param[in] client a AudioClient struct describing the client starting on this stream.
jiabind1f1cb62020-03-24 11:57:57 -0700127 * \param[in] attr audio attributes provided by the client.
Eric Laurentfc235202016-12-20 18:48:17 -0800128 * \param[out] handle unique handle for this instance. Used with stop().
129 * \return OK in case of success.
Eric Laurent18b57012017-02-13 16:23:52 -0800130 * NO_INIT in case of initialization error
Eric Laurentfc235202016-12-20 18:48:17 -0800131 * INVALID_OPERATION if called out of sequence
132 */
jiabind1f1cb62020-03-24 11:57:57 -0700133 virtual status_t start(const AudioClient& client,
134 const audio_attributes_t *attr,
135 audio_port_handle_t *handle) = 0;
Eric Laurentfc235202016-12-20 18:48:17 -0800136
137 /**
138 * Stop a stream operating in mmap mode.
139 * Must be called after start()
140 *
141 * \param[in] handle unique handle allocated by start().
142 * \return OK in case of success.
Eric Laurent18b57012017-02-13 16:23:52 -0800143 * NO_INIT in case of initialization error
Eric Laurentfc235202016-12-20 18:48:17 -0800144 * INVALID_OPERATION if called out of sequence
145 */
146 virtual status_t stop(audio_port_handle_t handle) = 0;
147
Eric Laurent18b57012017-02-13 16:23:52 -0800148 /**
149 * Put a stream operating in mmap mode into standby.
150 * Must be called after createMmapBuffer(). Cannot be called if any client is active.
151 * It is recommended to place a mmap stream into standby as often as possible when no client is
152 * active to save power.
153 *
154 * \return OK in case of success.
155 * NO_INIT in case of initialization error
156 * INVALID_OPERATION if called out of sequence
157 */
158 virtual status_t standby() = 0;
159
jiabinfc791ee2023-02-15 19:43:40 +0000160 /**
161 * Report when data being written to a playback buffer. Currently, this is used by mmap
162 * playback thread for sound dose computation.
163 *
164 * \param[in] buffer a pointer to the audio data
165 * \param[in] frameCount the number of frames written by the CPU
166 * \return OK in case of success.
167 * NO_INIT in case of initialization error
168 * INVALID_OPERATION in case of wrong thread type
169 */
170 virtual status_t reportData(const void* buffer, size_t frameCount) = 0;
171
Eric Laurentfc235202016-12-20 18:48:17 -0800172 protected:
173 // Subclasses can not be constructed directly by clients.
174 MmapStreamInterface() {}
175
176 // The destructor automatically closes the stream.
177 virtual ~MmapStreamInterface() {}
178};
179
180} // namespace android
181
182#endif // ANDROID_AUDIO_MMAP_STREAM_INTERFACE_H