blob: dc6fe905e9ba3ca4d0d93de01e37343f68e94df5 [file] [log] [blame]
Phil Burk204a1632017-01-03 17:23:43 -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 "OboeAudio"
18//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
21#include <stdint.h>
22#include <assert.h>
23
24#include <binder/IServiceManager.h>
Phil Burkdec33ab2017-01-17 14:48:16 -080025#include <utils/Mutex.h>
Phil Burk204a1632017-01-03 17:23:43 -080026
27#include <oboe/OboeAudio.h>
28
29#include "AudioClock.h"
30#include "AudioEndpointParcelable.h"
31#include "binding/OboeStreamRequest.h"
32#include "binding/OboeStreamConfiguration.h"
33#include "binding/IOboeAudioService.h"
34#include "binding/OboeServiceMessage.h"
35
36#include "AudioStreamInternal.h"
37
38#define LOG_TIMESTAMPS 0
39
40using android::String16;
41using android::IServiceManager;
42using android::defaultServiceManager;
43using android::interface_cast;
Phil Burkdec33ab2017-01-17 14:48:16 -080044using android::Mutex;
Phil Burk204a1632017-01-03 17:23:43 -080045
46using namespace oboe;
47
Phil Burkdec33ab2017-01-17 14:48:16 -080048static android::Mutex gServiceLock;
49static sp<IOboeAudioService> gOboeService;
50
51#define OBOE_SERVICE_NAME "OboeAudioService"
52
Phil Burk204a1632017-01-03 17:23:43 -080053// Helper function to get access to the "OboeAudioService" service.
Phil Burkdec33ab2017-01-17 14:48:16 -080054// This code was modeled after frameworks/av/media/libaudioclient/AudioSystem.cpp
55static const sp<IOboeAudioService> getOboeAudioService() {
56 sp<IBinder> binder;
57 Mutex::Autolock _l(gServiceLock);
58 if (gOboeService == 0) {
59 sp<IServiceManager> sm = defaultServiceManager();
60 // Try several times to get the service.
61 int retries = 4;
62 do {
63 binder = sm->getService(String16(OBOE_SERVICE_NAME)); // This will wait a while.
64 if (binder != 0) {
65 break;
66 }
67 } while (retries-- > 0);
68
69 if (binder != 0) {
70 // TODO Add linkToDeath() like in frameworks/av/media/libaudioclient/AudioSystem.cpp
71 // TODO Create a DeathRecipient that disconnects all active streams.
72 gOboeService = interface_cast<IOboeAudioService>(binder);
73 } else {
74 ALOGE("AudioStreamInternal could not get %s", OBOE_SERVICE_NAME);
75 }
76 }
77 return gOboeService;
Phil Burk204a1632017-01-03 17:23:43 -080078}
79
80AudioStreamInternal::AudioStreamInternal()
81 : AudioStream()
82 , mClockModel()
83 , mAudioEndpoint()
84 , mServiceStreamHandle(OBOE_HANDLE_INVALID)
85 , mFramesPerBurst(16)
86{
Phil Burk204a1632017-01-03 17:23:43 -080087}
88
89AudioStreamInternal::~AudioStreamInternal() {
90}
91
92oboe_result_t AudioStreamInternal::open(const AudioStreamBuilder &builder) {
93
Phil Burkdec33ab2017-01-17 14:48:16 -080094 const sp<IOboeAudioService>& service = getOboeAudioService();
95 if (service == 0) return OBOE_ERROR_NO_SERVICE;
96
Phil Burk204a1632017-01-03 17:23:43 -080097 oboe_result_t result = OBOE_OK;
98 OboeStreamRequest request;
99 OboeStreamConfiguration configuration;
100
101 result = AudioStream::open(builder);
102 if (result < 0) {
103 return result;
104 }
105
Phil Burkdec33ab2017-01-17 14:48:16 -0800106 // Build the request to send to the server.
Phil Burk204a1632017-01-03 17:23:43 -0800107 request.setUserId(getuid());
108 request.setProcessId(getpid());
109 request.getConfiguration().setDeviceId(getDeviceId());
110 request.getConfiguration().setSampleRate(getSampleRate());
111 request.getConfiguration().setSamplesPerFrame(getSamplesPerFrame());
112 request.getConfiguration().setAudioFormat(getFormat());
113 request.dump();
114
Phil Burkdec33ab2017-01-17 14:48:16 -0800115 mServiceStreamHandle = service->openStream(request, configuration);
Phil Burk204a1632017-01-03 17:23:43 -0800116 ALOGD("AudioStreamInternal.open(): openStream returned mServiceStreamHandle = 0x%08X",
117 (unsigned int)mServiceStreamHandle);
118 if (mServiceStreamHandle < 0) {
119 result = mServiceStreamHandle;
120 ALOGE("AudioStreamInternal.open(): acquireRealtimeStream oboe_result_t = 0x%08X", result);
121 } else {
122 result = configuration.validate();
123 if (result != OBOE_OK) {
124 close();
125 return result;
126 }
127 // Save results of the open.
128 setSampleRate(configuration.getSampleRate());
129 setSamplesPerFrame(configuration.getSamplesPerFrame());
130 setFormat(configuration.getAudioFormat());
131
132 oboe::AudioEndpointParcelable parcelable;
Phil Burkdec33ab2017-01-17 14:48:16 -0800133 result = service->getStreamDescription(mServiceStreamHandle, parcelable);
Phil Burk204a1632017-01-03 17:23:43 -0800134 if (result != OBOE_OK) {
135 ALOGE("AudioStreamInternal.open(): getStreamDescriptor returns %d", result);
Phil Burkdec33ab2017-01-17 14:48:16 -0800136 service->closeStream(mServiceStreamHandle);
Phil Burk204a1632017-01-03 17:23:43 -0800137 return result;
138 }
139 // resolve parcelable into a descriptor
140 parcelable.resolve(&mEndpointDescriptor);
141
142 // Configure endpoint based on descriptor.
143 mAudioEndpoint.configure(&mEndpointDescriptor);
144
145
146 mFramesPerBurst = mEndpointDescriptor.downDataQueueDescriptor.framesPerBurst;
147 assert(mFramesPerBurst >= 16);
148 assert(mEndpointDescriptor.downDataQueueDescriptor.capacityInFrames < 10 * 1024);
149
150 mClockModel.setSampleRate(getSampleRate());
151 mClockModel.setFramesPerBurst(mFramesPerBurst);
152
153 setState(OBOE_STREAM_STATE_OPEN);
154 }
155 return result;
156}
157
158oboe_result_t AudioStreamInternal::close() {
159 ALOGD("AudioStreamInternal.close(): mServiceStreamHandle = 0x%08X", mServiceStreamHandle);
160 if (mServiceStreamHandle != OBOE_HANDLE_INVALID) {
Phil Burkdec33ab2017-01-17 14:48:16 -0800161 oboe_handle_t serviceStreamHandle = mServiceStreamHandle;
Phil Burk204a1632017-01-03 17:23:43 -0800162 mServiceStreamHandle = OBOE_HANDLE_INVALID;
Phil Burkdec33ab2017-01-17 14:48:16 -0800163 const sp<IOboeAudioService>& oboeService = getOboeAudioService();
164 if (oboeService == 0) return OBOE_ERROR_NO_SERVICE;
165 oboeService->closeStream(serviceStreamHandle);
Phil Burk204a1632017-01-03 17:23:43 -0800166 return OBOE_OK;
167 } else {
Phil Burkdec33ab2017-01-17 14:48:16 -0800168 return OBOE_ERROR_INVALID_HANDLE;
Phil Burk204a1632017-01-03 17:23:43 -0800169 }
170}
171
172oboe_result_t AudioStreamInternal::requestStart()
173{
174 oboe_nanoseconds_t startTime;
175 ALOGD("AudioStreamInternal(): start()");
176 if (mServiceStreamHandle == OBOE_HANDLE_INVALID) {
177 return OBOE_ERROR_INVALID_STATE;
178 }
Phil Burkdec33ab2017-01-17 14:48:16 -0800179 const sp<IOboeAudioService>& oboeService = getOboeAudioService();
180 if (oboeService == 0) return OBOE_ERROR_NO_SERVICE;
Phil Burk204a1632017-01-03 17:23:43 -0800181 startTime = Oboe_getNanoseconds(OBOE_CLOCK_MONOTONIC);
182 mClockModel.start(startTime);
183 processTimestamp(0, startTime);
184 setState(OBOE_STREAM_STATE_STARTING);
Phil Burkdec33ab2017-01-17 14:48:16 -0800185 return oboeService->startStream(mServiceStreamHandle);
Phil Burk204a1632017-01-03 17:23:43 -0800186}
187
188oboe_result_t AudioStreamInternal::requestPause()
189{
190 ALOGD("AudioStreamInternal(): pause()");
191 if (mServiceStreamHandle == OBOE_HANDLE_INVALID) {
192 return OBOE_ERROR_INVALID_STATE;
193 }
Phil Burkdec33ab2017-01-17 14:48:16 -0800194 const sp<IOboeAudioService>& oboeService = getOboeAudioService();
195 if (oboeService == 0) return OBOE_ERROR_NO_SERVICE;
Phil Burk204a1632017-01-03 17:23:43 -0800196 mClockModel.stop(Oboe_getNanoseconds(OBOE_CLOCK_MONOTONIC));
197 setState(OBOE_STREAM_STATE_PAUSING);
Phil Burkdec33ab2017-01-17 14:48:16 -0800198 return oboeService->pauseStream(mServiceStreamHandle);
Phil Burk204a1632017-01-03 17:23:43 -0800199}
200
201oboe_result_t AudioStreamInternal::requestFlush() {
202 ALOGD("AudioStreamInternal(): flush()");
203 if (mServiceStreamHandle == OBOE_HANDLE_INVALID) {
204 return OBOE_ERROR_INVALID_STATE;
205 }
Phil Burkdec33ab2017-01-17 14:48:16 -0800206 const sp<IOboeAudioService>& oboeService = getOboeAudioService();
207 if (oboeService == 0) return OBOE_ERROR_NO_SERVICE;
208setState(OBOE_STREAM_STATE_FLUSHING);
209 return oboeService->flushStream(mServiceStreamHandle);
Phil Burk204a1632017-01-03 17:23:43 -0800210}
211
212void AudioStreamInternal::onFlushFromServer() {
213 ALOGD("AudioStreamInternal(): onFlushFromServer()");
214 oboe_position_frames_t readCounter = mAudioEndpoint.getDownDataReadCounter();
215 oboe_position_frames_t writeCounter = mAudioEndpoint.getDownDataWriteCounter();
216 // Bump offset so caller does not see the retrograde motion in getFramesRead().
217 oboe_position_frames_t framesFlushed = writeCounter - readCounter;
218 mFramesOffsetFromService += framesFlushed;
219 // Flush written frames by forcing writeCounter to readCounter.
220 // This is because we cannot move the read counter in the hardware.
221 mAudioEndpoint.setDownDataWriteCounter(readCounter);
222}
223
224oboe_result_t AudioStreamInternal::requestStop()
225{
226 // TODO better implementation of requestStop()
227 oboe_result_t result = requestPause();
228 if (result == OBOE_OK) {
229 oboe_stream_state_t state;
230 result = waitForStateChange(OBOE_STREAM_STATE_PAUSING,
231 &state,
232 500 * OBOE_NANOS_PER_MILLISECOND);// TODO temporary code
233 if (result == OBOE_OK) {
234 result = requestFlush();
235 }
236 }
237 return result;
238}
239
240oboe_result_t AudioStreamInternal::registerThread() {
241 ALOGD("AudioStreamInternal(): registerThread()");
242 if (mServiceStreamHandle == OBOE_HANDLE_INVALID) {
243 return OBOE_ERROR_INVALID_STATE;
244 }
Phil Burkdec33ab2017-01-17 14:48:16 -0800245 const sp<IOboeAudioService>& oboeService = getOboeAudioService();
246 if (oboeService == 0) return OBOE_ERROR_NO_SERVICE;
247 return oboeService->registerAudioThread(mServiceStreamHandle,
Phil Burk204a1632017-01-03 17:23:43 -0800248 gettid(),
249 getPeriodNanoseconds());
250}
251
252oboe_result_t AudioStreamInternal::unregisterThread() {
253 ALOGD("AudioStreamInternal(): unregisterThread()");
254 if (mServiceStreamHandle == OBOE_HANDLE_INVALID) {
255 return OBOE_ERROR_INVALID_STATE;
256 }
Phil Burkdec33ab2017-01-17 14:48:16 -0800257 const sp<IOboeAudioService>& oboeService = getOboeAudioService();
258 if (oboeService == 0) return OBOE_ERROR_NO_SERVICE;
259 return oboeService->unregisterAudioThread(mServiceStreamHandle, gettid());
Phil Burk204a1632017-01-03 17:23:43 -0800260}
261
262// TODO use oboe_clockid_t all the way down to AudioClock
263oboe_result_t AudioStreamInternal::getTimestamp(clockid_t clockId,
264 oboe_position_frames_t *framePosition,
265 oboe_nanoseconds_t *timeNanoseconds) {
266// TODO implement using real HAL
267 oboe_nanoseconds_t time = AudioClock::getNanoseconds();
268 *framePosition = mClockModel.convertTimeToPosition(time);
269 *timeNanoseconds = time + (10 * OBOE_NANOS_PER_MILLISECOND); // Fake hardware delay
270 return OBOE_OK;
271}
272
273oboe_result_t AudioStreamInternal::updateState() {
274 return processCommands();
275}
276
277#if LOG_TIMESTAMPS
278static void AudioStreamInternal_LogTimestamp(OboeServiceMessage &command) {
279 static int64_t oldPosition = 0;
280 static oboe_nanoseconds_t oldTime = 0;
281 int64_t framePosition = command.timestamp.position;
282 oboe_nanoseconds_t nanoTime = command.timestamp.timestamp;
283 ALOGD("AudioStreamInternal() timestamp says framePosition = %08lld at nanoTime %llu",
284 (long long) framePosition,
285 (long long) nanoTime);
286 int64_t nanosDelta = nanoTime - oldTime;
287 if (nanosDelta > 0 && oldTime > 0) {
288 int64_t framesDelta = framePosition - oldPosition;
289 int64_t rate = (framesDelta * OBOE_NANOS_PER_SECOND) / nanosDelta;
290 ALOGD("AudioStreamInternal() - framesDelta = %08lld", (long long) framesDelta);
291 ALOGD("AudioStreamInternal() - nanosDelta = %08lld", (long long) nanosDelta);
292 ALOGD("AudioStreamInternal() - measured rate = %llu", (unsigned long long) rate);
293 }
294 oldPosition = framePosition;
295 oldTime = nanoTime;
296}
297#endif
298
299oboe_result_t AudioStreamInternal::onTimestampFromServer(OboeServiceMessage *message) {
300 oboe_position_frames_t framePosition = 0;
301#if LOG_TIMESTAMPS
302 AudioStreamInternal_LogTimestamp(command);
303#endif
304 framePosition = message->timestamp.position;
305 processTimestamp(framePosition, message->timestamp.timestamp);
306 return OBOE_OK;
307}
308
309oboe_result_t AudioStreamInternal::onEventFromServer(OboeServiceMessage *message) {
310 oboe_result_t result = OBOE_OK;
311 ALOGD("processCommands() got event %d", message->event.event);
312 switch (message->event.event) {
313 case OBOE_SERVICE_EVENT_STARTED:
314 ALOGD("processCommands() got OBOE_SERVICE_EVENT_STARTED");
315 setState(OBOE_STREAM_STATE_STARTED);
316 break;
317 case OBOE_SERVICE_EVENT_PAUSED:
318 ALOGD("processCommands() got OBOE_SERVICE_EVENT_PAUSED");
319 setState(OBOE_STREAM_STATE_PAUSED);
320 break;
321 case OBOE_SERVICE_EVENT_FLUSHED:
322 ALOGD("processCommands() got OBOE_SERVICE_EVENT_FLUSHED");
323 setState(OBOE_STREAM_STATE_FLUSHED);
324 onFlushFromServer();
325 break;
326 case OBOE_SERVICE_EVENT_CLOSED:
327 ALOGD("processCommands() got OBOE_SERVICE_EVENT_CLOSED");
328 setState(OBOE_STREAM_STATE_CLOSED);
329 break;
330 case OBOE_SERVICE_EVENT_DISCONNECTED:
331 result = OBOE_ERROR_DISCONNECTED;
332 ALOGW("WARNING - processCommands() OBOE_SERVICE_EVENT_DISCONNECTED");
333 break;
334 default:
335 ALOGW("WARNING - processCommands() Unrecognized event = %d",
336 (int) message->event.event);
337 break;
338 }
339 return result;
340}
341
342// Process all the commands coming from the server.
343oboe_result_t AudioStreamInternal::processCommands() {
344 oboe_result_t result = OBOE_OK;
345
Phil Burk204a1632017-01-03 17:23:43 -0800346 while (result == OBOE_OK) {
347 OboeServiceMessage message;
348 if (mAudioEndpoint.readUpCommand(&message) != 1) {
349 break; // no command this time, no problem
350 }
351 switch (message.what) {
352 case OboeServiceMessage::code::TIMESTAMP:
353 result = onTimestampFromServer(&message);
354 break;
355
356 case OboeServiceMessage::code::EVENT:
357 result = onEventFromServer(&message);
358 break;
359
360 default:
361 ALOGW("WARNING - AudioStreamInternal::processCommands() Unrecognized what = %d",
362 (int) message.what);
363 result = OBOE_ERROR_UNEXPECTED_VALUE;
364 break;
365 }
366 }
367 return result;
368}
369
370// Write the data, block if needed and timeoutMillis > 0
371oboe_result_t AudioStreamInternal::write(const void *buffer, int32_t numFrames,
372 oboe_nanoseconds_t timeoutNanoseconds)
373{
374 oboe_result_t result = OBOE_OK;
375 uint8_t* source = (uint8_t*)buffer;
376 oboe_nanoseconds_t currentTimeNanos = AudioClock::getNanoseconds();
377 oboe_nanoseconds_t deadlineNanos = currentTimeNanos + timeoutNanoseconds;
378 int32_t framesLeft = numFrames;
379// ALOGD("AudioStreamInternal::write(%p, %d) at time %08llu , mState = %d ------------------",
380// buffer, numFrames, (unsigned long long) currentTimeNanos, mState);
381
382 // Write until all the data has been written or until a timeout occurs.
383 while (framesLeft > 0) {
384 // The call to writeNow() will not block. It will just write as much as it can.
385 oboe_nanoseconds_t wakeTimeNanos = 0;
386 oboe_result_t framesWritten = writeNow(source, framesLeft,
387 currentTimeNanos, &wakeTimeNanos);
388// ALOGD("AudioStreamInternal::write() writeNow() framesLeft = %d --> framesWritten = %d", framesLeft, framesWritten);
389 if (framesWritten < 0) {
390 result = framesWritten;
391 break;
392 }
393 framesLeft -= (int32_t) framesWritten;
394 source += framesWritten * getBytesPerFrame();
395
396 // Should we block?
397 if (timeoutNanoseconds == 0) {
398 break; // don't block
399 } else if (framesLeft > 0) {
400 //ALOGD("AudioStreamInternal:: original wakeTimeNanos %lld", (long long) wakeTimeNanos);
401 // clip the wake time to something reasonable
402 if (wakeTimeNanos < currentTimeNanos) {
403 wakeTimeNanos = currentTimeNanos;
404 }
405 if (wakeTimeNanos > deadlineNanos) {
406 // If we time out, just return the framesWritten so far.
407 ALOGE("AudioStreamInternal::write(): timed out after %lld nanos", (long long) timeoutNanoseconds);
408 break;
409 }
410
411 //ALOGD("AudioStreamInternal:: sleep until %lld, dur = %lld", (long long) wakeTimeNanos,
412 // (long long) (wakeTimeNanos - currentTimeNanos));
413 AudioClock::sleepForNanos(wakeTimeNanos - currentTimeNanos);
414 currentTimeNanos = AudioClock::getNanoseconds();
415 }
416 }
417
418 // return error or framesWritten
419 return (result < 0) ? result : numFrames - framesLeft;
420}
421
422// Write as much data as we can without blocking.
423oboe_result_t AudioStreamInternal::writeNow(const void *buffer, int32_t numFrames,
424 oboe_nanoseconds_t currentNanoTime, oboe_nanoseconds_t *wakeTimePtr) {
425 {
426 oboe_result_t result = processCommands();
427 if (result != OBOE_OK) {
428 return result;
429 }
430 }
431
432 if (mAudioEndpoint.isOutputFreeRunning()) {
433 // Update data queue based on the timing model.
434 int64_t estimatedReadCounter = mClockModel.convertTimeToPosition(currentNanoTime);
435 mAudioEndpoint.setDownDataReadCounter(estimatedReadCounter);
436 // If the read index passed the write index then consider it an underrun.
437 if (mAudioEndpoint.getFullFramesAvailable() < 0) {
438 mXRunCount++;
439 }
440 }
441 // TODO else query from endpoint cuz set by actual reader, maybe
442
443 // Write some data to the buffer.
444 int32_t framesWritten = mAudioEndpoint.writeDataNow(buffer, numFrames);
445 if (framesWritten > 0) {
446 incrementFramesWritten(framesWritten);
447 }
448 //ALOGD("AudioStreamInternal::writeNow() - tried to write %d frames, wrote %d",
449 // numFrames, framesWritten);
450
451 // Calculate an ideal time to wake up.
452 if (wakeTimePtr != nullptr && framesWritten >= 0) {
453 // By default wake up a few milliseconds from now. // TODO review
454 oboe_nanoseconds_t wakeTime = currentNanoTime + (2 * OBOE_NANOS_PER_MILLISECOND);
455 switch (getState()) {
456 case OBOE_STREAM_STATE_OPEN:
457 case OBOE_STREAM_STATE_STARTING:
458 if (framesWritten != 0) {
459 // Don't wait to write more data. Just prime the buffer.
460 wakeTime = currentNanoTime;
461 }
462 break;
463 case OBOE_STREAM_STATE_STARTED: // When do we expect the next read burst to occur?
464 {
465 uint32_t burstSize = mFramesPerBurst;
466 if (burstSize < 32) {
467 burstSize = 32; // TODO review
468 }
469
470 uint64_t nextReadPosition = mAudioEndpoint.getDownDataReadCounter() + burstSize;
471 wakeTime = mClockModel.convertPositionToTime(nextReadPosition);
472 }
473 break;
474 default:
475 break;
476 }
477 *wakeTimePtr = wakeTime;
478
479 }
480// ALOGD("AudioStreamInternal::writeNow finished: now = %llu, read# = %llu, wrote# = %llu",
481// (unsigned long long)currentNanoTime,
482// (unsigned long long)mAudioEndpoint.getDownDataReadCounter(),
483// (unsigned long long)mAudioEndpoint.getDownDataWriteCounter());
484 return framesWritten;
485}
486
487oboe_result_t AudioStreamInternal::waitForStateChange(oboe_stream_state_t currentState,
488 oboe_stream_state_t *nextState,
489 oboe_nanoseconds_t timeoutNanoseconds)
490
491{
492 oboe_result_t result = processCommands();
493// ALOGD("AudioStreamInternal::waitForStateChange() - processCommands() returned %d", result);
494 if (result != OBOE_OK) {
495 return result;
496 }
497 // TODO replace this polling with a timed sleep on a futex on the message queue
498 int32_t durationNanos = 5 * OBOE_NANOS_PER_MILLISECOND;
499 oboe_stream_state_t state = getState();
500// ALOGD("AudioStreamInternal::waitForStateChange() - state = %d", state);
501 while (state == currentState && timeoutNanoseconds > 0) {
502 // TODO use futex from service message queue
503 if (durationNanos > timeoutNanoseconds) {
504 durationNanos = timeoutNanoseconds;
505 }
506 AudioClock::sleepForNanos(durationNanos);
507 timeoutNanoseconds -= durationNanos;
508
509 result = processCommands();
510 if (result != OBOE_OK) {
511 return result;
512 }
513
514 state = getState();
515// ALOGD("AudioStreamInternal::waitForStateChange() - state = %d", state);
516 }
517 if (nextState != nullptr) {
518 *nextState = state;
519 }
520 return (state == currentState) ? OBOE_ERROR_TIMEOUT : OBOE_OK;
521}
522
523
524void AudioStreamInternal::processTimestamp(uint64_t position, oboe_nanoseconds_t time) {
525 mClockModel.processTimestamp( position, time);
526}
527
528oboe_result_t AudioStreamInternal::setBufferSize(oboe_size_frames_t requestedFrames,
529 oboe_size_frames_t *actualFrames) {
530 return mAudioEndpoint.setBufferSizeInFrames(requestedFrames, actualFrames);
531}
532
533oboe_size_frames_t AudioStreamInternal::getBufferSize() const
534{
535 return mAudioEndpoint.getBufferSizeInFrames();
536}
537
538oboe_size_frames_t AudioStreamInternal::getBufferCapacity() const
539{
540 return mAudioEndpoint.getBufferCapacityInFrames();
541}
542
543oboe_size_frames_t AudioStreamInternal::getFramesPerBurst() const
544{
545 return mEndpointDescriptor.downDataQueueDescriptor.framesPerBurst;
546}
547
548oboe_position_frames_t AudioStreamInternal::getFramesRead()
549{
550 oboe_position_frames_t framesRead =
551 mClockModel.convertTimeToPosition(AudioClock::getNanoseconds())
552 + mFramesOffsetFromService;
553 // Prevent retrograde motion.
554 if (framesRead < mLastFramesRead) {
555 framesRead = mLastFramesRead;
556 } else {
557 mLastFramesRead = framesRead;
558 }
559 ALOGD("AudioStreamInternal::getFramesRead() returns %lld", (long long)framesRead);
560 return framesRead;
561}
562
563// TODO implement getTimestamp