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 | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 53 | if (mState == AAUDIO_STREAM_STATE_CLOSED) { |
| 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) { |
| 70 | ALOGE("AAudioServiceStreamBase open returned %d", result); |
| 71 | return result; |
| 72 | } |
| 73 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 74 | result = mServiceEndpoint->registerStream(keep); |
| 75 | if (result != AAUDIO_OK) { |
| 76 | goto error; |
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 | |
| 81 | error: |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 82 | return AAUDIO_OK; |
| 83 | } |
| 84 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 85 | /** |
| 86 | * Start the flow of data. |
| 87 | */ |
| 88 | aaudio_result_t AAudioServiceStreamMMAP::start() { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 89 | if (isRunning()) { |
| 90 | return AAUDIO_OK; |
| 91 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 92 | |
| 93 | aaudio_result_t result = AAudioServiceStreamBase::start(); |
| 94 | if (!mInService && result == AAUDIO_OK) { |
Phil Burk | 81ad5ec | 2017-09-01 10:45:41 -0700 | [diff] [blame] | 95 | result = startClient(mMmapClient, &mClientHandle); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 96 | } |
| 97 | return result; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Stop the flow of data such that start() can resume with loss of data. |
| 102 | */ |
| 103 | aaudio_result_t AAudioServiceStreamMMAP::pause() { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 104 | if (!isRunning()) { |
| 105 | return AAUDIO_OK; |
| 106 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 107 | aaudio_result_t result = AAudioServiceStreamBase::pause(); |
| 108 | // TODO put before base::pause()? |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 109 | if (!mInService) { |
Phil Burk | 81ad5ec | 2017-09-01 10:45:41 -0700 | [diff] [blame] | 110 | (void) stopClient(mClientHandle); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 111 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 112 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 115 | aaudio_result_t AAudioServiceStreamMMAP::stop() { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 116 | if (!isRunning()) { |
| 117 | return AAUDIO_OK; |
| 118 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 119 | aaudio_result_t result = AAudioServiceStreamBase::stop(); |
| 120 | // TODO put before base::stop()? |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 121 | if (!mInService) { |
Phil Burk | 81ad5ec | 2017-09-01 10:45:41 -0700 | [diff] [blame] | 122 | (void) stopClient(mClientHandle); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 123 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 124 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 127 | aaudio_result_t AAudioServiceStreamMMAP::startClient(const android::AudioClient& client, |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 128 | audio_port_handle_t *clientHandle) { |
| 129 | aaudio_result_t result = mServiceEndpoint->startClient(client, clientHandle); |
| 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 | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 134 | aaudio_result_t result = mServiceEndpoint->stopClient(clientHandle); |
| 135 | return result; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 138 | // Get free-running DSP or DMA hardware position from the HAL. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 139 | aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames, |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 140 | int64_t *timeNanos) { |
| 141 | sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP{ |
| 142 | static_cast<AAudioServiceEndpointMMAP *>(mServiceEndpoint.get())}; |
| 143 | aaudio_result_t result = serviceEndpointMMAP->getFreeRunningPosition(positionFrames, timeNanos); |
| 144 | if (result == AAUDIO_OK) { |
| 145 | Timestamp timestamp(*positionFrames, *timeNanos); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 146 | mAtomicTimestamp.write(timestamp); |
| 147 | *positionFrames = timestamp.getPosition(); |
| 148 | *timeNanos = timestamp.getNanoseconds(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 149 | } else if (result != AAUDIO_ERROR_UNAVAILABLE) { |
| 150 | disconnect(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 151 | } |
Phil Burk | 940083c | 2017-07-17 17:00:02 -0700 | [diff] [blame] | 152 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 155 | // Get timestamp that was written by getFreeRunningPosition() |
| 156 | aaudio_result_t AAudioServiceStreamMMAP::getHardwareTimestamp(int64_t *positionFrames, |
| 157 | int64_t *timeNanos) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 158 | sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP{ |
| 159 | static_cast<AAudioServiceEndpointMMAP *>(mServiceEndpoint.get())}; |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 160 | // TODO Get presentation timestamp from the HAL |
| 161 | if (mAtomicTimestamp.isValid()) { |
| 162 | Timestamp timestamp = mAtomicTimestamp.read(); |
| 163 | *positionFrames = timestamp.getPosition(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 164 | *timeNanos = timestamp.getNanoseconds() + serviceEndpointMMAP->getHardwareTimeOffsetNanos(); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 165 | return AAUDIO_OK; |
| 166 | } else { |
| 167 | return AAUDIO_ERROR_UNAVAILABLE; |
| 168 | } |
| 169 | } |
| 170 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 171 | /** |
| 172 | * Get an immutable description of the data queue from the HAL. |
| 173 | */ |
| 174 | aaudio_result_t AAudioServiceStreamMMAP::getDownDataDescription(AudioEndpointParcelable &parcelable) |
| 175 | { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 176 | sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP{ |
| 177 | static_cast<AAudioServiceEndpointMMAP *>(mServiceEndpoint.get())}; |
| 178 | return serviceEndpointMMAP->getDownDataDescription(parcelable); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 179 | } |