2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "AAudioServiceStreamMMAP"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
26 #include <utils/String16.h>
27 #include <media/nbaio/AudioStreamOutSink.h>
28 #include <media/MmapStreamInterface.h>
30 #include "binding/AudioEndpointParcelable.h"
31 #include "utility/AAudioUtilities.h"
33 #include "AAudioServiceEndpointMMAP.h"
34 #include "AAudioServiceStreamBase.h"
35 #include "AAudioServiceStreamMMAP.h"
36 #include "SharedMemoryProxy.h"
38 using android::base::unique_fd;
39 using namespace android;
40 using namespace aaudio;
43 * Service Stream that uses an MMAP buffer.
46 AAudioServiceStreamMMAP::AAudioServiceStreamMMAP(android::AAudioService &aAudioService,
48 : AAudioServiceStreamBase(aAudioService)
49 , mInService(inService) {
52 aaudio_result_t AAudioServiceStreamMMAP::close() {
53 if (getState() == AAUDIO_STREAM_STATE_CLOSED) {
59 return AAudioServiceStreamBase::close();
62 // Open stream on HAL and pass information about the shared memory buffer back to the client.
63 aaudio_result_t AAudioServiceStreamMMAP::open(const aaudio::AAudioStreamRequest &request) {
65 sp<AAudioServiceStreamMMAP> keep(this);
67 aaudio_result_t result = AAudioServiceStreamBase::open(request,
68 AAUDIO_SHARING_MODE_EXCLUSIVE);
69 if (result != AAUDIO_OK) {
73 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
74 if (endpoint == nullptr) {
75 ALOGE("%s() has no endpoint", __func__);
76 return AAUDIO_ERROR_INVALID_STATE;
79 result = endpoint->registerStream(keep);
80 if (result != AAUDIO_OK) {
84 setState(AAUDIO_STREAM_STATE_OPEN);
90 * Start the flow of data.
92 aaudio_result_t AAudioServiceStreamMMAP::startDevice() {
93 aaudio_result_t result = AAudioServiceStreamBase::startDevice();
94 if (!mInService && result == AAUDIO_OK) {
95 // Note that this can sometimes take 200 to 300 msec for a cold start!
96 result = startClient(mMmapClient, &mClientHandle);
102 * Stop the flow of data such that start() can resume with loss of data.
104 aaudio_result_t AAudioServiceStreamMMAP::pause() {
108 aaudio_result_t result = AAudioServiceStreamBase::pause();
109 // TODO put before base::pause()?
111 (void) stopClient(mClientHandle);
116 aaudio_result_t AAudioServiceStreamMMAP::stop() {
120 aaudio_result_t result = AAudioServiceStreamBase::stop();
121 // TODO put before base::stop()?
123 (void) stopClient(mClientHandle);
128 aaudio_result_t AAudioServiceStreamMMAP::startClient(const android::AudioClient& client,
129 audio_port_handle_t *clientHandle) {
130 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
131 if (endpoint == nullptr) {
132 ALOGE("%s() has no endpoint", __func__);
133 return AAUDIO_ERROR_INVALID_STATE;
135 // Start the client on behalf of the application. Generate a new porthandle.
136 aaudio_result_t result = endpoint->startClient(client, clientHandle);
140 aaudio_result_t AAudioServiceStreamMMAP::stopClient(audio_port_handle_t clientHandle) {
141 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
142 if (endpoint == nullptr) {
143 ALOGE("%s() has no endpoint", __func__);
144 return AAUDIO_ERROR_INVALID_STATE;
146 aaudio_result_t result = endpoint->stopClient(clientHandle);
150 // Get free-running DSP or DMA hardware position from the HAL.
151 aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames,
152 int64_t *timeNanos) {
153 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
154 if (endpoint == nullptr) {
155 ALOGE("%s() has no endpoint", __func__);
156 return AAUDIO_ERROR_INVALID_STATE;
158 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
159 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
161 aaudio_result_t result = serviceEndpointMMAP->getFreeRunningPosition(positionFrames, timeNanos);
162 if (result == AAUDIO_OK) {
163 Timestamp timestamp(*positionFrames, *timeNanos);
164 mAtomicTimestamp.write(timestamp);
165 *positionFrames = timestamp.getPosition();
166 *timeNanos = timestamp.getNanoseconds();
167 } else if (result != AAUDIO_ERROR_UNAVAILABLE) {
173 // Get timestamp that was written by getFreeRunningPosition()
174 aaudio_result_t AAudioServiceStreamMMAP::getHardwareTimestamp(int64_t *positionFrames,
175 int64_t *timeNanos) {
177 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
178 if (endpoint == nullptr) {
179 ALOGE("%s() has no endpoint", __func__);
180 return AAUDIO_ERROR_INVALID_STATE;
182 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
183 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
185 // TODO Get presentation timestamp from the HAL
186 if (mAtomicTimestamp.isValid()) {
187 Timestamp timestamp = mAtomicTimestamp.read();
188 *positionFrames = timestamp.getPosition();
189 *timeNanos = timestamp.getNanoseconds() + serviceEndpointMMAP->getHardwareTimeOffsetNanos();
192 return AAUDIO_ERROR_UNAVAILABLE;
197 * Get an immutable description of the data queue from the HAL.
199 aaudio_result_t AAudioServiceStreamMMAP::getAudioDataDescription(
200 AudioEndpointParcelable &parcelable)
202 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
203 if (endpoint == nullptr) {
204 ALOGE("%s() has no endpoint", __func__);
205 return AAUDIO_ERROR_INVALID_STATE;
207 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
208 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
209 return serviceEndpointMMAP->getDownDataDescription(parcelable);