Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 "AAudioServiceStreamBase" |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame^] | 21 | #include <iomanip> |
| 22 | #include <iostream> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 23 | #include <mutex> |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 24 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 25 | #include "binding/IAAudioService.h" |
| 26 | #include "binding/AAudioServiceMessage.h" |
| 27 | #include "utility/AudioClock.h" |
| 28 | |
| 29 | #include "AAudioServiceStreamBase.h" |
| 30 | #include "TimestampScheduler.h" |
| 31 | |
| 32 | using namespace android; // TODO just import names needed |
| 33 | using namespace aaudio; // TODO just import names needed |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 36 | * Base class for streams in the service. |
| 37 | * @return |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 38 | */ |
| 39 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 40 | AAudioServiceStreamBase::AAudioServiceStreamBase() |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 41 | : mUpMessageQueue(nullptr) |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 42 | , mAAudioThread() |
| 43 | , mAtomicTimestamp() { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 44 | mMmapClient.clientUid = -1; |
| 45 | mMmapClient.clientPid = -1; |
| 46 | mMmapClient.packageName = String16(""); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 49 | AAudioServiceStreamBase::~AAudioServiceStreamBase() { |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 50 | ALOGD("AAudioServiceStreamBase::~AAudioServiceStreamBase() destroying %p", this); |
Phil Burk | 5a26e66 | 2017-07-07 12:44:48 -0700 | [diff] [blame] | 51 | // If the stream is deleted when OPEN or in use then audio resources will leak. |
| 52 | // This would indicate an internal error. So we want to find this ASAP. |
| 53 | LOG_ALWAYS_FATAL_IF(!(mState == AAUDIO_STREAM_STATE_CLOSED |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 54 | || mState == AAUDIO_STREAM_STATE_UNINITIALIZED |
| 55 | || mState == AAUDIO_STREAM_STATE_DISCONNECTED), |
Phil Burk | 5a26e66 | 2017-07-07 12:44:48 -0700 | [diff] [blame] | 56 | "service stream still open, state = %d", mState); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame^] | 59 | std::string AAudioServiceStreamBase::dumpHeader() { |
| 60 | return std::string(" T Handle UId Run State Format Burst Chan Capacity"); |
| 61 | } |
| 62 | |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 63 | std::string AAudioServiceStreamBase::dump() const { |
| 64 | std::stringstream result; |
| 65 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame^] | 66 | result << " 0x" << std::setfill('0') << std::setw(8) << std::hex << mHandle |
| 67 | << std::dec << std::setfill(' ') ; |
| 68 | result << std::setw(6) << mMmapClient.clientUid; |
| 69 | result << std::setw(4) << (isRunning() ? "yes" : " no"); |
| 70 | result << std::setw(6) << mState; |
| 71 | result << std::setw(7) << mAudioFormat; |
| 72 | result << std::setw(6) << mFramesPerBurst; |
| 73 | result << std::setw(5) << mSamplesPerFrame; |
| 74 | result << std::setw(9) << mCapacityInFrames; |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 75 | |
| 76 | return result.str(); |
| 77 | } |
| 78 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 79 | aaudio_result_t AAudioServiceStreamBase::open(const aaudio::AAudioStreamRequest &request, |
| 80 | aaudio::AAudioStreamConfiguration &configurationOutput) { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 81 | |
| 82 | mMmapClient.clientUid = request.getUserId(); |
| 83 | mMmapClient.clientPid = request.getProcessId(); |
| 84 | mMmapClient.packageName.setTo(String16("")); // FIXME what should we do here? |
| 85 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 86 | std::lock_guard<std::mutex> lock(mLockUpMessageQueue); |
| 87 | if (mUpMessageQueue != nullptr) { |
| 88 | return AAUDIO_ERROR_INVALID_STATE; |
| 89 | } else { |
| 90 | mUpMessageQueue = new SharedRingBuffer(); |
| 91 | return mUpMessageQueue->allocate(sizeof(AAudioServiceMessage), QUEUE_UP_CAPACITY_COMMANDS); |
| 92 | } |
| 93 | } |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 94 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 95 | aaudio_result_t AAudioServiceStreamBase::close() { |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 96 | if (mState != AAUDIO_STREAM_STATE_CLOSED) { |
| 97 | stopTimestampThread(); |
| 98 | std::lock_guard<std::mutex> lock(mLockUpMessageQueue); |
| 99 | delete mUpMessageQueue; |
| 100 | mUpMessageQueue = nullptr; |
| 101 | mState = AAUDIO_STREAM_STATE_CLOSED; |
| 102 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 103 | return AAUDIO_OK; |
| 104 | } |
| 105 | |
| 106 | aaudio_result_t AAudioServiceStreamBase::start() { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 107 | if (isRunning()) { |
| 108 | return AAUDIO_OK; |
| 109 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 110 | sendServiceEvent(AAUDIO_SERVICE_EVENT_STARTED); |
| 111 | mState = AAUDIO_STREAM_STATE_STARTED; |
| 112 | mThreadEnabled.store(true); |
| 113 | return mAAudioThread.start(this); |
| 114 | } |
| 115 | |
| 116 | aaudio_result_t AAudioServiceStreamBase::pause() { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 117 | aaudio_result_t result = AAUDIO_OK; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 118 | if (!isRunning()) { |
| 119 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 120 | } |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 121 | sendCurrentTimestamp(); |
| 122 | mThreadEnabled.store(false); |
| 123 | result = mAAudioThread.stop(); |
| 124 | if (result != AAUDIO_OK) { |
| 125 | disconnect(); |
| 126 | return result; |
| 127 | } |
| 128 | sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 129 | mState = AAUDIO_STREAM_STATE_PAUSED; |
| 130 | return result; |
| 131 | } |
| 132 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 133 | aaudio_result_t AAudioServiceStreamBase::stop() { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 134 | aaudio_result_t result = AAUDIO_OK; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 135 | if (!isRunning()) { |
| 136 | return result; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 137 | } |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 138 | // TODO wait for data to be played out |
| 139 | sendCurrentTimestamp(); // warning - this calls a virtual function |
| 140 | result = stopTimestampThread(); |
| 141 | if (result != AAUDIO_OK) { |
| 142 | disconnect(); |
| 143 | return result; |
| 144 | } |
| 145 | sendServiceEvent(AAUDIO_SERVICE_EVENT_STOPPED); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 146 | mState = AAUDIO_STREAM_STATE_STOPPED; |
| 147 | return result; |
| 148 | } |
| 149 | |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 150 | aaudio_result_t AAudioServiceStreamBase::stopTimestampThread() { |
| 151 | aaudio_result_t result = AAUDIO_OK; |
| 152 | // clear flag that tells thread to loop |
| 153 | if (mThreadEnabled.exchange(false)) { |
| 154 | result = mAAudioThread.stop(); |
| 155 | } |
| 156 | return result; |
| 157 | } |
| 158 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 159 | aaudio_result_t AAudioServiceStreamBase::flush() { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 160 | sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED); |
| 161 | mState = AAUDIO_STREAM_STATE_FLUSHED; |
| 162 | return AAUDIO_OK; |
| 163 | } |
| 164 | |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 165 | // implement Runnable, periodically send timestamps to client |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 166 | void AAudioServiceStreamBase::run() { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 167 | ALOGD("AAudioServiceStreamBase::run() entering ----------------"); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 168 | TimestampScheduler timestampScheduler; |
| 169 | timestampScheduler.setBurstPeriod(mFramesPerBurst, mSampleRate); |
| 170 | timestampScheduler.start(AudioClock::getNanoseconds()); |
| 171 | int64_t nextTime = timestampScheduler.nextAbsoluteTime(); |
| 172 | while(mThreadEnabled.load()) { |
| 173 | if (AudioClock::getNanoseconds() >= nextTime) { |
| 174 | aaudio_result_t result = sendCurrentTimestamp(); |
| 175 | if (result != AAUDIO_OK) { |
| 176 | break; |
| 177 | } |
| 178 | nextTime = timestampScheduler.nextAbsoluteTime(); |
| 179 | } else { |
| 180 | // Sleep until it is time to send the next timestamp. |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 181 | // TODO Wait for a signal with a timeout so that we can stop more quickly. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 182 | AudioClock::sleepUntilNanoTime(nextTime); |
| 183 | } |
| 184 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 185 | ALOGD("AAudioServiceStreamBase::run() exiting ----------------"); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 188 | void AAudioServiceStreamBase::disconnect() { |
| 189 | if (mState != AAUDIO_STREAM_STATE_DISCONNECTED) { |
| 190 | sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED); |
| 191 | mState = AAUDIO_STREAM_STATE_DISCONNECTED; |
| 192 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event, |
| 196 | double dataDouble, |
| 197 | int64_t dataLong) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 198 | AAudioServiceMessage command; |
| 199 | command.what = AAudioServiceMessage::code::EVENT; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 200 | command.event.event = event; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 201 | command.event.dataDouble = dataDouble; |
| 202 | command.event.dataLong = dataLong; |
| 203 | return writeUpMessageQueue(&command); |
| 204 | } |
| 205 | |
| 206 | aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) { |
| 207 | std::lock_guard<std::mutex> lock(mLockUpMessageQueue); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 208 | if (mUpMessageQueue == nullptr) { |
| 209 | ALOGE("writeUpMessageQueue(): mUpMessageQueue null! - stream not open"); |
| 210 | return AAUDIO_ERROR_NULL; |
| 211 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 212 | int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1); |
| 213 | if (count != 1) { |
| 214 | ALOGE("writeUpMessageQueue(): Queue full. Did client die?"); |
| 215 | return AAUDIO_ERROR_WOULD_BLOCK; |
| 216 | } else { |
| 217 | return AAUDIO_OK; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp() { |
| 222 | AAudioServiceMessage command; |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 223 | // Send a timestamp for the clock model. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 224 | aaudio_result_t result = getFreeRunningPosition(&command.timestamp.position, |
| 225 | &command.timestamp.timestamp); |
| 226 | if (result == AAUDIO_OK) { |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 227 | command.what = AAudioServiceMessage::code::TIMESTAMP_SERVICE; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 228 | result = writeUpMessageQueue(&command); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 229 | |
| 230 | if (result == AAUDIO_OK) { |
| 231 | // Send a hardware timestamp for presentation time. |
| 232 | result = getHardwareTimestamp(&command.timestamp.position, |
| 233 | &command.timestamp.timestamp); |
| 234 | if (result == AAUDIO_OK) { |
| 235 | command.what = AAudioServiceMessage::code::TIMESTAMP_HARDWARE; |
| 236 | result = writeUpMessageQueue(&command); |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | if (result == AAUDIO_ERROR_UNAVAILABLE) { |
Phil Burk | 940083c | 2017-07-17 17:00:02 -0700 | [diff] [blame] | 242 | result = AAUDIO_OK; // just not available yet, try again later |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 243 | } |
| 244 | return result; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 247 | /** |
| 248 | * Get an immutable description of the in-memory queues |
| 249 | * used to communicate with the underlying HAL or Service. |
| 250 | */ |
| 251 | aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) { |
| 252 | // Gather information on the message queue. |
| 253 | mUpMessageQueue->fillParcelable(parcelable, |
| 254 | parcelable.mUpMessageQueueParcelable); |
| 255 | return getDownDataDescription(parcelable); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 256 | } |