1/* 2 * Copyright (C) 2017 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 "AAudioServiceStreamMMAP" 18//#define LOG_NDEBUG 0 19#include <utils/Log.h> 20 21#include <atomic> 22#include <iomanip> 23#include <iostream> 24#include <stdint.h> 25 26#include <utils/String16.h> 27#include <media/nbaio/AudioStreamOutSink.h> 28#include <media/MmapStreamInterface.h> 29 30#include "binding/AudioEndpointParcelable.h" 31#include "utility/AAudioUtilities.h" 32 33#include "AAudioServiceEndpointMMAP.h" 34#include "AAudioServiceStreamBase.h" 35#include "AAudioServiceStreamMMAP.h" 36#include "SharedMemoryProxy.h" 37 38using android::base::unique_fd; 39using namespace android; 40using namespace aaudio; 41 42/** 43 * Service Stream that uses an MMAP buffer. 44 */ 45 46AAudioServiceStreamMMAP::AAudioServiceStreamMMAP(android::AAudioService &aAudioService, 47 bool inService) 48 : AAudioServiceStreamBase(aAudioService) 49 , mInService(inService) { 50} 51 52aaudio_result_t AAudioServiceStreamMMAP::close() { 53 if (getState() == AAUDIO_STREAM_STATE_CLOSED) { 54 return AAUDIO_OK; 55 } 56 57 stop(); 58 59 return AAudioServiceStreamBase::close(); 60} 61 62// Open stream on HAL and pass information about the shared memory buffer back to the client. 63aaudio_result_t AAudioServiceStreamMMAP::open(const aaudio::AAudioStreamRequest &request) { 64 65 sp<AAudioServiceStreamMMAP> keep(this); 66 67 aaudio_result_t result = AAudioServiceStreamBase::open(request, 68 AAUDIO_SHARING_MODE_EXCLUSIVE); 69 if (result != AAUDIO_OK) { 70 return result; 71 } 72 73 result = mServiceEndpoint->registerStream(keep); 74 if (result != AAUDIO_OK) { 75 goto error; 76 } 77 78 setState(AAUDIO_STREAM_STATE_OPEN); 79 80error: 81 return AAUDIO_OK; 82} 83 84/** 85 * Start the flow of data. 86 */ 87aaudio_result_t AAudioServiceStreamMMAP::startDevice() { 88 aaudio_result_t result = AAudioServiceStreamBase::startDevice(); 89 if (!mInService && result == AAUDIO_OK) { 90 // Note that this can sometimes take 200 to 300 msec for a cold start! 91 result = startClient(mMmapClient, &mClientHandle); 92 } 93 return result; 94} 95 96/** 97 * Stop the flow of data such that start() can resume with loss of data. 98 */ 99aaudio_result_t AAudioServiceStreamMMAP::pause() { 100 if (!isRunning()) { 101 return AAUDIO_OK; 102 } 103 aaudio_result_t result = AAudioServiceStreamBase::pause(); 104 // TODO put before base::pause()? 105 if (!mInService) { 106 (void) stopClient(mClientHandle); 107 } 108 return result; 109} 110 111aaudio_result_t AAudioServiceStreamMMAP::stop() { 112 if (!isRunning()) { 113 return AAUDIO_OK; 114 } 115 aaudio_result_t result = AAudioServiceStreamBase::stop(); 116 // TODO put before base::stop()? 117 if (!mInService) { 118 (void) stopClient(mClientHandle); 119 } 120 return result; 121} 122 123aaudio_result_t AAudioServiceStreamMMAP::startClient(const android::AudioClient& client, 124 audio_port_handle_t *clientHandle) { 125 // Start the client on behalf of the application. Generate a new porthandle. 126 aaudio_result_t result = mServiceEndpoint->startClient(client, clientHandle); 127 return result; 128} 129 130aaudio_result_t AAudioServiceStreamMMAP::stopClient(audio_port_handle_t clientHandle) { 131 aaudio_result_t result = mServiceEndpoint->stopClient(clientHandle); 132 return result; 133} 134 135// Get free-running DSP or DMA hardware position from the HAL. 136aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames, 137 int64_t *timeNanos) { 138 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP{ 139 static_cast<AAudioServiceEndpointMMAP *>(mServiceEndpoint.get())}; 140 aaudio_result_t result = serviceEndpointMMAP->getFreeRunningPosition(positionFrames, timeNanos); 141 if (result == AAUDIO_OK) { 142 Timestamp timestamp(*positionFrames, *timeNanos); 143 mAtomicTimestamp.write(timestamp); 144 *positionFrames = timestamp.getPosition(); 145 *timeNanos = timestamp.getNanoseconds(); 146 } else if (result != AAUDIO_ERROR_UNAVAILABLE) { 147 disconnect(); 148 } 149 return result; 150} 151 152// Get timestamp that was written by getFreeRunningPosition() 153aaudio_result_t AAudioServiceStreamMMAP::getHardwareTimestamp(int64_t *positionFrames, 154 int64_t *timeNanos) { 155 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP{ 156 static_cast<AAudioServiceEndpointMMAP *>(mServiceEndpoint.get())}; 157 // TODO Get presentation timestamp from the HAL 158 if (mAtomicTimestamp.isValid()) { 159 Timestamp timestamp = mAtomicTimestamp.read(); 160 *positionFrames = timestamp.getPosition(); 161 *timeNanos = timestamp.getNanoseconds() + serviceEndpointMMAP->getHardwareTimeOffsetNanos(); 162 return AAUDIO_OK; 163 } else { 164 return AAUDIO_ERROR_UNAVAILABLE; 165 } 166} 167 168/** 169 * Get an immutable description of the data queue from the HAL. 170 */ 171aaudio_result_t AAudioServiceStreamMMAP::getAudioDataDescription( 172 AudioEndpointParcelable &parcelable) 173{ 174 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP{ 175 static_cast<AAudioServiceEndpointMMAP *>(mServiceEndpoint.get())}; 176 return serviceEndpointMMAP->getDownDataDescription(parcelable); 177} 178