blob: ec9b2e21f5ebb0d5d2a69864d2aaa33480c558df [file] [log] [blame]
Phil Burkc0c70e32017-02-09 13:18:38 -08001/*
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
Eric Laurentcb4dae22017-07-01 19:39:32 -070017#define LOG_TAG "AAudioServiceStreamMMAP"
Phil Burkc0c70e32017-02-09 13:18:38 -080018//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
21#include <atomic>
Phil Burkf95ed732020-09-30 18:48:17 +000022#include <inttypes.h>
Phil Burk39f02dd2017-08-04 09:13:31 -070023#include <iomanip>
24#include <iostream>
Phil Burkc0c70e32017-02-09 13:18:38 -080025#include <stdint.h>
26
27#include <utils/String16.h>
28#include <media/nbaio/AudioStreamOutSink.h>
29#include <media/MmapStreamInterface.h>
30
Phil Burk39f02dd2017-08-04 09:13:31 -070031#include "binding/AudioEndpointParcelable.h"
32#include "utility/AAudioUtilities.h"
33
34#include "AAudioServiceEndpointMMAP.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080035#include "AAudioServiceStreamBase.h"
36#include "AAudioServiceStreamMMAP.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080037#include "SharedMemoryProxy.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080038
Phil Burke72481c2017-08-08 13:20:45 -070039using android::base::unique_fd;
Phil Burkc0c70e32017-02-09 13:18:38 -080040using namespace android;
41using namespace aaudio;
42
Phil Burkc0c70e32017-02-09 13:18:38 -080043/**
Phil Burk11e8d332017-05-24 09:59:02 -070044 * Service Stream that uses an MMAP buffer.
Phil Burkc0c70e32017-02-09 13:18:38 -080045 */
46
Phil Burk39f02dd2017-08-04 09:13:31 -070047AAudioServiceStreamMMAP::AAudioServiceStreamMMAP(android::AAudioService &aAudioService,
Eric Laurentcb4dae22017-07-01 19:39:32 -070048 bool inService)
Phil Burk39f02dd2017-08-04 09:13:31 -070049 : AAudioServiceStreamBase(aAudioService)
Eric Laurentcb4dae22017-07-01 19:39:32 -070050 , mInService(inService) {
Phil Burkc0c70e32017-02-09 13:18:38 -080051}
52
Phil Burkc0c70e32017-02-09 13:18:38 -080053// Open stream on HAL and pass information about the shared memory buffer back to the client.
Phil Burk39f02dd2017-08-04 09:13:31 -070054aaudio_result_t AAudioServiceStreamMMAP::open(const aaudio::AAudioStreamRequest &request) {
Phil Burkc0c70e32017-02-09 13:18:38 -080055
Phil Burk39f02dd2017-08-04 09:13:31 -070056 sp<AAudioServiceStreamMMAP> keep(this);
57
Phil Burk15f97c92018-09-04 14:06:27 -070058 if (request.getConstantConfiguration().getSharingMode() != AAUDIO_SHARING_MODE_EXCLUSIVE) {
59 ALOGE("%s() sharingMode mismatch %d", __func__,
60 request.getConstantConfiguration().getSharingMode());
61 return AAUDIO_ERROR_INTERNAL;
62 }
63
64 aaudio_result_t result = AAudioServiceStreamBase::open(request);
Phil Burkc0c70e32017-02-09 13:18:38 -080065 if (result != AAUDIO_OK) {
Phil Burkc0c70e32017-02-09 13:18:38 -080066 return result;
67 }
68
Phil Burk6e2770e2018-05-01 13:03:52 -070069 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
70 if (endpoint == nullptr) {
71 ALOGE("%s() has no endpoint", __func__);
72 return AAUDIO_ERROR_INVALID_STATE;
73 }
74
75 result = endpoint->registerStream(keep);
Phil Burk39f02dd2017-08-04 09:13:31 -070076 if (result != AAUDIO_OK) {
Phil Burk6e2770e2018-05-01 13:03:52 -070077 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -080078 }
Phil Burkc0c70e32017-02-09 13:18:38 -080079
Phil Burk5a26e662017-07-07 12:44:48 -070080 setState(AAUDIO_STREAM_STATE_OPEN);
Phil Burk39f02dd2017-08-04 09:13:31 -070081
Phil Burkc0c70e32017-02-09 13:18:38 -080082 return AAUDIO_OK;
83}
84
Phil Burkfbf031e2017-10-12 15:58:31 -070085// Start the flow of data.
Phil Burkbcc36742017-08-31 17:24:51 -070086aaudio_result_t AAudioServiceStreamMMAP::startDevice() {
87 aaudio_result_t result = AAudioServiceStreamBase::startDevice();
Phil Burk39f02dd2017-08-04 09:13:31 -070088 if (!mInService && result == AAUDIO_OK) {
Phil Burkbcc36742017-08-31 17:24:51 -070089 // Note that this can sometimes take 200 to 300 msec for a cold start!
jiabind1f1cb62020-03-24 11:57:57 -070090 result = startClient(mMmapClient, nullptr /*const audio_attributes_t* */, &mClientHandle);
Phil Burkc0c70e32017-02-09 13:18:38 -080091 }
92 return result;
93}
94
Phil Burkfbf031e2017-10-12 15:58:31 -070095// Stop the flow of data such that start() can resume with loss of data.
Phil Burk7ebbc112020-05-13 15:55:17 -070096aaudio_result_t AAudioServiceStreamMMAP::pause_l() {
Eric Laurentcb4dae22017-07-01 19:39:32 -070097 if (!isRunning()) {
98 return AAUDIO_OK;
99 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700100 aaudio_result_t result = AAudioServiceStreamBase::pause_l();
Phil Burk39f02dd2017-08-04 09:13:31 -0700101 // TODO put before base::pause()?
Eric Laurentcb4dae22017-07-01 19:39:32 -0700102 if (!mInService) {
Phil Burk81ad5ec2017-09-01 10:45:41 -0700103 (void) stopClient(mClientHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700104 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700105 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800106}
107
Phil Burk7ebbc112020-05-13 15:55:17 -0700108aaudio_result_t AAudioServiceStreamMMAP::stop_l() {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700109 if (!isRunning()) {
110 return AAUDIO_OK;
111 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700112 aaudio_result_t result = AAudioServiceStreamBase::stop_l();
Phil Burk39f02dd2017-08-04 09:13:31 -0700113 // TODO put before base::stop()?
Eric Laurentcb4dae22017-07-01 19:39:32 -0700114 if (!mInService) {
Phil Burk81ad5ec2017-09-01 10:45:41 -0700115 (void) stopClient(mClientHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700116 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700117 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800118}
119
jiabinf7f06152021-11-22 18:10:14 +0000120aaudio_result_t AAudioServiceStreamMMAP::standby_l() {
121 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
122 if (endpoint == nullptr) {
123 ALOGE("%s() has no endpoint", __func__);
124 return AAUDIO_ERROR_INVALID_STATE;
125 }
126 aaudio_result_t result = endpoint->standby();
127 if (result == AAUDIO_OK) {
128 setStandby_l(true);
129 }
130 return result;
131}
132
133aaudio_result_t AAudioServiceStreamMMAP::exitStandby_l(AudioEndpointParcelable* parcelable) {
134 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
135 if (endpoint == nullptr) {
136 ALOGE("%s() has no endpoint", __func__);
137 return AAUDIO_ERROR_INVALID_STATE;
138 }
139 aaudio_result_t result = endpoint->exitStandby(parcelable);
140 if (result == AAUDIO_OK) {
141 setStandby_l(false);
142 } else {
143 ALOGE("%s failed, result %d, disconnecting stream.", __func__, result);
144 disconnect_l();
145 }
146 return result;
147}
148
Eric Laurentcb4dae22017-07-01 19:39:32 -0700149aaudio_result_t AAudioServiceStreamMMAP::startClient(const android::AudioClient& client,
jiabind1f1cb62020-03-24 11:57:57 -0700150 const audio_attributes_t *attr,
151 audio_port_handle_t *clientHandle) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700152 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
153 if (endpoint == nullptr) {
154 ALOGE("%s() has no endpoint", __func__);
155 return AAUDIO_ERROR_INVALID_STATE;
156 }
Phil Burkbcc36742017-08-31 17:24:51 -0700157 // Start the client on behalf of the application. Generate a new porthandle.
jiabind1f1cb62020-03-24 11:57:57 -0700158 aaudio_result_t result = endpoint->startClient(client, attr, clientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700159 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700160}
161
162aaudio_result_t AAudioServiceStreamMMAP::stopClient(audio_port_handle_t clientHandle) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700163 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
164 if (endpoint == nullptr) {
165 ALOGE("%s() has no endpoint", __func__);
166 return AAUDIO_ERROR_INVALID_STATE;
167 }
168 aaudio_result_t result = endpoint->stopClient(clientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700169 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700170}
171
Phil Burk97350f92017-07-21 15:59:44 -0700172// Get free-running DSP or DMA hardware position from the HAL.
jiabin2a594622021-10-14 00:32:25 +0000173aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition_l(int64_t *positionFrames,
Phil Burk39f02dd2017-08-04 09:13:31 -0700174 int64_t *timeNanos) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700175 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
176 if (endpoint == nullptr) {
177 ALOGE("%s() has no endpoint", __func__);
178 return AAUDIO_ERROR_INVALID_STATE;
179 }
180 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
181 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
182
Phil Burk39f02dd2017-08-04 09:13:31 -0700183 aaudio_result_t result = serviceEndpointMMAP->getFreeRunningPosition(positionFrames, timeNanos);
184 if (result == AAUDIO_OK) {
185 Timestamp timestamp(*positionFrames, *timeNanos);
Phil Burka53ffa62018-10-10 16:21:37 -0700186 mAtomicStreamTimestamp.write(timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700187 *positionFrames = timestamp.getPosition();
188 *timeNanos = timestamp.getNanoseconds();
Phil Burk39f02dd2017-08-04 09:13:31 -0700189 } else if (result != AAUDIO_ERROR_UNAVAILABLE) {
jiabin2a594622021-10-14 00:32:25 +0000190 disconnect_l();
Phil Burkc0c70e32017-02-09 13:18:38 -0800191 }
Phil Burk940083c2017-07-17 17:00:02 -0700192 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800193}
194
jiabinb7d8c5a2020-08-26 17:24:52 -0700195// Get timestamp from presentation position.
Phil Burkf95ed732020-09-30 18:48:17 +0000196// If it fails, get timestamp that was written by getFreeRunningPosition()
jiabin2a594622021-10-14 00:32:25 +0000197aaudio_result_t AAudioServiceStreamMMAP::getHardwareTimestamp_l(int64_t *positionFrames,
Phil Burk97350f92017-07-21 15:59:44 -0700198 int64_t *timeNanos) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700199 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
200 if (endpoint == nullptr) {
201 ALOGE("%s() has no endpoint", __func__);
202 return AAUDIO_ERROR_INVALID_STATE;
203 }
204 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
205 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
206
jiabinb7d8c5a2020-08-26 17:24:52 -0700207 uint64_t position;
jiabina5df87b2020-12-29 10:45:19 -0800208 aaudio_result_t result = serviceEndpointMMAP->getExternalPosition(&position, timeNanos);
209 if (result == AAUDIO_OK) {
210 ALOGV("%s() getExternalPosition() says pos = %" PRIi64 ", time = %" PRIi64,
Phil Burkf95ed732020-09-30 18:48:17 +0000211 __func__, position, *timeNanos);
jiabinb7d8c5a2020-08-26 17:24:52 -0700212 *positionFrames = (int64_t) position;
213 return AAUDIO_OK;
jiabina5df87b2020-12-29 10:45:19 -0800214 } else {
215 ALOGV("%s() getExternalPosition() returns error %d", __func__, result);
216 }
217
Phil Burkf95ed732020-09-30 18:48:17 +0000218 if (mAtomicStreamTimestamp.isValid()) {
Phil Burka53ffa62018-10-10 16:21:37 -0700219 Timestamp timestamp = mAtomicStreamTimestamp.read();
Phil Burk97350f92017-07-21 15:59:44 -0700220 *positionFrames = timestamp.getPosition();
Phil Burk39f02dd2017-08-04 09:13:31 -0700221 *timeNanos = timestamp.getNanoseconds() + serviceEndpointMMAP->getHardwareTimeOffsetNanos();
Phil Burk97350f92017-07-21 15:59:44 -0700222 return AAUDIO_OK;
223 } else {
224 return AAUDIO_ERROR_UNAVAILABLE;
225 }
226}
227
Phil Burkfbf031e2017-10-12 15:58:31 -0700228// Get an immutable description of the data queue from the HAL.
jiabin2a594622021-10-14 00:32:25 +0000229aaudio_result_t AAudioServiceStreamMMAP::getAudioDataDescription_l(
230 AudioEndpointParcelable* parcelable)
Phil Burkc0c70e32017-02-09 13:18:38 -0800231{
Phil Burk6e2770e2018-05-01 13:03:52 -0700232 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
233 if (endpoint == nullptr) {
234 ALOGE("%s() has no endpoint", __func__);
235 return AAUDIO_ERROR_INVALID_STATE;
236 }
237 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
238 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
Phil Burk39f02dd2017-08-04 09:13:31 -0700239 return serviceEndpointMMAP->getDownDataDescription(parcelable);
Phil Burkec89b2e2017-06-20 15:05:06 -0700240}