AudioStreamInternal.h revision 9dca9824da74d50be02bc81f539cc77b7bde678a
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 175204d315c6c6f53188f8d1414dd1b55b6c90142bPhil Burk#ifndef ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H 185204d315c6c6f53188f8d1414dd1b55b6c90142bPhil Burk#define ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H 19204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 20204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#include <stdint.h> 215ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include <aaudio/AAudio.h> 22204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 235ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include "binding/IAAudioService.h" 24204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#include "binding/AudioEndpointParcelable.h" 25e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk#include "binding/AAudioServiceInterface.h" 26204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#include "client/IsochronousClockModel.h" 27204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#include "client/AudioEndpoint.h" 28204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#include "core/AudioStream.h" 29e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk#include "utility/LinearRamp.h" 30c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk 31204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkusing android::sp; 325ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkusing android::IAAudioService; 33204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 345ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burknamespace aaudio { 35204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 365ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk// A stream that talks to the AAudioService or directly to a HAL. 37204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkclass AudioStreamInternal : public AudioStream { 38204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 39204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkpublic: 4087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk AudioStreamInternal(AAudioServiceInterface &serviceInterface, bool inService); 41204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk virtual ~AudioStreamInternal(); 42204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 43204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk // =========== Begin ABSTRACT methods =========================== 44c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk aaudio_result_t requestStart() override; 45204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 46c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk aaudio_result_t requestPause() override; 47204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 48c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk aaudio_result_t requestFlush() override; 49204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 50c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk aaudio_result_t requestStop() override; 51204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 52c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk aaudio_result_t getTimestamp(clockid_t clockId, 533316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk int64_t *framePosition, 543316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk int64_t *timeNanoseconds) override; 55204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 56e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk virtual aaudio_result_t updateStateWhileWaiting() override; 57204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk // =========== End ABSTRACT methods =========================== 58204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 59c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk aaudio_result_t open(const AudioStreamBuilder &builder) override; 60204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 61c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk aaudio_result_t close() override; 62204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 63c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk aaudio_result_t setBufferSize(int32_t requestedFrames) override; 64204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 65c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk int32_t getBufferSize() const override; 66204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 67c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk int32_t getBufferCapacity() const override; 68204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 69c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk int32_t getFramesPerBurst() const override; 70204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 71c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk int32_t getXRunCount() const override { 72204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk return mXRunCount; 73204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk } 74204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 75c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk aaudio_result_t registerThread() override; 76204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 77c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk aaudio_result_t unregisterThread() override; 78204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 7987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk aaudio_result_t joinThread(void** returnArg); 8087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk 81e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk // Called internally from 'C' 8287c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk virtual void *callbackLoop() = 0; 83e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk 84e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk 85e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk bool isMMap() override { 86e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk return true; 87e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk } 88e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk 8987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk // Calculate timeout based on framesPerBurst 9087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk int64_t calculateReasonableTimeout(); 9187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk 92204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkprotected: 93204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 9487c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk aaudio_result_t processData(void *buffer, 9587c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk int32_t numFrames, 9687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk int64_t timeoutNanoseconds); 9787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk 9887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk/** 9987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk * Low level data processing that will not block. It will just read or write as much as it can. 10087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk * 10187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk * It passed back a recommended time to wake up if wakeTimePtr is not NULL. 10287c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk * 10387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk * @return the number of frames processed or a negative error code. 10487c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk */ 10587c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk virtual aaudio_result_t processDataNow(void *buffer, 10687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk int32_t numFrames, 10787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk int64_t currentTimeNanos, 10887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk int64_t *wakeTimePtr) = 0; 10987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk 1105ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk aaudio_result_t processCommands(); 111204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 112e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk aaudio_result_t requestPauseInternal(); 11371f35bb687476694882a617ba4a810a0bb56fe23Phil Burk aaudio_result_t requestStopInternal(); 114e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk 115e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk aaudio_result_t stopCallback(); 116e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk 117204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 118204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk void onFlushFromServer(); 119204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 1205ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk aaudio_result_t onEventFromServer(AAudioServiceMessage *message); 121204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 1225ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk aaudio_result_t onTimestampFromServer(AAudioServiceMessage *message); 123204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 124e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk // Calculate timeout for an operation involving framesPerOperation. 125e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk int64_t calculateReasonableTimeout(int32_t framesPerOperation); 126e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk 1279dca9824da74d50be02bc81f539cc77b7bde678aPhil Burk aaudio_format_t mDeviceFormat = AAUDIO_FORMAT_UNSPECIFIED; 12887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk 12987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk IsochronousClockModel mClockModel; // timing model for chasing the HAL 13087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk 13187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk AudioEndpoint mAudioEndpoint; // source for reads or sink for writes 13287c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk aaudio_handle_t mServiceStreamHandle; // opaque handle returned from service 13387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk 13487c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk int32_t mFramesPerBurst; // frames per HAL transfer 13587c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk int32_t mXRunCount = 0; // how many underrun events? 13687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk 13787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk LinearRamp mVolumeRamp; 13887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk 13987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk // Offset from underlying frame position. 14087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk int64_t mFramesOffsetFromService = 0; // offset for timestamps 14187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk 14287c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk uint8_t *mCallbackBuffer = nullptr; 14387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk int32_t mCallbackFrames = 0; 14487c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk 145204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkprivate: 146c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk /* 147c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk * Asynchronous write with data conversion. 148c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk * @param buffer 149c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk * @param numFrames 150c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk * @return fdrames written or negative error 151c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk */ 152c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk aaudio_result_t writeNowWithConversion(const void *buffer, 153c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk int32_t numFrames); 154c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk 15587c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk // Adjust timing model based on timestamp from service. 15687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk void processTimestamp(uint64_t position, int64_t time); 15771f35bb687476694882a617ba4a810a0bb56fe23Phil Burk 158c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk AudioEndpointParcelable mEndPointParcelable; // description of the buffers filled by service 159c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk EndpointDescriptor mEndpointDescriptor; // buffer description with resolved addresses 160c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk AAudioServiceInterface &mServiceInterface; // abstract interface to the service 161c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk 162c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk // The service uses this for SHARED mode. 16387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk bool mInService = false; // Is this running in the client or the service? 164204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk}; 165204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 1665ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk} /* namespace aaudio */ 167204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk 1685204d315c6c6f53188f8d1414dd1b55b6c90142bPhil Burk#endif //ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H 169