Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AAudioServiceStreamMMAP" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <atomic> |
Phil Burk | f95ed73 | 2020-09-30 18:48:17 +0000 | [diff] [blame] | 22 | #include <inttypes.h> |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 23 | #include <iomanip> |
| 24 | #include <iostream> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 25 | #include <stdint.h> |
| 26 | |
| 27 | #include <utils/String16.h> |
| 28 | #include <media/nbaio/AudioStreamOutSink.h> |
| 29 | #include <media/MmapStreamInterface.h> |
| 30 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 31 | #include "binding/AudioEndpointParcelable.h" |
| 32 | #include "utility/AAudioUtilities.h" |
| 33 | |
| 34 | #include "AAudioServiceEndpointMMAP.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 35 | #include "AAudioServiceStreamBase.h" |
| 36 | #include "AAudioServiceStreamMMAP.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 37 | #include "SharedMemoryProxy.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 38 | |
Phil Burk | e72481c | 2017-08-08 13:20:45 -0700 | [diff] [blame] | 39 | using android::base::unique_fd; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 40 | using namespace android; |
| 41 | using namespace aaudio; |
| 42 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 43 | /** |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 44 | * Service Stream that uses an MMAP buffer. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 45 | */ |
| 46 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 47 | AAudioServiceStreamMMAP::AAudioServiceStreamMMAP(android::AAudioService &aAudioService, |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 48 | bool inService) |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 49 | : AAudioServiceStreamBase(aAudioService) |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 50 | , mInService(inService) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 53 | // Open stream on HAL and pass information about the shared memory buffer back to the client. |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 54 | aaudio_result_t AAudioServiceStreamMMAP::open(const aaudio::AAudioStreamRequest &request) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 55 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 56 | sp<AAudioServiceStreamMMAP> keep(this); |
| 57 | |
Phil Burk | 15f97c9 | 2018-09-04 14:06:27 -0700 | [diff] [blame] | 58 | if (request.getConstantConfiguration().getSharingMode() != AAUDIO_SHARING_MODE_EXCLUSIVE) { |
| 59 | ALOGE("%s() sharingMode mismatch %d", __func__, |
| 60 | request.getConstantConfiguration().getSharingMode()); |
| 61 | return AAUDIO_ERROR_INTERNAL; |
| 62 | } |
| 63 | |
| 64 | aaudio_result_t result = AAudioServiceStreamBase::open(request); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 65 | if (result != AAUDIO_OK) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 66 | return result; |
| 67 | } |
| 68 | |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 69 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 70 | if (endpoint == nullptr) { |
| 71 | ALOGE("%s() has no endpoint", __func__); |
| 72 | return AAUDIO_ERROR_INVALID_STATE; |
| 73 | } |
| 74 | |
| 75 | result = endpoint->registerStream(keep); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 76 | if (result != AAUDIO_OK) { |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 77 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 78 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 79 | |
Phil Burk | 5a26e66 | 2017-07-07 12:44:48 -0700 | [diff] [blame] | 80 | setState(AAUDIO_STREAM_STATE_OPEN); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 81 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 82 | return AAUDIO_OK; |
| 83 | } |
| 84 | |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 85 | // Start the flow of data. |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 86 | aaudio_result_t AAudioServiceStreamMMAP::startDevice() { |
| 87 | aaudio_result_t result = AAudioServiceStreamBase::startDevice(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 88 | if (!mInService && result == AAUDIO_OK) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 89 | // Note that this can sometimes take 200 to 300 msec for a cold start! |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 90 | result = startClient(mMmapClient, nullptr /*const audio_attributes_t* */, &mClientHandle); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 91 | } |
| 92 | return result; |
| 93 | } |
| 94 | |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 95 | // Stop the flow of data such that start() can resume with loss of data. |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 96 | aaudio_result_t AAudioServiceStreamMMAP::pause_l() { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 97 | if (!isRunning()) { |
| 98 | return AAUDIO_OK; |
| 99 | } |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 100 | aaudio_result_t result = AAudioServiceStreamBase::pause_l(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 101 | // TODO put before base::pause()? |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 102 | if (!mInService) { |
Phil Burk | 81ad5ec | 2017-09-01 10:45:41 -0700 | [diff] [blame] | 103 | (void) stopClient(mClientHandle); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 104 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 105 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 108 | aaudio_result_t AAudioServiceStreamMMAP::stop_l() { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 109 | if (!isRunning()) { |
| 110 | return AAUDIO_OK; |
| 111 | } |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 112 | aaudio_result_t result = AAudioServiceStreamBase::stop_l(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 113 | // TODO put before base::stop()? |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 114 | if (!mInService) { |
Phil Burk | 81ad5ec | 2017-09-01 10:45:41 -0700 | [diff] [blame] | 115 | (void) stopClient(mClientHandle); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 116 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 117 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 120 | aaudio_result_t AAudioServiceStreamMMAP::startClient(const android::AudioClient& client, |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 121 | const audio_attributes_t *attr, |
| 122 | audio_port_handle_t *clientHandle) { |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 123 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 124 | if (endpoint == nullptr) { |
| 125 | ALOGE("%s() has no endpoint", __func__); |
| 126 | return AAUDIO_ERROR_INVALID_STATE; |
| 127 | } |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 128 | // Start the client on behalf of the application. Generate a new porthandle. |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 129 | aaudio_result_t result = endpoint->startClient(client, attr, clientHandle); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 130 | return result; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | aaudio_result_t AAudioServiceStreamMMAP::stopClient(audio_port_handle_t clientHandle) { |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 134 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 135 | if (endpoint == nullptr) { |
| 136 | ALOGE("%s() has no endpoint", __func__); |
| 137 | return AAUDIO_ERROR_INVALID_STATE; |
| 138 | } |
| 139 | aaudio_result_t result = endpoint->stopClient(clientHandle); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 140 | return result; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 143 | // Get free-running DSP or DMA hardware position from the HAL. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 144 | aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames, |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 145 | int64_t *timeNanos) { |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 146 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 147 | if (endpoint == nullptr) { |
| 148 | ALOGE("%s() has no endpoint", __func__); |
| 149 | return AAUDIO_ERROR_INVALID_STATE; |
| 150 | } |
| 151 | sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP = |
| 152 | static_cast<AAudioServiceEndpointMMAP *>(endpoint.get()); |
| 153 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 154 | aaudio_result_t result = serviceEndpointMMAP->getFreeRunningPosition(positionFrames, timeNanos); |
| 155 | if (result == AAUDIO_OK) { |
| 156 | Timestamp timestamp(*positionFrames, *timeNanos); |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 157 | mAtomicStreamTimestamp.write(timestamp); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 158 | *positionFrames = timestamp.getPosition(); |
| 159 | *timeNanos = timestamp.getNanoseconds(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 160 | } else if (result != AAUDIO_ERROR_UNAVAILABLE) { |
| 161 | disconnect(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 162 | } |
Phil Burk | 940083c | 2017-07-17 17:00:02 -0700 | [diff] [blame] | 163 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 164 | } |
| 165 | |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 166 | // Get timestamp from presentation position. |
Phil Burk | f95ed73 | 2020-09-30 18:48:17 +0000 | [diff] [blame] | 167 | // If it fails, get timestamp that was written by getFreeRunningPosition() |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 168 | aaudio_result_t AAudioServiceStreamMMAP::getHardwareTimestamp(int64_t *positionFrames, |
| 169 | int64_t *timeNanos) { |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 170 | |
| 171 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 172 | if (endpoint == nullptr) { |
| 173 | ALOGE("%s() has no endpoint", __func__); |
| 174 | return AAUDIO_ERROR_INVALID_STATE; |
| 175 | } |
| 176 | sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP = |
| 177 | static_cast<AAudioServiceEndpointMMAP *>(endpoint.get()); |
| 178 | |
Phil Burk | f95ed73 | 2020-09-30 18:48:17 +0000 | [diff] [blame] | 179 | // Disable this code temporarily because the HAL is not returning |
| 180 | // a useful result. |
| 181 | #if 0 |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 182 | uint64_t position; |
| 183 | if (serviceEndpointMMAP->getExternalPosition(&position, timeNanos) == AAUDIO_OK) { |
Phil Burk | f95ed73 | 2020-09-30 18:48:17 +0000 | [diff] [blame] | 184 | ALOGD("%s() getExternalPosition() says pos = %" PRIi64 ", time = %" PRIi64, |
| 185 | __func__, position, *timeNanos); |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 186 | *positionFrames = (int64_t) position; |
| 187 | return AAUDIO_OK; |
Phil Burk | f95ed73 | 2020-09-30 18:48:17 +0000 | [diff] [blame] | 188 | } else |
| 189 | #endif |
| 190 | if (mAtomicStreamTimestamp.isValid()) { |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 191 | Timestamp timestamp = mAtomicStreamTimestamp.read(); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 192 | *positionFrames = timestamp.getPosition(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 193 | *timeNanos = timestamp.getNanoseconds() + serviceEndpointMMAP->getHardwareTimeOffsetNanos(); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 194 | return AAUDIO_OK; |
| 195 | } else { |
| 196 | return AAUDIO_ERROR_UNAVAILABLE; |
| 197 | } |
| 198 | } |
| 199 | |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 200 | // Get an immutable description of the data queue from the HAL. |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 201 | aaudio_result_t AAudioServiceStreamMMAP::getAudioDataDescription( |
| 202 | AudioEndpointParcelable &parcelable) |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 203 | { |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 204 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 205 | if (endpoint == nullptr) { |
| 206 | ALOGE("%s() has no endpoint", __func__); |
| 207 | return AAUDIO_ERROR_INVALID_STATE; |
| 208 | } |
| 209 | sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP = |
| 210 | static_cast<AAudioServiceEndpointMMAP *>(endpoint.get()); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 211 | return serviceEndpointMMAP->getDownDataDescription(parcelable); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 212 | } |