| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 1 | /* | 
 | 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 Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 21 | #include <media/AudioClient.h> | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 22 | #include <utils/Errors.h> | 
 | 23 | #include <utils/RefBase.h> | 
 | 24 |  | 
| jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 25 | #include <time.h> | 
 | 26 |  | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 27 | namespace android { | 
 | 28 |  | 
 | 29 | class MmapStreamCallback; | 
 | 30 |  | 
 | 31 | class MmapStreamInterface : public virtual RefBase | 
 | 32 | { | 
 | 33 |   public: | 
 | 34 |  | 
 | 35 |     /** | 
 | 36 |      * Values for direction argument passed to openMmapStream() | 
 | 37 |      */ | 
 | 38 |     typedef enum { | 
 | 39 |         DIRECTION_OUTPUT = 0,  /**< open a playback mmap stream */ | 
 | 40 |         DIRECTION_INPUT,       /**< open a capture mmap stream */ | 
 | 41 |     } stream_direction_t; | 
 | 42 |  | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 43 |     /** | 
 | 44 |      * Open a playback or capture stream in MMAP mode at the audio HAL. | 
 | 45 |      * | 
 | 46 |      * \note This method is implemented by AudioFlinger | 
 | 47 |      * | 
 | 48 |      * \param[in] direction open a playback or capture stream. | 
 | 49 |      * \param[in] attr audio attributes defining the main use case for this stream | 
 | 50 |      * \param[in,out] config audio parameters (sampling rate, format ...) for the stream. | 
 | 51 |      *                       Requested parameters as input, | 
 | 52 |      *                       Actual parameters as output | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 53 |      * \param[in] client a AudioClient struct describing the first client using this stream. | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 54 |      * \param[in,out] deviceId audio device the stream should preferably be routed to/from | 
 | 55 |      *                       Requested as input, | 
 | 56 |      *                       Actual as output | 
| Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 57 |      * \param[in,out] sessionId audio sessionId for the stream | 
 | 58 |      *                       Requested as input, may be AUDIO_SESSION_ALLOCATE | 
 | 59 |      *                       Actual as output | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 60 |      * \param[in] callback the MmapStreamCallback interface used by AudioFlinger to notify | 
 | 61 |      *                     condition changes affecting the stream operation | 
 | 62 |      * \param[out] interface the MmapStreamInterface interface controlling the created stream | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 63 |      * \param[out] same unique handle as the one used for the first client stream started. | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 64 |      * \return OK if the stream was successfully created. | 
 | 65 |      *         NO_INIT if AudioFlinger is not properly initialized | 
 | 66 |      *         BAD_VALUE if the stream cannot be opened because of invalid arguments | 
 | 67 |      *         INVALID_OPERATION if the stream cannot be opened because of platform limitations | 
 | 68 |      */ | 
 | 69 |     static status_t openMmapStream(stream_direction_t direction, | 
 | 70 |                                            const audio_attributes_t *attr, | 
 | 71 |                                            audio_config_base_t *config, | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 72 |                                            const AudioClient& client, | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 73 |                                            audio_port_handle_t *deviceId, | 
| Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 74 |                                            audio_session_t *sessionId, | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 75 |                                            const sp<MmapStreamCallback>& callback, | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 76 |                                            sp<MmapStreamInterface>& interface, | 
 | 77 |                                            audio_port_handle_t *handle); | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 78 |  | 
 | 79 |     /** | 
 | 80 |      * Retrieve information on the mmap buffer used for audio samples transfer. | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 81 |      * Must be called before any other method after opening the stream or entering standby. | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 82 |      * | 
 | 83 |      * \param[in] min_size_frames minimum buffer size requested. The actual buffer | 
 | 84 |      *        size returned in struct audio_mmap_buffer_info can be larger. | 
 | 85 |      * \param[out] info address at which the mmap buffer information should be returned. | 
 | 86 |      * | 
 | 87 |      * \return OK if the buffer was allocated. | 
 | 88 |      *         NO_INIT in case of initialization error | 
 | 89 |      *         BAD_VALUE if the requested buffer size is too large | 
 | 90 |      *         INVALID_OPERATION if called out of sequence (e.g. buffer already allocated) | 
 | 91 |      */ | 
 | 92 |     virtual status_t createMmapBuffer(int32_t minSizeFrames, | 
 | 93 |                                       struct audio_mmap_buffer_info *info) = 0; | 
 | 94 |  | 
 | 95 |     /** | 
 | 96 |      * Read current read/write position in the mmap buffer with associated time stamp. | 
 | 97 |      * | 
 | 98 |      * \param[out] position address at which the mmap read/write position should be returned. | 
 | 99 |      * | 
 | 100 |      * \return OK if the position is successfully returned. | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 101 |      *         NO_INIT in case of initialization error | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 102 |      *         NOT_ENOUGH_DATA if the position cannot be retrieved | 
 | 103 |      *         INVALID_OPERATION if called before createMmapBuffer() | 
 | 104 |      */ | 
 | 105 |     virtual status_t getMmapPosition(struct audio_mmap_position *position) = 0; | 
 | 106 |  | 
 | 107 |     /** | 
| jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 108 |      * Get a recent count of the number of audio frames presented/received to/from an | 
 | 109 |      * external observer. | 
 | 110 |      * | 
 | 111 |      * \param[out] position count of presented audio frames | 
 | 112 |      * \param[out] timeNanos associated clock time | 
 | 113 |      * | 
 | 114 |      * \return OK if the external position is set correctly. | 
 | 115 |      *         NO_INIT in case of initialization error | 
 | 116 |      *         INVALID_OPERATION if the interface is not implemented | 
 | 117 |      */ | 
 | 118 |     virtual status_t getExternalPosition(uint64_t* position, int64_t* timeNanos) = 0; | 
 | 119 |  | 
 | 120 |     /** | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 121 |      * Start a stream operating in mmap mode. | 
 | 122 |      * createMmapBuffer() must be called before calling start() | 
 | 123 |      * | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 124 |      * \param[in] client a AudioClient struct describing the client starting on this stream. | 
| jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 125 |      * \param[in] attr audio attributes provided by the client. | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 126 |      * \param[out] handle unique handle for this instance. Used with stop(). | 
 | 127 |      * \return OK in case of success. | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 128 |      *         NO_INIT in case of initialization error | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 129 |      *         INVALID_OPERATION if called out of sequence | 
 | 130 |      */ | 
| jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 131 |     virtual status_t start(const AudioClient& client, | 
 | 132 |                            const audio_attributes_t *attr, | 
 | 133 |                            audio_port_handle_t *handle) = 0; | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 134 |  | 
 | 135 |     /** | 
 | 136 |      * Stop a stream operating in mmap mode. | 
 | 137 |      * Must be called after start() | 
 | 138 |      * | 
 | 139 |      * \param[in] handle unique handle allocated by start(). | 
 | 140 |      * \return OK in case of success. | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 141 |      *         NO_INIT in case of initialization error | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 142 |      *         INVALID_OPERATION if called out of sequence | 
 | 143 |      */ | 
 | 144 |     virtual status_t stop(audio_port_handle_t handle) = 0; | 
 | 145 |  | 
| Eric Laurent | 18b5701 | 2017-02-13 16:23:52 -0800 | [diff] [blame] | 146 |     /** | 
 | 147 |      * Put a stream operating in mmap mode into standby. | 
 | 148 |      * Must be called after createMmapBuffer(). Cannot be called if any client is active. | 
 | 149 |      * It is recommended to place a mmap stream into standby as often as possible when no client is | 
 | 150 |      * active to save power. | 
 | 151 |      * | 
 | 152 |      * \return OK in case of success. | 
 | 153 |      *         NO_INIT in case of initialization error | 
 | 154 |      *         INVALID_OPERATION if called out of sequence | 
 | 155 |      */ | 
 | 156 |     virtual status_t standby() = 0; | 
 | 157 |  | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 158 |   protected: | 
 | 159 |     // Subclasses can not be constructed directly by clients. | 
 | 160 |     MmapStreamInterface() {} | 
 | 161 |  | 
 | 162 |     // The destructor automatically closes the stream. | 
 | 163 |     virtual ~MmapStreamInterface() {} | 
 | 164 | }; | 
 | 165 |  | 
 | 166 | } // namespace android | 
 | 167 |  | 
 | 168 | #endif // ANDROID_AUDIO_MMAP_STREAM_INTERFACE_H |