blob: da4099dfffdaa2233aa202a46b117af56a28d016 [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
17#define LOG_TAG "OboeService"
18//#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
26#include "OboeServiceStreamBase.h"
27#include "OboeServiceStreamFakeHal.h"
28
29#include "FakeAudioHal.h"
30
31using namespace android;
32using 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
42OboeServiceStreamFakeHal::OboeServiceStreamFakeHal()
43 : OboeServiceStreamBase()
44 , mStreamId(nullptr)
45 , mPreviousFrameCounter(0)
Phil Burkdec33ab2017-01-17 14:48:16 -080046 , mOboeThread()
Phil Burk2355edb2016-12-26 13:54:02 -080047{
48}
49
50OboeServiceStreamFakeHal::~OboeServiceStreamFakeHal() {
51 ALOGD("OboeServiceStreamFakeHal::~OboeServiceStreamFakeHal() call close()");
52 close();
53}
54
55oboe_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 Burkdec33ab2017-01-17 14:48:16 -080092 configuration.setAudioFormat(OBOE_AUDIO_FORMAT_PCM_I16);
93
Phil Burk2355edb2016-12-26 13:54:02 -080094 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 */
101oboe_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 */
119oboe_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 Burkdec33ab2017-01-17 14:48:16 -0800124 if (result == OBOE_OK) {
125 mThreadEnabled.store(true);
126 result = mOboeThread.start(this);
127 }
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 */
134oboe_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 Burkdec33ab2017-01-17 14:48:16 -0800142 mThreadEnabled.store(false);
143 result = mOboeThread.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 */
150oboe_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
159oboe_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
168void 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 Burkdec33ab2017-01-17 14:48:16 -0800179 ALOGD("OboeServiceStreamFakeHal::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
188void 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 Burk2355edb2016-12-26 13:54:02 -0800199 }
200 }
201}
202