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