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