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