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