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 | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 44 | close(); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 45 | ALOGD("AAudioServiceStreamBase::~AAudioServiceStreamBase() destroyed %p", this); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 48 | aaudio_result_t AAudioServiceStreamBase::open(const aaudio::AAudioStreamRequest &request, |
| 49 | aaudio::AAudioStreamConfiguration &configurationOutput) { |
| 50 | std::lock_guard<std::mutex> lock(mLockUpMessageQueue); |
| 51 | if (mUpMessageQueue != nullptr) { |
| 52 | return AAUDIO_ERROR_INVALID_STATE; |
| 53 | } else { |
| 54 | mUpMessageQueue = new SharedRingBuffer(); |
| 55 | return mUpMessageQueue->allocate(sizeof(AAudioServiceMessage), QUEUE_UP_CAPACITY_COMMANDS); |
| 56 | } |
| 57 | } |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 58 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 59 | aaudio_result_t AAudioServiceStreamBase::close() { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 60 | stop(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 61 | std::lock_guard<std::mutex> lock(mLockUpMessageQueue); |
| 62 | delete mUpMessageQueue; |
| 63 | mUpMessageQueue = nullptr; |
Phil Burk | 942bdc0 | 2017-05-03 11:36:50 -0700 | [diff] [blame] | 64 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 65 | return AAUDIO_OK; |
| 66 | } |
| 67 | |
| 68 | aaudio_result_t AAudioServiceStreamBase::start() { |
| 69 | sendServiceEvent(AAUDIO_SERVICE_EVENT_STARTED); |
| 70 | mState = AAUDIO_STREAM_STATE_STARTED; |
| 71 | mThreadEnabled.store(true); |
| 72 | return mAAudioThread.start(this); |
| 73 | } |
| 74 | |
| 75 | aaudio_result_t AAudioServiceStreamBase::pause() { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 76 | aaudio_result_t result = AAUDIO_OK; |
| 77 | if (isRunning()) { |
| 78 | sendCurrentTimestamp(); |
| 79 | mThreadEnabled.store(false); |
| 80 | result = mAAudioThread.stop(); |
| 81 | if (result != AAUDIO_OK) { |
| 82 | processFatalError(); |
| 83 | return result; |
| 84 | } |
| 85 | sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 86 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 87 | mState = AAUDIO_STREAM_STATE_PAUSED; |
| 88 | return result; |
| 89 | } |
| 90 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 91 | aaudio_result_t AAudioServiceStreamBase::stop() { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 92 | aaudio_result_t result = AAUDIO_OK; |
| 93 | if (isRunning()) { |
| 94 | // TODO wait for data to be played out |
| 95 | sendCurrentTimestamp(); |
| 96 | mThreadEnabled.store(false); |
| 97 | result = mAAudioThread.stop(); |
| 98 | if (result != AAUDIO_OK) { |
| 99 | processFatalError(); |
| 100 | return result; |
| 101 | } |
| 102 | sendServiceEvent(AAUDIO_SERVICE_EVENT_STOPPED); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 103 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 104 | mState = AAUDIO_STREAM_STATE_STOPPED; |
| 105 | return result; |
| 106 | } |
| 107 | |
| 108 | aaudio_result_t AAudioServiceStreamBase::flush() { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 109 | sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED); |
| 110 | mState = AAUDIO_STREAM_STATE_FLUSHED; |
| 111 | return AAUDIO_OK; |
| 112 | } |
| 113 | |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 114 | // implement Runnable, periodically send timestamps to client |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 115 | void AAudioServiceStreamBase::run() { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 116 | ALOGD("AAudioServiceStreamBase::run() entering ----------------"); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 117 | TimestampScheduler timestampScheduler; |
| 118 | timestampScheduler.setBurstPeriod(mFramesPerBurst, mSampleRate); |
| 119 | timestampScheduler.start(AudioClock::getNanoseconds()); |
| 120 | int64_t nextTime = timestampScheduler.nextAbsoluteTime(); |
| 121 | while(mThreadEnabled.load()) { |
| 122 | if (AudioClock::getNanoseconds() >= nextTime) { |
| 123 | aaudio_result_t result = sendCurrentTimestamp(); |
| 124 | if (result != AAUDIO_OK) { |
| 125 | break; |
| 126 | } |
| 127 | nextTime = timestampScheduler.nextAbsoluteTime(); |
| 128 | } else { |
| 129 | // Sleep until it is time to send the next timestamp. |
| 130 | AudioClock::sleepUntilNanoTime(nextTime); |
| 131 | } |
| 132 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 133 | ALOGD("AAudioServiceStreamBase::run() exiting ----------------"); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 136 | void AAudioServiceStreamBase::processFatalError() { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 137 | sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED); |
| 138 | } |
| 139 | |
| 140 | aaudio_result_t AAudioServiceStreamBase::sendServiceEvent(aaudio_service_event_t event, |
| 141 | double dataDouble, |
| 142 | int64_t dataLong) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 143 | AAudioServiceMessage command; |
| 144 | command.what = AAudioServiceMessage::code::EVENT; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 145 | command.event.event = event; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 146 | command.event.dataDouble = dataDouble; |
| 147 | command.event.dataLong = dataLong; |
| 148 | return writeUpMessageQueue(&command); |
| 149 | } |
| 150 | |
| 151 | aaudio_result_t AAudioServiceStreamBase::writeUpMessageQueue(AAudioServiceMessage *command) { |
| 152 | std::lock_guard<std::mutex> lock(mLockUpMessageQueue); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 153 | if (mUpMessageQueue == nullptr) { |
| 154 | ALOGE("writeUpMessageQueue(): mUpMessageQueue null! - stream not open"); |
| 155 | return AAUDIO_ERROR_NULL; |
| 156 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 157 | int32_t count = mUpMessageQueue->getFifoBuffer()->write(command, 1); |
| 158 | if (count != 1) { |
| 159 | ALOGE("writeUpMessageQueue(): Queue full. Did client die?"); |
| 160 | return AAUDIO_ERROR_WOULD_BLOCK; |
| 161 | } else { |
| 162 | return AAUDIO_OK; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | aaudio_result_t AAudioServiceStreamBase::sendCurrentTimestamp() { |
| 167 | AAudioServiceMessage command; |
| 168 | aaudio_result_t result = getFreeRunningPosition(&command.timestamp.position, |
| 169 | &command.timestamp.timestamp); |
| 170 | if (result == AAUDIO_OK) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 171 | // ALOGD("sendCurrentTimestamp(): position = %lld, nanos = %lld", |
| 172 | // (long long) command.timestamp.position, |
| 173 | // (long long) command.timestamp.timestamp); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 174 | command.what = AAudioServiceMessage::code::TIMESTAMP; |
| 175 | result = writeUpMessageQueue(&command); |
| 176 | } |
| 177 | return result; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 180 | /** |
| 181 | * Get an immutable description of the in-memory queues |
| 182 | * used to communicate with the underlying HAL or Service. |
| 183 | */ |
| 184 | aaudio_result_t AAudioServiceStreamBase::getDescription(AudioEndpointParcelable &parcelable) { |
| 185 | // Gather information on the message queue. |
| 186 | mUpMessageQueue->fillParcelable(parcelable, |
| 187 | parcelable.mUpMessageQueueParcelable); |
| 188 | return getDownDataDescription(parcelable); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 189 | } |