blob: d689e25e1ce14db3f8f3d5cc62b39c0a23bb204e [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 Laurentcb4dae22017-07-01 19:39:32 -070021#include <media/AudioClient.h>
Eric Laurentfc235202016-12-20 18:48:17 -080022#include <utils/Errors.h>
23#include <utils/RefBase.h>
24
25namespace android {
26
27class MmapStreamCallback;
28
29class MmapStreamInterface : public virtual RefBase
30{
31 public:
32
33 /**
34 * Values for direction argument passed to openMmapStream()
35 */
36 typedef enum {
37 DIRECTION_OUTPUT = 0, /**< open a playback mmap stream */
38 DIRECTION_INPUT, /**< open a capture mmap stream */
39 } stream_direction_t;
40
Eric Laurentfc235202016-12-20 18:48:17 -080041 /**
42 * Open a playback or capture stream in MMAP mode at the audio HAL.
43 *
44 * \note This method is implemented by AudioFlinger
45 *
46 * \param[in] direction open a playback or capture stream.
47 * \param[in] attr audio attributes defining the main use case for this stream
48 * \param[in,out] config audio parameters (sampling rate, format ...) for the stream.
49 * Requested parameters as input,
50 * Actual parameters as output
Eric Laurentcb4dae22017-07-01 19:39:32 -070051 * \param[in] client a AudioClient struct describing the first client using this stream.
Eric Laurentfc235202016-12-20 18:48:17 -080052 * \param[in,out] deviceId audio device the stream should preferably be routed to/from
53 * Requested as input,
54 * Actual as output
55 * \param[in] callback the MmapStreamCallback interface used by AudioFlinger to notify
56 * condition changes affecting the stream operation
57 * \param[out] interface the MmapStreamInterface interface controlling the created stream
Eric Laurentcb4dae22017-07-01 19:39:32 -070058 * \param[out] same unique handle as the one used for the first client stream started.
Eric Laurentfc235202016-12-20 18:48:17 -080059 * \return OK if the stream was successfully created.
60 * NO_INIT if AudioFlinger is not properly initialized
61 * BAD_VALUE if the stream cannot be opened because of invalid arguments
62 * INVALID_OPERATION if the stream cannot be opened because of platform limitations
63 */
64 static status_t openMmapStream(stream_direction_t direction,
65 const audio_attributes_t *attr,
66 audio_config_base_t *config,
Eric Laurentcb4dae22017-07-01 19:39:32 -070067 const AudioClient& client,
Eric Laurentfc235202016-12-20 18:48:17 -080068 audio_port_handle_t *deviceId,
69 const sp<MmapStreamCallback>& callback,
Eric Laurentcb4dae22017-07-01 19:39:32 -070070 sp<MmapStreamInterface>& interface,
71 audio_port_handle_t *handle);
Eric Laurentfc235202016-12-20 18:48:17 -080072
73 /**
74 * Retrieve information on the mmap buffer used for audio samples transfer.
Eric Laurent18b57012017-02-13 16:23:52 -080075 * Must be called before any other method after opening the stream or entering standby.
Eric Laurentfc235202016-12-20 18:48:17 -080076 *
77 * \param[in] min_size_frames minimum buffer size requested. The actual buffer
78 * size returned in struct audio_mmap_buffer_info can be larger.
79 * \param[out] info address at which the mmap buffer information should be returned.
80 *
81 * \return OK if the buffer was allocated.
82 * NO_INIT in case of initialization error
83 * BAD_VALUE if the requested buffer size is too large
84 * INVALID_OPERATION if called out of sequence (e.g. buffer already allocated)
85 */
86 virtual status_t createMmapBuffer(int32_t minSizeFrames,
87 struct audio_mmap_buffer_info *info) = 0;
88
89 /**
90 * Read current read/write position in the mmap buffer with associated time stamp.
91 *
92 * \param[out] position address at which the mmap read/write position should be returned.
93 *
94 * \return OK if the position is successfully returned.
Eric Laurent18b57012017-02-13 16:23:52 -080095 * NO_INIT in case of initialization error
Eric Laurentfc235202016-12-20 18:48:17 -080096 * NOT_ENOUGH_DATA if the position cannot be retrieved
97 * INVALID_OPERATION if called before createMmapBuffer()
98 */
99 virtual status_t getMmapPosition(struct audio_mmap_position *position) = 0;
100
101 /**
102 * Start a stream operating in mmap mode.
103 * createMmapBuffer() must be called before calling start()
104 *
Eric Laurentcb4dae22017-07-01 19:39:32 -0700105 * \param[in] client a AudioClient struct describing the client starting on this stream.
Eric Laurentfc235202016-12-20 18:48:17 -0800106 * \param[out] handle unique handle for this instance. Used with stop().
107 * \return OK in case of success.
Eric Laurent18b57012017-02-13 16:23:52 -0800108 * NO_INIT in case of initialization error
Eric Laurentfc235202016-12-20 18:48:17 -0800109 * INVALID_OPERATION if called out of sequence
110 */
Eric Laurentcb4dae22017-07-01 19:39:32 -0700111 virtual status_t start(const AudioClient& client, audio_port_handle_t *handle) = 0;
Eric Laurentfc235202016-12-20 18:48:17 -0800112
113 /**
114 * Stop a stream operating in mmap mode.
115 * Must be called after start()
116 *
117 * \param[in] handle unique handle allocated by start().
118 * \return OK in case of success.
Eric Laurent18b57012017-02-13 16:23:52 -0800119 * NO_INIT in case of initialization error
Eric Laurentfc235202016-12-20 18:48:17 -0800120 * INVALID_OPERATION if called out of sequence
121 */
122 virtual status_t stop(audio_port_handle_t handle) = 0;
123
Eric Laurent18b57012017-02-13 16:23:52 -0800124 /**
125 * Put a stream operating in mmap mode into standby.
126 * Must be called after createMmapBuffer(). Cannot be called if any client is active.
127 * It is recommended to place a mmap stream into standby as often as possible when no client is
128 * active to save power.
129 *
130 * \return OK in case of success.
131 * NO_INIT in case of initialization error
132 * INVALID_OPERATION if called out of sequence
133 */
134 virtual status_t standby() = 0;
135
Eric Laurentfc235202016-12-20 18:48:17 -0800136 protected:
137 // Subclasses can not be constructed directly by clients.
138 MmapStreamInterface() {}
139
140 // The destructor automatically closes the stream.
141 virtual ~MmapStreamInterface() {}
142};
143
144} // namespace android
145
146#endif // ANDROID_AUDIO_MMAP_STREAM_INTERFACE_H