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 | |
Phil Burk | 15f97c9 | 2018-09-04 14:06:27 -0700 | [diff] [blame] | 67 | if (request.getConstantConfiguration().getSharingMode() != AAUDIO_SHARING_MODE_EXCLUSIVE) { |
| 68 | ALOGE("%s() sharingMode mismatch %d", __func__, |
| 69 | request.getConstantConfiguration().getSharingMode()); |
| 70 | return AAUDIO_ERROR_INTERNAL; |
| 71 | } |
| 72 | |
| 73 | aaudio_result_t result = AAudioServiceStreamBase::open(request); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 74 | if (result != AAUDIO_OK) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 75 | return result; |
| 76 | } |
| 77 | |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 78 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 79 | if (endpoint == nullptr) { |
| 80 | ALOGE("%s() has no endpoint", __func__); |
| 81 | return AAUDIO_ERROR_INVALID_STATE; |
| 82 | } |
| 83 | |
| 84 | result = endpoint->registerStream(keep); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 85 | if (result != AAUDIO_OK) { |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 86 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 87 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 88 | |
Phil Burk | 5a26e66 | 2017-07-07 12:44:48 -0700 | [diff] [blame] | 89 | setState(AAUDIO_STREAM_STATE_OPEN); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 90 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 91 | return AAUDIO_OK; |
| 92 | } |
| 93 | |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 94 | // Start the flow of data. |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 95 | aaudio_result_t AAudioServiceStreamMMAP::startDevice() { |
| 96 | aaudio_result_t result = AAudioServiceStreamBase::startDevice(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 97 | if (!mInService && result == AAUDIO_OK) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 98 | // 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] | 99 | result = startClient(mMmapClient, &mClientHandle); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 100 | } |
| 101 | return result; |
| 102 | } |
| 103 | |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 104 | // Stop the flow of data such that start() can resume with loss of data. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 105 | aaudio_result_t AAudioServiceStreamMMAP::pause() { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 106 | if (!isRunning()) { |
| 107 | return AAUDIO_OK; |
| 108 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 109 | aaudio_result_t result = AAudioServiceStreamBase::pause(); |
| 110 | // TODO put before base::pause()? |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 111 | if (!mInService) { |
Phil Burk | 81ad5ec | 2017-09-01 10:45:41 -0700 | [diff] [blame] | 112 | (void) stopClient(mClientHandle); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 113 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 114 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 117 | aaudio_result_t AAudioServiceStreamMMAP::stop() { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 118 | if (!isRunning()) { |
| 119 | return AAUDIO_OK; |
| 120 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 121 | aaudio_result_t result = AAudioServiceStreamBase::stop(); |
| 122 | // TODO put before base::stop()? |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 123 | if (!mInService) { |
Phil Burk | 81ad5ec | 2017-09-01 10:45:41 -0700 | [diff] [blame] | 124 | (void) stopClient(mClientHandle); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 125 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 126 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 129 | aaudio_result_t AAudioServiceStreamMMAP::startClient(const android::AudioClient& client, |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 130 | audio_port_handle_t *clientHandle) { |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 131 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 132 | if (endpoint == nullptr) { |
| 133 | ALOGE("%s() has no endpoint", __func__); |
| 134 | return AAUDIO_ERROR_INVALID_STATE; |
| 135 | } |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 136 | // Start the client on behalf of the application. Generate a new porthandle. |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 137 | aaudio_result_t result = endpoint->startClient(client, clientHandle); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 138 | return result; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | aaudio_result_t AAudioServiceStreamMMAP::stopClient(audio_port_handle_t clientHandle) { |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 142 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 143 | if (endpoint == nullptr) { |
| 144 | ALOGE("%s() has no endpoint", __func__); |
| 145 | return AAUDIO_ERROR_INVALID_STATE; |
| 146 | } |
| 147 | aaudio_result_t result = endpoint->stopClient(clientHandle); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 148 | return result; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 151 | // Get free-running DSP or DMA hardware position from the HAL. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 152 | aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames, |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 153 | int64_t *timeNanos) { |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 154 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 155 | if (endpoint == nullptr) { |
| 156 | ALOGE("%s() has no endpoint", __func__); |
| 157 | return AAUDIO_ERROR_INVALID_STATE; |
| 158 | } |
| 159 | sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP = |
| 160 | static_cast<AAudioServiceEndpointMMAP *>(endpoint.get()); |
| 161 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 162 | aaudio_result_t result = serviceEndpointMMAP->getFreeRunningPosition(positionFrames, timeNanos); |
| 163 | if (result == AAUDIO_OK) { |
| 164 | Timestamp timestamp(*positionFrames, *timeNanos); |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 165 | mAtomicStreamTimestamp.write(timestamp); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 166 | *positionFrames = timestamp.getPosition(); |
| 167 | *timeNanos = timestamp.getNanoseconds(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 168 | } else if (result != AAUDIO_ERROR_UNAVAILABLE) { |
| 169 | disconnect(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 170 | } |
Phil Burk | 940083c | 2017-07-17 17:00:02 -0700 | [diff] [blame] | 171 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 174 | // Get timestamp that was written by getFreeRunningPosition() |
| 175 | aaudio_result_t AAudioServiceStreamMMAP::getHardwareTimestamp(int64_t *positionFrames, |
| 176 | int64_t *timeNanos) { |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 177 | |
| 178 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 179 | if (endpoint == nullptr) { |
| 180 | ALOGE("%s() has no endpoint", __func__); |
| 181 | return AAUDIO_ERROR_INVALID_STATE; |
| 182 | } |
| 183 | sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP = |
| 184 | static_cast<AAudioServiceEndpointMMAP *>(endpoint.get()); |
| 185 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 186 | // TODO Get presentation timestamp from the HAL |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 187 | if (mAtomicStreamTimestamp.isValid()) { |
| 188 | Timestamp timestamp = mAtomicStreamTimestamp.read(); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 189 | *positionFrames = timestamp.getPosition(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 190 | *timeNanos = timestamp.getNanoseconds() + serviceEndpointMMAP->getHardwareTimeOffsetNanos(); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 191 | return AAUDIO_OK; |
| 192 | } else { |
| 193 | return AAUDIO_ERROR_UNAVAILABLE; |
| 194 | } |
| 195 | } |
| 196 | |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 197 | // Get an immutable description of the data queue from the HAL. |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 198 | aaudio_result_t AAudioServiceStreamMMAP::getAudioDataDescription( |
| 199 | AudioEndpointParcelable &parcelable) |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 200 | { |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 201 | sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote(); |
| 202 | if (endpoint == nullptr) { |
| 203 | ALOGE("%s() has no endpoint", __func__); |
| 204 | return AAUDIO_ERROR_INVALID_STATE; |
| 205 | } |
| 206 | sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP = |
| 207 | static_cast<AAudioServiceEndpointMMAP *>(endpoint.get()); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 208 | return serviceEndpointMMAP->getDownDataDescription(parcelable); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 209 | } |