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 | |
| 17 | #define LOG_TAG "OboeService" |
| 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 | |
| 26 | #include "OboeServiceStreamBase.h" |
| 27 | #include "OboeServiceStreamFakeHal.h" |
| 28 | |
| 29 | #include "FakeAudioHal.h" |
| 30 | |
| 31 | using namespace android; |
| 32 | using namespace oboe; |
| 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 | |
| 42 | OboeServiceStreamFakeHal::OboeServiceStreamFakeHal() |
| 43 | : OboeServiceStreamBase() |
| 44 | , mStreamId(nullptr) |
| 45 | , mPreviousFrameCounter(0) |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame^] | 46 | , mOboeThread() |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 47 | { |
| 48 | } |
| 49 | |
| 50 | OboeServiceStreamFakeHal::~OboeServiceStreamFakeHal() { |
| 51 | ALOGD("OboeServiceStreamFakeHal::~OboeServiceStreamFakeHal() call close()"); |
| 52 | close(); |
| 53 | } |
| 54 | |
| 55 | oboe_result_t OboeServiceStreamFakeHal::open(oboe::OboeStreamRequest &request, |
| 56 | oboe::OboeStreamConfiguration &configuration) { |
| 57 | // Open stream on HAL and pass information about the ring buffer to the client. |
| 58 | mmap_buffer_info mmapInfo; |
| 59 | oboe_result_t error; |
| 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 |
| 82 | ALOGD("OboeServiceStreamFakeHal::open() mmapInfo.burst_size_in_frames = %d", |
| 83 | mmapInfo.burst_size_in_frames); |
| 84 | ALOGD("OboeServiceStreamFakeHal::open() mmapInfo.buffer_capacity_in_frames = %d", |
| 85 | mmapInfo.buffer_capacity_in_frames); |
| 86 | ALOGD("OboeServiceStreamFakeHal::open() mmapInfo.buffer_capacity_in_bytes = %d", |
| 87 | mmapInfo.buffer_capacity_in_bytes); |
| 88 | |
| 89 | // Fill in OboeStreamConfiguration |
| 90 | configuration.setSampleRate(mSampleRate); |
| 91 | configuration.setSamplesPerFrame(mmapInfo.channel_count); |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame^] | 92 | configuration.setAudioFormat(OBOE_AUDIO_FORMAT_PCM_I16); |
| 93 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 94 | return OBOE_OK; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get an immutable description of the in-memory queues |
| 99 | * used to communicate with the underlying HAL or Service. |
| 100 | */ |
| 101 | oboe_result_t OboeServiceStreamFakeHal::getDescription(AudioEndpointParcelable &parcelable) { |
| 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); |
| 113 | return OBOE_OK; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Start the flow of data. |
| 118 | */ |
| 119 | oboe_result_t OboeServiceStreamFakeHal::start() { |
| 120 | if (mStreamId == nullptr) return OBOE_ERROR_NULL; |
| 121 | oboe_result_t result = fake_hal_start(mStreamId); |
| 122 | sendServiceEvent(OBOE_SERVICE_EVENT_STARTED); |
| 123 | mState = OBOE_STREAM_STATE_STARTED; |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame^] | 124 | if (result == OBOE_OK) { |
| 125 | mThreadEnabled.store(true); |
| 126 | result = mOboeThread.start(this); |
| 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 | */ |
| 134 | oboe_result_t OboeServiceStreamFakeHal::pause() { |
| 135 | if (mStreamId == nullptr) return OBOE_ERROR_NULL; |
| 136 | sendCurrentTimestamp(); |
| 137 | oboe_result_t result = fake_hal_pause(mStreamId); |
| 138 | sendServiceEvent(OBOE_SERVICE_EVENT_PAUSED); |
| 139 | mState = OBOE_STREAM_STATE_PAUSED; |
| 140 | mFramesRead.reset32(); |
| 141 | ALOGD("OboeServiceStreamFakeHal::pause() sent OBOE_SERVICE_EVENT_PAUSED"); |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame^] | 142 | mThreadEnabled.store(false); |
| 143 | result = mOboeThread.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 | */ |
| 150 | oboe_result_t OboeServiceStreamFakeHal::flush() { |
| 151 | if (mStreamId == nullptr) return OBOE_ERROR_NULL; |
| 152 | // TODO how do we flush an MMAP/NOIRQ buffer? sync pointers? |
| 153 | ALOGD("OboeServiceStreamFakeHal::pause() send OBOE_SERVICE_EVENT_FLUSHED"); |
| 154 | sendServiceEvent(OBOE_SERVICE_EVENT_FLUSHED); |
| 155 | mState = OBOE_STREAM_STATE_FLUSHED; |
| 156 | return OBOE_OK; |
| 157 | } |
| 158 | |
| 159 | oboe_result_t OboeServiceStreamFakeHal::close() { |
| 160 | oboe_result_t result = OBOE_OK; |
| 161 | if (mStreamId != nullptr) { |
| 162 | result = fake_hal_close(mStreamId); |
| 163 | mStreamId = nullptr; |
| 164 | } |
| 165 | return result; |
| 166 | } |
| 167 | |
| 168 | void OboeServiceStreamFakeHal::sendCurrentTimestamp() { |
| 169 | int frameCounter = 0; |
| 170 | int error = fake_hal_get_frame_counter(mStreamId, &frameCounter); |
| 171 | if (error < 0) { |
| 172 | ALOGE("OboeServiceStreamFakeHal::sendCurrentTimestamp() error %d", |
| 173 | error); |
| 174 | } else if (frameCounter != mPreviousFrameCounter) { |
| 175 | OboeServiceMessage command; |
| 176 | command.what = OboeServiceMessage::code::TIMESTAMP; |
| 177 | mFramesRead.update32(frameCounter); |
| 178 | command.timestamp.position = mFramesRead.get(); |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame^] | 179 | ALOGD("OboeServiceStreamFakeHal::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 |
| 188 | void OboeServiceStreamFakeHal::run() { |
| 189 | TimestampScheduler timestampScheduler; |
| 190 | timestampScheduler.setBurstPeriod(mFramesPerBurst, mSampleRate); |
| 191 | timestampScheduler.start(AudioClock::getNanoseconds()); |
| 192 | while(mThreadEnabled.load()) { |
| 193 | oboe_nanoseconds_t nextTime = timestampScheduler.nextAbsoluteTime(); |
| 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 | |