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