blob: 1caeb3f2f63c80bf850feb08f79a4c40969c5daa [file] [log] [blame]
Phil Burk2355edb2016-12-26 13:54:02 -08001/*
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 Burk5ed503c2017-02-01 09:38:15 -080017#define LOG_TAG "AAudioService"
Phil Burk2355edb2016-12-26 13:54:02 -080018//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
Phil Burkdec33ab2017-01-17 14:48:16 -080021#include <atomic>
22
Phil Burk2355edb2016-12-26 13:54:02 -080023#include "AudioClock.h"
24#include "AudioEndpointParcelable.h"
25
Phil Burk5ed503c2017-02-01 09:38:15 -080026#include "AAudioServiceStreamBase.h"
27#include "AAudioServiceStreamFakeHal.h"
Phil Burk2355edb2016-12-26 13:54:02 -080028
29#include "FakeAudioHal.h"
30
31using namespace android;
Phil Burk5ed503c2017-02-01 09:38:15 -080032using namespace aaudio;
Phil Burk2355edb2016-12-26 13:54:02 -080033
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 Burk5ed503c2017-02-01 09:38:15 -080042AAudioServiceStreamFakeHal::AAudioServiceStreamFakeHal()
43 : AAudioServiceStreamBase()
Phil Burk2355edb2016-12-26 13:54:02 -080044 , mStreamId(nullptr)
45 , mPreviousFrameCounter(0)
Phil Burk5ed503c2017-02-01 09:38:15 -080046 , mAAudioThread()
Phil Burk2355edb2016-12-26 13:54:02 -080047{
48}
49
Phil Burk5ed503c2017-02-01 09:38:15 -080050AAudioServiceStreamFakeHal::~AAudioServiceStreamFakeHal() {
51 ALOGD("AAudioServiceStreamFakeHal::~AAudioServiceStreamFakeHal() call close()");
Phil Burk2355edb2016-12-26 13:54:02 -080052 close();
53}
54
Phil Burk5ed503c2017-02-01 09:38:15 -080055aaudio_result_t AAudioServiceStreamFakeHal::open(aaudio::AAudioStreamRequest &request,
Phil Burk3df348f2017-02-08 11:41:55 -080056 aaudio::AAudioStreamConfiguration &configurationOutput) {
Phil Burk2355edb2016-12-26 13:54:02 -080057 // Open stream on HAL and pass information about the ring buffer to the client.
58 mmap_buffer_info mmapInfo;
Phil Burk5ed503c2017-02-01 09:38:15 -080059 aaudio_result_t error;
Phil Burk2355edb2016-12-26 13:54:02 -080060
61 // Open HAL
Phil Burk3df348f2017-02-08 11:41:55 -080062 int bufferCapacity = request.getConfiguration().getBufferCapacity();
63 error = fake_hal_open(CARD_ID, DEVICE_ID, bufferCapacity, &mStreamId);
Phil Burk2355edb2016-12-26 13:54:02 -080064 if(error < 0) {
65 ALOGE("Could not open card %d, device %d", CARD_ID, DEVICE_ID);
66 return error;
67 }
68
69 // Get information about the shared audio buffer.
70 error = fake_hal_get_mmap_info(mStreamId, &mmapInfo);
71 if (error < 0) {
72 ALOGE("fake_hal_get_mmap_info returned %d", error);
73 fake_hal_close(mStreamId);
74 mStreamId = nullptr;
75 return error;
76 }
77 mHalFileDescriptor = mmapInfo.fd;
78 mFramesPerBurst = mmapInfo.burst_size_in_frames;
79 mCapacityInFrames = mmapInfo.buffer_capacity_in_frames;
80 mCapacityInBytes = mmapInfo.buffer_capacity_in_bytes;
81 mSampleRate = mmapInfo.sample_rate;
82 mBytesPerFrame = mmapInfo.channel_count * sizeof(int16_t); // FIXME based on data format
Phil Burk5ed503c2017-02-01 09:38:15 -080083 ALOGD("AAudioServiceStreamFakeHal::open() mmapInfo.burst_size_in_frames = %d",
Phil Burk2355edb2016-12-26 13:54:02 -080084 mmapInfo.burst_size_in_frames);
Phil Burk5ed503c2017-02-01 09:38:15 -080085 ALOGD("AAudioServiceStreamFakeHal::open() mmapInfo.buffer_capacity_in_frames = %d",
Phil Burk2355edb2016-12-26 13:54:02 -080086 mmapInfo.buffer_capacity_in_frames);
Phil Burk5ed503c2017-02-01 09:38:15 -080087 ALOGD("AAudioServiceStreamFakeHal::open() mmapInfo.buffer_capacity_in_bytes = %d",
Phil Burk2355edb2016-12-26 13:54:02 -080088 mmapInfo.buffer_capacity_in_bytes);
89
Phil Burk5ed503c2017-02-01 09:38:15 -080090 // Fill in AAudioStreamConfiguration
Phil Burk3df348f2017-02-08 11:41:55 -080091 configurationOutput.setSampleRate(mSampleRate);
92 configurationOutput.setSamplesPerFrame(mmapInfo.channel_count);
93 configurationOutput.setAudioFormat(AAUDIO_FORMAT_PCM_I16);
Phil Burkdec33ab2017-01-17 14:48:16 -080094
Phil Burk5ed503c2017-02-01 09:38:15 -080095 return AAUDIO_OK;
Phil Burk2355edb2016-12-26 13:54:02 -080096}
97
98/**
99 * Get an immutable description of the in-memory queues
100 * used to communicate with the underlying HAL or Service.
101 */
Phil Burk5ed503c2017-02-01 09:38:15 -0800102aaudio_result_t AAudioServiceStreamFakeHal::getDescription(AudioEndpointParcelable &parcelable) {
Phil Burk2355edb2016-12-26 13:54:02 -0800103 // Gather information on the message queue.
104 mUpMessageQueue->fillParcelable(parcelable,
105 parcelable.mUpMessageQueueParcelable);
106
107 // Gather information on the data queue.
108 // TODO refactor into a SharedRingBuffer?
109 int fdIndex = parcelable.addFileDescriptor(mHalFileDescriptor, mCapacityInBytes);
110 parcelable.mDownDataQueueParcelable.setupMemory(fdIndex, 0, mCapacityInBytes);
111 parcelable.mDownDataQueueParcelable.setBytesPerFrame(mBytesPerFrame);
112 parcelable.mDownDataQueueParcelable.setFramesPerBurst(mFramesPerBurst);
113 parcelable.mDownDataQueueParcelable.setCapacityInFrames(mCapacityInFrames);
Phil Burk5ed503c2017-02-01 09:38:15 -0800114 return AAUDIO_OK;
Phil Burk2355edb2016-12-26 13:54:02 -0800115}
116
117/**
118 * Start the flow of data.
119 */
Phil Burk5ed503c2017-02-01 09:38:15 -0800120aaudio_result_t AAudioServiceStreamFakeHal::start() {
121 if (mStreamId == nullptr) return AAUDIO_ERROR_NULL;
122 aaudio_result_t result = fake_hal_start(mStreamId);
123 sendServiceEvent(AAUDIO_SERVICE_EVENT_STARTED);
124 mState = AAUDIO_STREAM_STATE_STARTED;
125 if (result == AAUDIO_OK) {
Phil Burkdec33ab2017-01-17 14:48:16 -0800126 mThreadEnabled.store(true);
Phil Burk5ed503c2017-02-01 09:38:15 -0800127 result = mAAudioThread.start(this);
Phil Burkdec33ab2017-01-17 14:48:16 -0800128 }
Phil Burk2355edb2016-12-26 13:54:02 -0800129 return result;
130}
131
132/**
133 * Stop the flow of data such that start() can resume with loss of data.
134 */
Phil Burk5ed503c2017-02-01 09:38:15 -0800135aaudio_result_t AAudioServiceStreamFakeHal::pause() {
136 if (mStreamId == nullptr) return AAUDIO_ERROR_NULL;
Phil Burk2355edb2016-12-26 13:54:02 -0800137 sendCurrentTimestamp();
Phil Burk5ed503c2017-02-01 09:38:15 -0800138 aaudio_result_t result = fake_hal_pause(mStreamId);
139 sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED);
140 mState = AAUDIO_STREAM_STATE_PAUSED;
Phil Burk2355edb2016-12-26 13:54:02 -0800141 mFramesRead.reset32();
Phil Burk5ed503c2017-02-01 09:38:15 -0800142 ALOGD("AAudioServiceStreamFakeHal::pause() sent AAUDIO_SERVICE_EVENT_PAUSED");
Phil Burkdec33ab2017-01-17 14:48:16 -0800143 mThreadEnabled.store(false);
Phil Burk5ed503c2017-02-01 09:38:15 -0800144 result = mAAudioThread.stop();
Phil Burk2355edb2016-12-26 13:54:02 -0800145 return result;
146}
147
148/**
149 * Discard any data held by the underlying HAL or Service.
150 */
Phil Burk5ed503c2017-02-01 09:38:15 -0800151aaudio_result_t AAudioServiceStreamFakeHal::flush() {
152 if (mStreamId == nullptr) return AAUDIO_ERROR_NULL;
Phil Burk2355edb2016-12-26 13:54:02 -0800153 // TODO how do we flush an MMAP/NOIRQ buffer? sync pointers?
Phil Burk5ed503c2017-02-01 09:38:15 -0800154 ALOGD("AAudioServiceStreamFakeHal::pause() send AAUDIO_SERVICE_EVENT_FLUSHED");
155 sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED);
156 mState = AAUDIO_STREAM_STATE_FLUSHED;
157 return AAUDIO_OK;
Phil Burk2355edb2016-12-26 13:54:02 -0800158}
159
Phil Burk5ed503c2017-02-01 09:38:15 -0800160aaudio_result_t AAudioServiceStreamFakeHal::close() {
161 aaudio_result_t result = AAUDIO_OK;
Phil Burk2355edb2016-12-26 13:54:02 -0800162 if (mStreamId != nullptr) {
163 result = fake_hal_close(mStreamId);
164 mStreamId = nullptr;
165 }
166 return result;
167}
168
Phil Burk5ed503c2017-02-01 09:38:15 -0800169void AAudioServiceStreamFakeHal::sendCurrentTimestamp() {
Phil Burk2355edb2016-12-26 13:54:02 -0800170 int frameCounter = 0;
171 int error = fake_hal_get_frame_counter(mStreamId, &frameCounter);
172 if (error < 0) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800173 ALOGE("AAudioServiceStreamFakeHal::sendCurrentTimestamp() error %d",
Phil Burk2355edb2016-12-26 13:54:02 -0800174 error);
175 } else if (frameCounter != mPreviousFrameCounter) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800176 AAudioServiceMessage command;
177 command.what = AAudioServiceMessage::code::TIMESTAMP;
Phil Burk2355edb2016-12-26 13:54:02 -0800178 mFramesRead.update32(frameCounter);
179 command.timestamp.position = mFramesRead.get();
Phil Burk5ed503c2017-02-01 09:38:15 -0800180 ALOGD("AAudioServiceStreamFakeHal::sendCurrentTimestamp() HAL frames = %d, pos = %d",
Phil Burk2355edb2016-12-26 13:54:02 -0800181 frameCounter, (int)mFramesRead.get());
182 command.timestamp.timestamp = AudioClock::getNanoseconds();
183 mUpMessageQueue->getFifoBuffer()->write(&command, 1);
184 mPreviousFrameCounter = frameCounter;
185 }
186}
187
Phil Burkdec33ab2017-01-17 14:48:16 -0800188// implement Runnable
Phil Burk5ed503c2017-02-01 09:38:15 -0800189void AAudioServiceStreamFakeHal::run() {
Phil Burkdec33ab2017-01-17 14:48:16 -0800190 TimestampScheduler timestampScheduler;
191 timestampScheduler.setBurstPeriod(mFramesPerBurst, mSampleRate);
192 timestampScheduler.start(AudioClock::getNanoseconds());
193 while(mThreadEnabled.load()) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800194 aaudio_nanoseconds_t nextTime = timestampScheduler.nextAbsoluteTime();
Phil Burkdec33ab2017-01-17 14:48:16 -0800195 if (AudioClock::getNanoseconds() >= nextTime) {
196 sendCurrentTimestamp();
197 } else {
198 // Sleep until it is time to send the next timestamp.
199 AudioClock::sleepUntilNanoTime(nextTime);
Phil Burk2355edb2016-12-26 13:54:02 -0800200 }
201 }
202}
203