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 | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 21 | #include <atomic> |
| 22 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 23 | #include "AudioClock.h" |
| 24 | #include "AudioEndpointParcelable.h" |
| 25 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 26 | #include "AAudioServiceStreamBase.h" |
| 27 | #include "AAudioServiceStreamFakeHal.h" |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 28 | |
| 29 | #include "FakeAudioHal.h" |
| 30 | |
| 31 | using namespace android; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 32 | using namespace aaudio; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 33 | |
| 34 | // HACK values for Marlin |
| 35 | #define CARD_ID 0 |
| 36 | #define DEVICE_ID 19 |
| 37 | |
| 38 | /** |
| 39 | * Construct the audio message queuues and message queues. |
| 40 | */ |
| 41 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 42 | AAudioServiceStreamFakeHal::AAudioServiceStreamFakeHal() |
| 43 | : AAudioServiceStreamBase() |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 44 | , mStreamId(nullptr) |
| 45 | , mPreviousFrameCounter(0) |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 46 | , mAAudioThread() |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 47 | { |
| 48 | } |
| 49 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 50 | AAudioServiceStreamFakeHal::~AAudioServiceStreamFakeHal() { |
| 51 | ALOGD("AAudioServiceStreamFakeHal::~AAudioServiceStreamFakeHal() call close()"); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 52 | close(); |
| 53 | } |
| 54 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 55 | aaudio_result_t AAudioServiceStreamFakeHal::open(aaudio::AAudioStreamRequest &request, |
| 56 | aaudio::AAudioStreamConfiguration &configuration) { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 57 | // Open stream on HAL and pass information about the ring buffer to the client. |
| 58 | mmap_buffer_info mmapInfo; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 59 | aaudio_result_t error; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 60 | |
| 61 | // Open HAL |
| 62 | error = fake_hal_open(CARD_ID, DEVICE_ID, &mStreamId); |
| 63 | if(error < 0) { |
| 64 | ALOGE("Could not open card %d, device %d", CARD_ID, DEVICE_ID); |
| 65 | return error; |
| 66 | } |
| 67 | |
| 68 | // Get information about the shared audio buffer. |
| 69 | error = fake_hal_get_mmap_info(mStreamId, &mmapInfo); |
| 70 | if (error < 0) { |
| 71 | ALOGE("fake_hal_get_mmap_info returned %d", error); |
| 72 | fake_hal_close(mStreamId); |
| 73 | mStreamId = nullptr; |
| 74 | return error; |
| 75 | } |
| 76 | mHalFileDescriptor = mmapInfo.fd; |
| 77 | mFramesPerBurst = mmapInfo.burst_size_in_frames; |
| 78 | mCapacityInFrames = mmapInfo.buffer_capacity_in_frames; |
| 79 | mCapacityInBytes = mmapInfo.buffer_capacity_in_bytes; |
| 80 | mSampleRate = mmapInfo.sample_rate; |
| 81 | mBytesPerFrame = mmapInfo.channel_count * sizeof(int16_t); // FIXME based on data format |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 82 | ALOGD("AAudioServiceStreamFakeHal::open() mmapInfo.burst_size_in_frames = %d", |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 83 | mmapInfo.burst_size_in_frames); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 84 | ALOGD("AAudioServiceStreamFakeHal::open() mmapInfo.buffer_capacity_in_frames = %d", |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 85 | mmapInfo.buffer_capacity_in_frames); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 86 | ALOGD("AAudioServiceStreamFakeHal::open() mmapInfo.buffer_capacity_in_bytes = %d", |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 87 | mmapInfo.buffer_capacity_in_bytes); |
| 88 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 89 | // Fill in AAudioStreamConfiguration |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 90 | configuration.setSampleRate(mSampleRate); |
| 91 | configuration.setSamplesPerFrame(mmapInfo.channel_count); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 92 | configuration.setAudioFormat(AAUDIO_FORMAT_PCM_I16); |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 93 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 94 | return AAUDIO_OK; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get an immutable description of the in-memory queues |
| 99 | * used to communicate with the underlying HAL or Service. |
| 100 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 101 | aaudio_result_t AAudioServiceStreamFakeHal::getDescription(AudioEndpointParcelable &parcelable) { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 102 | // Gather information on the message queue. |
| 103 | mUpMessageQueue->fillParcelable(parcelable, |
| 104 | parcelable.mUpMessageQueueParcelable); |
| 105 | |
| 106 | // Gather information on the data queue. |
| 107 | // TODO refactor into a SharedRingBuffer? |
| 108 | int fdIndex = parcelable.addFileDescriptor(mHalFileDescriptor, mCapacityInBytes); |
| 109 | parcelable.mDownDataQueueParcelable.setupMemory(fdIndex, 0, mCapacityInBytes); |
| 110 | parcelable.mDownDataQueueParcelable.setBytesPerFrame(mBytesPerFrame); |
| 111 | parcelable.mDownDataQueueParcelable.setFramesPerBurst(mFramesPerBurst); |
| 112 | parcelable.mDownDataQueueParcelable.setCapacityInFrames(mCapacityInFrames); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 113 | return AAUDIO_OK; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Start the flow of data. |
| 118 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 119 | aaudio_result_t AAudioServiceStreamFakeHal::start() { |
| 120 | if (mStreamId == nullptr) return AAUDIO_ERROR_NULL; |
| 121 | aaudio_result_t result = fake_hal_start(mStreamId); |
| 122 | sendServiceEvent(AAUDIO_SERVICE_EVENT_STARTED); |
| 123 | mState = AAUDIO_STREAM_STATE_STARTED; |
| 124 | if (result == AAUDIO_OK) { |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 125 | mThreadEnabled.store(true); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 126 | result = mAAudioThread.start(this); |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 127 | } |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 128 | return result; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Stop the flow of data such that start() can resume with loss of data. |
| 133 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 134 | aaudio_result_t AAudioServiceStreamFakeHal::pause() { |
| 135 | if (mStreamId == nullptr) return AAUDIO_ERROR_NULL; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 136 | sendCurrentTimestamp(); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 137 | aaudio_result_t result = fake_hal_pause(mStreamId); |
| 138 | sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED); |
| 139 | mState = AAUDIO_STREAM_STATE_PAUSED; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 140 | mFramesRead.reset32(); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 141 | ALOGD("AAudioServiceStreamFakeHal::pause() sent AAUDIO_SERVICE_EVENT_PAUSED"); |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 142 | mThreadEnabled.store(false); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 143 | result = mAAudioThread.stop(); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 144 | return result; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Discard any data held by the underlying HAL or Service. |
| 149 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 150 | aaudio_result_t AAudioServiceStreamFakeHal::flush() { |
| 151 | if (mStreamId == nullptr) return AAUDIO_ERROR_NULL; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 152 | // TODO how do we flush an MMAP/NOIRQ buffer? sync pointers? |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 153 | ALOGD("AAudioServiceStreamFakeHal::pause() send AAUDIO_SERVICE_EVENT_FLUSHED"); |
| 154 | sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED); |
| 155 | mState = AAUDIO_STREAM_STATE_FLUSHED; |
| 156 | return AAUDIO_OK; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 159 | aaudio_result_t AAudioServiceStreamFakeHal::close() { |
| 160 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 161 | if (mStreamId != nullptr) { |
| 162 | result = fake_hal_close(mStreamId); |
| 163 | mStreamId = nullptr; |
| 164 | } |
| 165 | return result; |
| 166 | } |
| 167 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 168 | void AAudioServiceStreamFakeHal::sendCurrentTimestamp() { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 169 | int frameCounter = 0; |
| 170 | int error = fake_hal_get_frame_counter(mStreamId, &frameCounter); |
| 171 | if (error < 0) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 172 | ALOGE("AAudioServiceStreamFakeHal::sendCurrentTimestamp() error %d", |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 173 | error); |
| 174 | } else if (frameCounter != mPreviousFrameCounter) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 175 | AAudioServiceMessage command; |
| 176 | command.what = AAudioServiceMessage::code::TIMESTAMP; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 177 | mFramesRead.update32(frameCounter); |
| 178 | command.timestamp.position = mFramesRead.get(); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 179 | ALOGD("AAudioServiceStreamFakeHal::sendCurrentTimestamp() HAL frames = %d, pos = %d", |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 180 | frameCounter, (int)mFramesRead.get()); |
| 181 | command.timestamp.timestamp = AudioClock::getNanoseconds(); |
| 182 | mUpMessageQueue->getFifoBuffer()->write(&command, 1); |
| 183 | mPreviousFrameCounter = frameCounter; |
| 184 | } |
| 185 | } |
| 186 | |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 187 | // implement Runnable |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 188 | void AAudioServiceStreamFakeHal::run() { |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 189 | TimestampScheduler timestampScheduler; |
| 190 | timestampScheduler.setBurstPeriod(mFramesPerBurst, mSampleRate); |
| 191 | timestampScheduler.start(AudioClock::getNanoseconds()); |
| 192 | while(mThreadEnabled.load()) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 193 | aaudio_nanoseconds_t nextTime = timestampScheduler.nextAbsoluteTime(); |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 194 | if (AudioClock::getNanoseconds() >= nextTime) { |
| 195 | sendCurrentTimestamp(); |
| 196 | } else { |
| 197 | // Sleep until it is time to send the next timestamp. |
| 198 | AudioClock::sleepUntilNanoTime(nextTime); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | } |
| 202 | |