blob: 627a5045a57d372b57bcf50221717a34be562074 [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,
56 aaudio::AAudioStreamConfiguration &configuration) {
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
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 Burk5ed503c2017-02-01 09:38:15 -080082 ALOGD("AAudioServiceStreamFakeHal::open() mmapInfo.burst_size_in_frames = %d",
Phil Burk2355edb2016-12-26 13:54:02 -080083 mmapInfo.burst_size_in_frames);
Phil Burk5ed503c2017-02-01 09:38:15 -080084 ALOGD("AAudioServiceStreamFakeHal::open() mmapInfo.buffer_capacity_in_frames = %d",
Phil Burk2355edb2016-12-26 13:54:02 -080085 mmapInfo.buffer_capacity_in_frames);
Phil Burk5ed503c2017-02-01 09:38:15 -080086 ALOGD("AAudioServiceStreamFakeHal::open() mmapInfo.buffer_capacity_in_bytes = %d",
Phil Burk2355edb2016-12-26 13:54:02 -080087 mmapInfo.buffer_capacity_in_bytes);
88
Phil Burk5ed503c2017-02-01 09:38:15 -080089 // Fill in AAudioStreamConfiguration
Phil Burk2355edb2016-12-26 13:54:02 -080090 configuration.setSampleRate(mSampleRate);
91 configuration.setSamplesPerFrame(mmapInfo.channel_count);
Phil Burk5ed503c2017-02-01 09:38:15 -080092 configuration.setAudioFormat(AAUDIO_FORMAT_PCM_I16);
Phil Burkdec33ab2017-01-17 14:48:16 -080093
Phil Burk5ed503c2017-02-01 09:38:15 -080094 return AAUDIO_OK;
Phil Burk2355edb2016-12-26 13:54:02 -080095}
96
97/**
98 * Get an immutable description of the in-memory queues
99 * used to communicate with the underlying HAL or Service.
100 */
Phil Burk5ed503c2017-02-01 09:38:15 -0800101aaudio_result_t AAudioServiceStreamFakeHal::getDescription(AudioEndpointParcelable &parcelable) {
Phil Burk2355edb2016-12-26 13:54:02 -0800102 // 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 Burk5ed503c2017-02-01 09:38:15 -0800113 return AAUDIO_OK;
Phil Burk2355edb2016-12-26 13:54:02 -0800114}
115
116/**
117 * Start the flow of data.
118 */
Phil Burk5ed503c2017-02-01 09:38:15 -0800119aaudio_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 Burkdec33ab2017-01-17 14:48:16 -0800125 mThreadEnabled.store(true);
Phil Burk5ed503c2017-02-01 09:38:15 -0800126 result = mAAudioThread.start(this);
Phil Burkdec33ab2017-01-17 14:48:16 -0800127 }
Phil Burk2355edb2016-12-26 13:54:02 -0800128 return result;
129}
130
131/**
132 * Stop the flow of data such that start() can resume with loss of data.
133 */
Phil Burk5ed503c2017-02-01 09:38:15 -0800134aaudio_result_t AAudioServiceStreamFakeHal::pause() {
135 if (mStreamId == nullptr) return AAUDIO_ERROR_NULL;
Phil Burk2355edb2016-12-26 13:54:02 -0800136 sendCurrentTimestamp();
Phil Burk5ed503c2017-02-01 09:38:15 -0800137 aaudio_result_t result = fake_hal_pause(mStreamId);
138 sendServiceEvent(AAUDIO_SERVICE_EVENT_PAUSED);
139 mState = AAUDIO_STREAM_STATE_PAUSED;
Phil Burk2355edb2016-12-26 13:54:02 -0800140 mFramesRead.reset32();
Phil Burk5ed503c2017-02-01 09:38:15 -0800141 ALOGD("AAudioServiceStreamFakeHal::pause() sent AAUDIO_SERVICE_EVENT_PAUSED");
Phil Burkdec33ab2017-01-17 14:48:16 -0800142 mThreadEnabled.store(false);
Phil Burk5ed503c2017-02-01 09:38:15 -0800143 result = mAAudioThread.stop();
Phil Burk2355edb2016-12-26 13:54:02 -0800144 return result;
145}
146
147/**
148 * Discard any data held by the underlying HAL or Service.
149 */
Phil Burk5ed503c2017-02-01 09:38:15 -0800150aaudio_result_t AAudioServiceStreamFakeHal::flush() {
151 if (mStreamId == nullptr) return AAUDIO_ERROR_NULL;
Phil Burk2355edb2016-12-26 13:54:02 -0800152 // TODO how do we flush an MMAP/NOIRQ buffer? sync pointers?
Phil Burk5ed503c2017-02-01 09:38:15 -0800153 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 Burk2355edb2016-12-26 13:54:02 -0800157}
158
Phil Burk5ed503c2017-02-01 09:38:15 -0800159aaudio_result_t AAudioServiceStreamFakeHal::close() {
160 aaudio_result_t result = AAUDIO_OK;
Phil Burk2355edb2016-12-26 13:54:02 -0800161 if (mStreamId != nullptr) {
162 result = fake_hal_close(mStreamId);
163 mStreamId = nullptr;
164 }
165 return result;
166}
167
Phil Burk5ed503c2017-02-01 09:38:15 -0800168void AAudioServiceStreamFakeHal::sendCurrentTimestamp() {
Phil Burk2355edb2016-12-26 13:54:02 -0800169 int frameCounter = 0;
170 int error = fake_hal_get_frame_counter(mStreamId, &frameCounter);
171 if (error < 0) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800172 ALOGE("AAudioServiceStreamFakeHal::sendCurrentTimestamp() error %d",
Phil Burk2355edb2016-12-26 13:54:02 -0800173 error);
174 } else if (frameCounter != mPreviousFrameCounter) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800175 AAudioServiceMessage command;
176 command.what = AAudioServiceMessage::code::TIMESTAMP;
Phil Burk2355edb2016-12-26 13:54:02 -0800177 mFramesRead.update32(frameCounter);
178 command.timestamp.position = mFramesRead.get();
Phil Burk5ed503c2017-02-01 09:38:15 -0800179 ALOGD("AAudioServiceStreamFakeHal::sendCurrentTimestamp() HAL frames = %d, pos = %d",
Phil Burk2355edb2016-12-26 13:54:02 -0800180 frameCounter, (int)mFramesRead.get());
181 command.timestamp.timestamp = AudioClock::getNanoseconds();
182 mUpMessageQueue->getFifoBuffer()->write(&command, 1);
183 mPreviousFrameCounter = frameCounter;
184 }
185}
186
Phil Burkdec33ab2017-01-17 14:48:16 -0800187// implement Runnable
Phil Burk5ed503c2017-02-01 09:38:15 -0800188void AAudioServiceStreamFakeHal::run() {
Phil Burkdec33ab2017-01-17 14:48:16 -0800189 TimestampScheduler timestampScheduler;
190 timestampScheduler.setBurstPeriod(mFramesPerBurst, mSampleRate);
191 timestampScheduler.start(AudioClock::getNanoseconds());
192 while(mThreadEnabled.load()) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800193 aaudio_nanoseconds_t nextTime = timestampScheduler.nextAbsoluteTime();
Phil Burkdec33ab2017-01-17 14:48:16 -0800194 if (AudioClock::getNanoseconds() >= nextTime) {
195 sendCurrentTimestamp();
196 } else {
197 // Sleep until it is time to send the next timestamp.
198 AudioClock::sleepUntilNanoTime(nextTime);
Phil Burk2355edb2016-12-26 13:54:02 -0800199 }
200 }
201}
202