AudioStreamInternal.cpp revision cf5f6d2825d9a8430a291042ca9c6f68e5b666d0
1204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk/* 2204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * Copyright (C) 2016 The Android Open Source Project 3204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * 4204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * Licensed under the Apache License, Version 2.0 (the "License"); 5204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * you may not use this file except in compliance with the License. 6204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * You may obtain a copy of the License at 7204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * 8204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * http://www.apache.org/licenses/LICENSE-2.0 9204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * 10204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * Unless required by applicable law or agreed to in writing, software 11204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * distributed under the License is distributed on an "AS IS" BASIS, 12204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * See the License for the specific language governing permissions and 14204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * limitations under the License. 15204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk */ 16204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 17cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk// This file is used in both client and server processes. 18cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk// This is needed to make sense of the logs more easily. 19cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk#define LOG_TAG (mInService ? "AAudioService" : "AAudio") 20204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk//#define LOG_NDEBUG 0 21204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#include <utils/Log.h> 22204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 234485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk#define ATRACE_TAG ATRACE_TAG_AUDIO 244485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk 25c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include <stdint.h> 26204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#include <assert.h> 27204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 28204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#include <binder/IServiceManager.h> 29204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 305ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include <aaudio/AAudio.h> 31e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk#include <utils/String16.h> 324485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk#include <utils/Trace.h> 33204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 34c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include "AudioClock.h" 35c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include "AudioEndpointParcelable.h" 36c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include "binding/AAudioStreamRequest.h" 37c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include "binding/AAudioStreamConfiguration.h" 38c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include "binding/IAAudioService.h" 395ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include "binding/AAudioServiceMessage.h" 40e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk#include "core/AudioStreamBuilder.h" 41c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include "fifo/FifoBuffer.h" 42e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk#include "utility/LinearRamp.h" 43204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 44c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include "AudioStreamInternal.h" 45204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 46204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkusing android::String16; 47dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burkusing android::Mutex; 48c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burkusing android::WrappingBuffer; 49204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 505ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkusing namespace aaudio; 51204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 52e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk#define MIN_TIMEOUT_NANOS (1000 * AAUDIO_NANOS_PER_MILLISECOND) 53e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk 54e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk// Wait at least this many times longer than the operation should take. 55e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk#define MIN_TIMEOUT_OPERATIONS 4 56e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk 5787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk#define LOG_TIMESTAMPS 0 5887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk 59c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil BurkAudioStreamInternal::AudioStreamInternal(AAudioServiceInterface &serviceInterface, bool inService) 60204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk : AudioStream() 61204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk , mClockModel() 62204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk , mAudioEndpoint() 635ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk , mServiceStreamHandle(AAUDIO_HANDLE_INVALID) 64204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk , mFramesPerBurst(16) 65c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk , mServiceInterface(serviceInterface) 6671f35bb687476694882a617ba4a810a0bb56fe23Phil Burk , mInService(inService) { 67204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk} 68204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 69204a163c86f357a878873fe7d4c4164f3d55c9b6Phil BurkAudioStreamInternal::~AudioStreamInternal() { 70204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk} 71204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 725ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkaaudio_result_t AudioStreamInternal::open(const AudioStreamBuilder &builder) { 73204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 745ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk aaudio_result_t result = AAUDIO_OK; 755ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk AAudioStreamRequest request; 765ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk AAudioStreamConfiguration configuration; 77204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 78204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk result = AudioStream::open(builder); 79204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk if (result < 0) { 80204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk return result; 81204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } 82204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 83c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk // We have to do volume scaling. So we prefer FLOAT format. 84c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk if (getFormat() == AAUDIO_UNSPECIFIED) { 85c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk setFormat(AAUDIO_FORMAT_PCM_FLOAT); 86c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk } 8771f35bb687476694882a617ba4a810a0bb56fe23Phil Burk // Request FLOAT for the shared mixer. 8871f35bb687476694882a617ba4a810a0bb56fe23Phil Burk request.getConfiguration().setAudioFormat(AAUDIO_FORMAT_PCM_FLOAT); 89c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk 90dec33abe3739b2116ef6fbac36f7ca5d26f9d190Phil Burk // Build the request to send to the server. 91204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk request.setUserId(getuid()); 92204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk request.setProcessId(getpid()); 93c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk request.setDirection(getDirection()); 9471f35bb687476694882a617ba4a810a0bb56fe23Phil Burk request.setSharingModeMatchRequired(isSharingModeMatchRequired()); 95c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk 96204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk request.getConfiguration().setDeviceId(getDeviceId()); 97204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk request.getConfiguration().setSampleRate(getSampleRate()); 98204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk request.getConfiguration().setSamplesPerFrame(getSamplesPerFrame()); 9971f35bb687476694882a617ba4a810a0bb56fe23Phil Burk request.getConfiguration().setSharingMode(getSharingMode()); 10071f35bb687476694882a617ba4a810a0bb56fe23Phil Burk 1013df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk request.getConfiguration().setBufferCapacity(builder.getBufferCapacity()); 102204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 103c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk mServiceStreamHandle = mServiceInterface.openStream(request, configuration); 104204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk if (mServiceStreamHandle < 0) { 105204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk result = mServiceStreamHandle; 106cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk ALOGE("AudioStreamInternal.open(): openStream() returned %d", result); 107204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } else { 108204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk result = configuration.validate(); 1095ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk if (result != AAUDIO_OK) { 110204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk close(); 111204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk return result; 112204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } 113204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk // Save results of the open. 114204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk setSampleRate(configuration.getSampleRate()); 115204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk setSamplesPerFrame(configuration.getSamplesPerFrame()); 116c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk setDeviceId(configuration.getDeviceId()); 117204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 118c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk // Save device format so we can do format conversion and volume scaling together. 119c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk mDeviceFormat = configuration.getAudioFormat(); 120c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk 121c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk result = mServiceInterface.getStreamDescription(mServiceStreamHandle, mEndPointParcelable); 1225ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk if (result != AAUDIO_OK) { 123c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk mServiceInterface.closeStream(mServiceStreamHandle); 124204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk return result; 125204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } 126c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk 127204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk // resolve parcelable into a descriptor 128c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk result = mEndPointParcelable.resolve(&mEndpointDescriptor); 129c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk if (result != AAUDIO_OK) { 130c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk mServiceInterface.closeStream(mServiceStreamHandle); 131c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk return result; 132c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk } 133204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 134204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk // Configure endpoint based on descriptor. 135204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk mAudioEndpoint.configure(&mEndpointDescriptor); 136204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 13787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk mFramesPerBurst = mEndpointDescriptor.dataQueueDescriptor.framesPerBurst; 13887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk int32_t capacity = mEndpointDescriptor.dataQueueDescriptor.capacityInFrames; 13971f35bb687476694882a617ba4a810a0bb56fe23Phil Burk 14071f35bb687476694882a617ba4a810a0bb56fe23Phil Burk // Validate result from server. 14171f35bb687476694882a617ba4a810a0bb56fe23Phil Burk if (mFramesPerBurst < 16 || mFramesPerBurst > 16 * 1024) { 14271f35bb687476694882a617ba4a810a0bb56fe23Phil Burk ALOGE("AudioStream::open(): framesPerBurst out of range = %d", mFramesPerBurst); 14371f35bb687476694882a617ba4a810a0bb56fe23Phil Burk return AAUDIO_ERROR_OUT_OF_RANGE; 14471f35bb687476694882a617ba4a810a0bb56fe23Phil Burk } 14571f35bb687476694882a617ba4a810a0bb56fe23Phil Burk if (capacity < mFramesPerBurst || capacity > 32 * 1024) { 14671f35bb687476694882a617ba4a810a0bb56fe23Phil Burk ALOGE("AudioStream::open(): bufferCapacity out of range = %d", capacity); 14771f35bb687476694882a617ba4a810a0bb56fe23Phil Burk return AAUDIO_ERROR_OUT_OF_RANGE; 14871f35bb687476694882a617ba4a810a0bb56fe23Phil Burk } 149204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 150204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk mClockModel.setSampleRate(getSampleRate()); 151204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk mClockModel.setFramesPerBurst(mFramesPerBurst); 152204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 153e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk if (getDataCallbackProc()) { 154e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk mCallbackFrames = builder.getFramesPerDataCallback(); 155e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk if (mCallbackFrames > getBufferCapacity() / 2) { 15671f35bb687476694882a617ba4a810a0bb56fe23Phil Burk ALOGE("AudioStreamInternal.open(): framesPerCallback too large = %d, capacity = %d", 15771f35bb687476694882a617ba4a810a0bb56fe23Phil Burk mCallbackFrames, getBufferCapacity()); 158c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk mServiceInterface.closeStream(mServiceStreamHandle); 159e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk return AAUDIO_ERROR_OUT_OF_RANGE; 160e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk 161e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk } else if (mCallbackFrames < 0) { 162e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk ALOGE("AudioStreamInternal.open(): framesPerCallback negative"); 163c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk mServiceInterface.closeStream(mServiceStreamHandle); 164e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk return AAUDIO_ERROR_OUT_OF_RANGE; 165e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk 166e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk } 167e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk if (mCallbackFrames == AAUDIO_UNSPECIFIED) { 168e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk mCallbackFrames = mFramesPerBurst; 169e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk } 170e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk 171e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk int32_t bytesPerFrame = getSamplesPerFrame() 172e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk * AAudioConvert_formatToSizeInBytes(getFormat()); 173e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk int32_t callbackBufferSize = mCallbackFrames * bytesPerFrame; 174e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk mCallbackBuffer = new uint8_t[callbackBufferSize]; 175e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk } 176e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk 1775ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk setState(AAUDIO_STREAM_STATE_OPEN); 178204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } 179204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk return result; 180204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk} 181204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 1825ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkaaudio_result_t AudioStreamInternal::close() { 183cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk ALOGD("AudioStreamInternal.close(): mServiceStreamHandle = 0x%08X", 18471f35bb687476694882a617ba4a810a0bb56fe23Phil Burk mServiceStreamHandle); 1855ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk if (mServiceStreamHandle != AAUDIO_HANDLE_INVALID) { 1864485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk // Don't close a stream while it is running. 1874485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk aaudio_stream_state_t currentState = getState(); 18887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk if (isActive()) { 1894485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk requestStop(); 1904485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk aaudio_stream_state_t nextState; 1914485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk int64_t timeoutNanoseconds = MIN_TIMEOUT_NANOS; 1924485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk aaudio_result_t result = waitForStateChange(currentState, &nextState, 1934485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk timeoutNanoseconds); 1944485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk if (result != AAUDIO_OK) { 1954485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk ALOGE("AudioStreamInternal::close() waitForStateChange() returned %d %s", 1964485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk result, AAudio_convertResultToText(result)); 1974485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk } 1984485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk } 1995ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk aaudio_handle_t serviceStreamHandle = mServiceStreamHandle; 2005ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk mServiceStreamHandle = AAUDIO_HANDLE_INVALID; 201c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk 202c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk mServiceInterface.closeStream(serviceStreamHandle); 203e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk delete[] mCallbackBuffer; 2044485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk mCallbackBuffer = nullptr; 205c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk return mEndPointParcelable.close(); 206204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } else { 2075ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk return AAUDIO_ERROR_INVALID_HANDLE; 208204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } 209204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk} 210204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 211c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk 212e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burkstatic void *aaudio_callback_thread_proc(void *context) 213e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk{ 214e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk AudioStreamInternal *stream = (AudioStreamInternal *)context; 215677d7916c0fa6f0955aae8f3ef921383e285beb2Phil Burk //LOGD("AudioStreamInternal(): oboe_callback_thread, stream = %p", stream); 216e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk if (stream != NULL) { 217e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk return stream->callbackLoop(); 218e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk } else { 219e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk return NULL; 220e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk } 221e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk} 222e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk 2235ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkaaudio_result_t AudioStreamInternal::requestStart() 224204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk{ 2253316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk int64_t startTime; 226cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk ALOGD("AudioStreamInternal(): start()"); 2275ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { 2285ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk return AAUDIO_ERROR_INVALID_STATE; 229204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } 230c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk 2313316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk startTime = AudioClock::getNanoseconds(); 232204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk mClockModel.start(startTime); 2335ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk setState(AAUDIO_STREAM_STATE_STARTING); 234c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk aaudio_result_t result = mServiceInterface.startStream(mServiceStreamHandle);; 235e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk 236e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk if (result == AAUDIO_OK && getDataCallbackProc() != nullptr) { 237e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk // Launch the callback loop thread. 238e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk int64_t periodNanos = mCallbackFrames 239e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk * AAUDIO_NANOS_PER_SECOND 240e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk / getSampleRate(); 241e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk mCallbackEnabled.store(true); 242e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk result = createThread(periodNanos, aaudio_callback_thread_proc, this); 243e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk } 244e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk return result; 245204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk} 246204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 247e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burkint64_t AudioStreamInternal::calculateReasonableTimeout(int32_t framesPerOperation) { 248e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk 249e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk // Wait for at least a second or some number of callbacks to join the thread. 25071f35bb687476694882a617ba4a810a0bb56fe23Phil Burk int64_t timeoutNanoseconds = (MIN_TIMEOUT_OPERATIONS 25171f35bb687476694882a617ba4a810a0bb56fe23Phil Burk * framesPerOperation 25271f35bb687476694882a617ba4a810a0bb56fe23Phil Burk * AAUDIO_NANOS_PER_SECOND) 25371f35bb687476694882a617ba4a810a0bb56fe23Phil Burk / getSampleRate(); 254e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk if (timeoutNanoseconds < MIN_TIMEOUT_NANOS) { // arbitrary number of seconds 255e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk timeoutNanoseconds = MIN_TIMEOUT_NANOS; 256e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk } 257e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk return timeoutNanoseconds; 258e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk} 259e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk 26087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burkint64_t AudioStreamInternal::calculateReasonableTimeout() { 26187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk return calculateReasonableTimeout(getFramesPerBurst()); 26287c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk} 26387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk 264e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burkaaudio_result_t AudioStreamInternal::stopCallback() 265e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk{ 266e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk if (isDataCallbackActive()) { 267e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk mCallbackEnabled.store(false); 26887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk return joinThread(NULL); 269e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk } else { 270e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk return AAUDIO_OK; 271e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk } 272e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk} 273e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk 274e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burkaaudio_result_t AudioStreamInternal::requestPauseInternal() 275204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk{ 2765ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { 27771f35bb687476694882a617ba4a810a0bb56fe23Phil Burk ALOGE("AudioStreamInternal(): requestPauseInternal() mServiceStreamHandle invalid = 0x%08X", 27871f35bb687476694882a617ba4a810a0bb56fe23Phil Burk mServiceStreamHandle); 2795ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk return AAUDIO_ERROR_INVALID_STATE; 280204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } 281c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk 2823316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk mClockModel.stop(AudioClock::getNanoseconds()); 2835ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk setState(AAUDIO_STREAM_STATE_PAUSING); 28471f35bb687476694882a617ba4a810a0bb56fe23Phil Burk return mServiceInterface.pauseStream(mServiceStreamHandle); 285204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk} 286204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 287e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burkaaudio_result_t AudioStreamInternal::requestPause() 288e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk{ 289e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk aaudio_result_t result = stopCallback(); 290e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk if (result != AAUDIO_OK) { 291e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk return result; 292e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk } 29371f35bb687476694882a617ba4a810a0bb56fe23Phil Burk result = requestPauseInternal(); 29471f35bb687476694882a617ba4a810a0bb56fe23Phil Burk return result; 295e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk} 296e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk 2975ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkaaudio_result_t AudioStreamInternal::requestFlush() { 2985ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { 29971f35bb687476694882a617ba4a810a0bb56fe23Phil Burk ALOGE("AudioStreamInternal(): requestFlush() mServiceStreamHandle invalid = 0x%08X", 30071f35bb687476694882a617ba4a810a0bb56fe23Phil Burk mServiceStreamHandle); 3015ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk return AAUDIO_ERROR_INVALID_STATE; 302204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } 303c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk 304e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk setState(AAUDIO_STREAM_STATE_FLUSHING); 305c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk return mServiceInterface.flushStream(mServiceStreamHandle); 306204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk} 307204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 30887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk// TODO for Play only 309204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkvoid AudioStreamInternal::onFlushFromServer() { 310cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk ALOGD("AudioStreamInternal(): onFlushFromServer()"); 31187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk int64_t readCounter = mAudioEndpoint.getDataReadCounter(); 31287c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk int64_t writeCounter = mAudioEndpoint.getDataWriteCounter(); 31371f35bb687476694882a617ba4a810a0bb56fe23Phil Burk 314204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk // Bump offset so caller does not see the retrograde motion in getFramesRead(). 3153316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk int64_t framesFlushed = writeCounter - readCounter; 316204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk mFramesOffsetFromService += framesFlushed; 31771f35bb687476694882a617ba4a810a0bb56fe23Phil Burk 318204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk // Flush written frames by forcing writeCounter to readCounter. 319204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk // This is because we cannot move the read counter in the hardware. 32087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk mAudioEndpoint.setDataWriteCounter(readCounter); 321204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk} 322204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 32371f35bb687476694882a617ba4a810a0bb56fe23Phil Burkaaudio_result_t AudioStreamInternal::requestStopInternal() 32471f35bb687476694882a617ba4a810a0bb56fe23Phil Burk{ 32571f35bb687476694882a617ba4a810a0bb56fe23Phil Burk if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { 32671f35bb687476694882a617ba4a810a0bb56fe23Phil Burk ALOGE("AudioStreamInternal(): requestStopInternal() mServiceStreamHandle invalid = 0x%08X", 32771f35bb687476694882a617ba4a810a0bb56fe23Phil Burk mServiceStreamHandle); 32871f35bb687476694882a617ba4a810a0bb56fe23Phil Burk return AAUDIO_ERROR_INVALID_STATE; 32971f35bb687476694882a617ba4a810a0bb56fe23Phil Burk } 33071f35bb687476694882a617ba4a810a0bb56fe23Phil Burk 33171f35bb687476694882a617ba4a810a0bb56fe23Phil Burk mClockModel.stop(AudioClock::getNanoseconds()); 33271f35bb687476694882a617ba4a810a0bb56fe23Phil Burk setState(AAUDIO_STREAM_STATE_STOPPING); 33371f35bb687476694882a617ba4a810a0bb56fe23Phil Burk return mServiceInterface.stopStream(mServiceStreamHandle); 33471f35bb687476694882a617ba4a810a0bb56fe23Phil Burk} 33571f35bb687476694882a617ba4a810a0bb56fe23Phil Burk 3365ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkaaudio_result_t AudioStreamInternal::requestStop() 337204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk{ 33871f35bb687476694882a617ba4a810a0bb56fe23Phil Burk aaudio_result_t result = stopCallback(); 33971f35bb687476694882a617ba4a810a0bb56fe23Phil Burk if (result != AAUDIO_OK) { 34071f35bb687476694882a617ba4a810a0bb56fe23Phil Burk return result; 341204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } 34271f35bb687476694882a617ba4a810a0bb56fe23Phil Burk result = requestStopInternal(); 343204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk return result; 344204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk} 345204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 3465ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkaaudio_result_t AudioStreamInternal::registerThread() { 3475ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { 3485ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk return AAUDIO_ERROR_INVALID_STATE; 349204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } 350c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk return mServiceInterface.registerAudioThread(mServiceStreamHandle, 351c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk getpid(), 352c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk gettid(), 353c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk getPeriodNanoseconds()); 354204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk} 355204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 3565ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkaaudio_result_t AudioStreamInternal::unregisterThread() { 3575ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { 3585ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk return AAUDIO_ERROR_INVALID_STATE; 359204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } 360c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk return mServiceInterface.unregisterAudioThread(mServiceStreamHandle, getpid(), gettid()); 361204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk} 362204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 3635ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkaaudio_result_t AudioStreamInternal::getTimestamp(clockid_t clockId, 3643316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk int64_t *framePosition, 3653316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk int64_t *timeNanoseconds) { 3665204d315c6c6f53188f8d1414dd1b55b6c90142bPhil Burk // TODO Generate in server and pass to client. Return latest. 3673316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk int64_t time = AudioClock::getNanoseconds(); 368204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk *framePosition = mClockModel.convertTimeToPosition(time); 36987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk // TODO Get a more accurate timestamp from the service. This code just adds a fudge factor. 37087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk *timeNanoseconds = time + (6 * AAUDIO_NANOS_PER_MILLISECOND); 3715ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk return AAUDIO_OK; 372204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk} 373204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 374e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burkaaudio_result_t AudioStreamInternal::updateStateWhileWaiting() { 375e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk if (isDataCallbackActive()) { 376e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk return AAUDIO_OK; // state is getting updated by the callback thread read/write call 377e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk } 378204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk return processCommands(); 379204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk} 380204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 381204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#if LOG_TIMESTAMPS 38287c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burkstatic void AudioStreamInternal_logTimestamp(AAudioServiceMessage &command) { 383204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk static int64_t oldPosition = 0; 3843316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk static int64_t oldTime = 0; 385204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk int64_t framePosition = command.timestamp.position; 3863316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk int64_t nanoTime = command.timestamp.timestamp; 387cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk ALOGD("AudioStreamInternal() timestamp says framePosition = %08lld at nanoTime %lld", 388204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk (long long) framePosition, 389204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk (long long) nanoTime); 390204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk int64_t nanosDelta = nanoTime - oldTime; 391204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk if (nanosDelta > 0 && oldTime > 0) { 392204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk int64_t framesDelta = framePosition - oldPosition; 3935ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk int64_t rate = (framesDelta * AAUDIO_NANOS_PER_SECOND) / nanosDelta; 394cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk ALOGD("AudioStreamInternal() - framesDelta = %08lld", (long long) framesDelta); 395cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk ALOGD("AudioStreamInternal() - nanosDelta = %08lld", (long long) nanosDelta); 396cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk ALOGD("AudioStreamInternal() - measured rate = %lld", (long long) rate); 397204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } 398204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk oldPosition = framePosition; 399204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk oldTime = nanoTime; 400204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk} 401204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#endif 402204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 4035ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkaaudio_result_t AudioStreamInternal::onTimestampFromServer(AAudioServiceMessage *message) { 404204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#if LOG_TIMESTAMPS 40587c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk AudioStreamInternal_logTimestamp(*message); 406204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#endif 40787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk processTimestamp(message->timestamp.position, message->timestamp.timestamp); 4085ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk return AAUDIO_OK; 409204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk} 410204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 4115ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkaaudio_result_t AudioStreamInternal::onEventFromServer(AAudioServiceMessage *message) { 4125ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk aaudio_result_t result = AAUDIO_OK; 413204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk switch (message->event.event) { 4145ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk case AAUDIO_SERVICE_EVENT_STARTED: 415cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk ALOGD("processCommands() got AAUDIO_SERVICE_EVENT_STARTED"); 41687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk if (getState() == AAUDIO_STREAM_STATE_STARTING) { 41787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk setState(AAUDIO_STREAM_STATE_STARTED); 41887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk } 419204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk break; 4205ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk case AAUDIO_SERVICE_EVENT_PAUSED: 421cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk ALOGD("processCommands() got AAUDIO_SERVICE_EVENT_PAUSED"); 42287c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk if (getState() == AAUDIO_STREAM_STATE_PAUSING) { 42387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk setState(AAUDIO_STREAM_STATE_PAUSED); 42487c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk } 425204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk break; 42671f35bb687476694882a617ba4a810a0bb56fe23Phil Burk case AAUDIO_SERVICE_EVENT_STOPPED: 427cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk ALOGD("processCommands() got AAUDIO_SERVICE_EVENT_STOPPED"); 42887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk if (getState() == AAUDIO_STREAM_STATE_STOPPING) { 42987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk setState(AAUDIO_STREAM_STATE_STOPPED); 43087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk } 43171f35bb687476694882a617ba4a810a0bb56fe23Phil Burk break; 4325ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk case AAUDIO_SERVICE_EVENT_FLUSHED: 433cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk ALOGD("processCommands() got AAUDIO_SERVICE_EVENT_FLUSHED"); 43487c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk if (getState() == AAUDIO_STREAM_STATE_FLUSHING) { 43587c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk setState(AAUDIO_STREAM_STATE_FLUSHED); 43687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk onFlushFromServer(); 43787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk } 438204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk break; 4395ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk case AAUDIO_SERVICE_EVENT_CLOSED: 440cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk ALOGD("processCommands() got AAUDIO_SERVICE_EVENT_CLOSED"); 4415ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk setState(AAUDIO_STREAM_STATE_CLOSED); 442204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk break; 4435ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk case AAUDIO_SERVICE_EVENT_DISCONNECTED: 4445ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk result = AAUDIO_ERROR_DISCONNECTED; 445c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk setState(AAUDIO_STREAM_STATE_DISCONNECTED); 4465ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk ALOGW("WARNING - processCommands() AAUDIO_SERVICE_EVENT_DISCONNECTED"); 447204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk break; 448c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk case AAUDIO_SERVICE_EVENT_VOLUME: 449e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk mVolumeRamp.setTarget((float) message->event.dataDouble); 450cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk ALOGD("processCommands() AAUDIO_SERVICE_EVENT_VOLUME %lf", 451e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk message->event.dataDouble); 452c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk break; 453204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk default: 454204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk ALOGW("WARNING - processCommands() Unrecognized event = %d", 455204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk (int) message->event.event); 456204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk break; 457204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } 458204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk return result; 459204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk} 460204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 461204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk// Process all the commands coming from the server. 4625ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkaaudio_result_t AudioStreamInternal::processCommands() { 4635ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk aaudio_result_t result = AAUDIO_OK; 464204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 4655ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk while (result == AAUDIO_OK) { 466cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk //ALOGD("AudioStreamInternal::processCommands() - looping, %d", result); 4675ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk AAudioServiceMessage message; 468204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk if (mAudioEndpoint.readUpCommand(&message) != 1) { 469204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk break; // no command this time, no problem 470204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } 471204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk switch (message.what) { 4725ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk case AAudioServiceMessage::code::TIMESTAMP: 473204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk result = onTimestampFromServer(&message); 474204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk break; 475204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 4765ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk case AAudioServiceMessage::code::EVENT: 477204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk result = onEventFromServer(&message); 478204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk break; 479204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 480204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk default: 48171f35bb687476694882a617ba4a810a0bb56fe23Phil Burk ALOGE("WARNING - AudioStreamInternal::processCommands() Unrecognized what = %d", 482204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk (int) message.what); 4835ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk result = AAUDIO_ERROR_UNEXPECTED_VALUE; 484204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk break; 485204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } 486204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } 487204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk return result; 488204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk} 489204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 49087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk// Read or write the data, block if needed and timeoutMillis > 0 49187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burkaaudio_result_t AudioStreamInternal::processData(void *buffer, int32_t numFrames, 49287c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk int64_t timeoutNanoseconds) 493204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk{ 4944485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk const char * traceName = (mInService) ? "aaWrtS" : "aaWrtC"; 4954485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk ATRACE_BEGIN(traceName); 4965ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk aaudio_result_t result = AAUDIO_OK; 497c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk int32_t loopCount = 0; 49887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk uint8_t* audioData = (uint8_t*)buffer; 4993316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk int64_t currentTimeNanos = AudioClock::getNanoseconds(); 5003316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk int64_t deadlineNanos = currentTimeNanos + timeoutNanoseconds; 501204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk int32_t framesLeft = numFrames; 502204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 5034485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable(); 5044485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk if (ATRACE_ENABLED()) { 5054485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk const char * traceName = (mInService) ? "aaFullS" : "aaFullC"; 5064485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk ATRACE_INT(traceName, fullFrames); 5074485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk } 5084485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk 50987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk // Loop until all the data has been processed or until a timeout occurs. 510204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk while (framesLeft > 0) { 51187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk // The call to processDataNow() will not block. It will just read as much as it can. 5123316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk int64_t wakeTimeNanos = 0; 51387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk aaudio_result_t framesProcessed = processDataNow(audioData, framesLeft, 51487c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk currentTimeNanos, &wakeTimeNanos); 51587c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk if (framesProcessed < 0) { 51687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk ALOGE("AudioStreamInternal::processData() loop: framesProcessed = %d", framesProcessed); 51787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk result = framesProcessed; 518204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk break; 519204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } 52087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk framesLeft -= (int32_t) framesProcessed; 52187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk audioData += framesProcessed * getBytesPerFrame(); 522204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 523204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk // Should we block? 524204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk if (timeoutNanoseconds == 0) { 525204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk break; // don't block 526204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } else if (framesLeft > 0) { 527204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk // clip the wake time to something reasonable 528204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk if (wakeTimeNanos < currentTimeNanos) { 529204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk wakeTimeNanos = currentTimeNanos; 530204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } 531204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk if (wakeTimeNanos > deadlineNanos) { 532204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk // If we time out, just return the framesWritten so far. 533cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk // TODO remove after we fix the deadline bug 53487c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk ALOGE("AudioStreamInternal::processData(): timed out after %lld nanos", 535c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk (long long) timeoutNanoseconds); 53687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk ALOGE("AudioStreamInternal::processData(): wakeTime = %lld, deadline = %lld nanos", 53787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk (long long) wakeTimeNanos, (long long) deadlineNanos); 53887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk ALOGE("AudioStreamInternal::processData(): past deadline by %d micros", 53987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk (int)((wakeTimeNanos - deadlineNanos) / AAUDIO_NANOS_PER_MICROSECOND)); 540204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk break; 541204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } 542204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 54371f35bb687476694882a617ba4a810a0bb56fe23Phil Burk int64_t sleepForNanos = wakeTimeNanos - currentTimeNanos; 54471f35bb687476694882a617ba4a810a0bb56fe23Phil Burk AudioClock::sleepForNanos(sleepForNanos); 545204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk currentTimeNanos = AudioClock::getNanoseconds(); 546204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } 547204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } 548204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 54987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk // return error or framesProcessed 550c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk (void) loopCount; 5514485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk ATRACE_END(); 552204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk return (result < 0) ? result : numFrames - framesLeft; 553204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk} 554204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 5553316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burkvoid AudioStreamInternal::processTimestamp(uint64_t position, int64_t time) { 55687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk mClockModel.processTimestamp(position, time); 557204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk} 558204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 5593316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burkaaudio_result_t AudioStreamInternal::setBufferSize(int32_t requestedFrames) { 5603316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk int32_t actualFrames = 0; 56171f35bb687476694882a617ba4a810a0bb56fe23Phil Burk // Round to the next highest burst size. 56271f35bb687476694882a617ba4a810a0bb56fe23Phil Burk if (getFramesPerBurst() > 0) { 56371f35bb687476694882a617ba4a810a0bb56fe23Phil Burk int32_t numBursts = (requestedFrames + getFramesPerBurst() - 1) / getFramesPerBurst(); 56471f35bb687476694882a617ba4a810a0bb56fe23Phil Burk requestedFrames = numBursts * getFramesPerBurst(); 56571f35bb687476694882a617ba4a810a0bb56fe23Phil Burk } 56671f35bb687476694882a617ba4a810a0bb56fe23Phil Burk 5673316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk aaudio_result_t result = mAudioEndpoint.setBufferSizeInFrames(requestedFrames, &actualFrames); 568cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk ALOGD("AudioStreamInternal::setBufferSize() req = %d => %d", requestedFrames, actualFrames); 5693316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk if (result < 0) { 5703316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk return result; 5713316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk } else { 5723316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk return (aaudio_result_t) actualFrames; 5733316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk } 574204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk} 575204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 57687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burkint32_t AudioStreamInternal::getBufferSize() const { 577204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk return mAudioEndpoint.getBufferSizeInFrames(); 578204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk} 579204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 58087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burkint32_t AudioStreamInternal::getBufferCapacity() const { 581204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk return mAudioEndpoint.getBufferCapacityInFrames(); 582204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk} 583204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 58487c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burkint32_t AudioStreamInternal::getFramesPerBurst() const { 58587c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk return mEndpointDescriptor.dataQueueDescriptor.framesPerBurst; 586204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk} 587204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 58887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burkaaudio_result_t AudioStreamInternal::joinThread(void** returnArg) { 58987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk return AudioStream::joinThread(returnArg, calculateReasonableTimeout(getFramesPerBurst())); 5904c5129b410884ec0400cbe65fce56d0ade12d11bPhil Burk} 591