blob: 57dc1ab26ef0e32dea161dd7644a432cc9328bca [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
Eric Laurentcb4dae22017-07-01 19:39:32 -0700120aaudio_result_t AAudioServiceStreamMMAP::startClient(const android::AudioClient& client,
jiabind1f1cb62020-03-24 11:57:57 -0700121 const audio_attributes_t *attr,
122 audio_port_handle_t *clientHandle) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700123 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
124 if (endpoint == nullptr) {
125 ALOGE("%s() has no endpoint", __func__);
126 return AAUDIO_ERROR_INVALID_STATE;
127 }
Phil Burkbcc36742017-08-31 17:24:51 -0700128 // Start the client on behalf of the application. Generate a new porthandle.
jiabind1f1cb62020-03-24 11:57:57 -0700129 aaudio_result_t result = endpoint->startClient(client, attr, clientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700130 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700131}
132
133aaudio_result_t AAudioServiceStreamMMAP::stopClient(audio_port_handle_t clientHandle) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700134 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->stopClient(clientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700140 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700141}
142
Phil Burk97350f92017-07-21 15:59:44 -0700143// Get free-running DSP or DMA hardware position from the HAL.
Phil Burkc0c70e32017-02-09 13:18:38 -0800144aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames,
Phil Burk39f02dd2017-08-04 09:13:31 -0700145 int64_t *timeNanos) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700146 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
147 if (endpoint == nullptr) {
148 ALOGE("%s() has no endpoint", __func__);
149 return AAUDIO_ERROR_INVALID_STATE;
150 }
151 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
152 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
153
Phil Burk39f02dd2017-08-04 09:13:31 -0700154 aaudio_result_t result = serviceEndpointMMAP->getFreeRunningPosition(positionFrames, timeNanos);
155 if (result == AAUDIO_OK) {
156 Timestamp timestamp(*positionFrames, *timeNanos);
Phil Burka53ffa62018-10-10 16:21:37 -0700157 mAtomicStreamTimestamp.write(timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700158 *positionFrames = timestamp.getPosition();
159 *timeNanos = timestamp.getNanoseconds();
Phil Burk39f02dd2017-08-04 09:13:31 -0700160 } else if (result != AAUDIO_ERROR_UNAVAILABLE) {
161 disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800162 }
Phil Burk940083c2017-07-17 17:00:02 -0700163 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800164}
165
jiabinb7d8c5a2020-08-26 17:24:52 -0700166// Get timestamp from presentation position.
Phil Burkf95ed732020-09-30 18:48:17 +0000167// If it fails, get timestamp that was written by getFreeRunningPosition()
Phil Burk97350f92017-07-21 15:59:44 -0700168aaudio_result_t AAudioServiceStreamMMAP::getHardwareTimestamp(int64_t *positionFrames,
169 int64_t *timeNanos) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700170
171 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
172 if (endpoint == nullptr) {
173 ALOGE("%s() has no endpoint", __func__);
174 return AAUDIO_ERROR_INVALID_STATE;
175 }
176 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
177 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
178
Phil Burkf95ed732020-09-30 18:48:17 +0000179 // Disable this code temporarily because the HAL is not returning
180 // a useful result.
181#if 0
jiabinb7d8c5a2020-08-26 17:24:52 -0700182 uint64_t position;
183 if (serviceEndpointMMAP->getExternalPosition(&position, timeNanos) == AAUDIO_OK) {
Phil Burkf95ed732020-09-30 18:48:17 +0000184 ALOGD("%s() getExternalPosition() says pos = %" PRIi64 ", time = %" PRIi64,
185 __func__, position, *timeNanos);
jiabinb7d8c5a2020-08-26 17:24:52 -0700186 *positionFrames = (int64_t) position;
187 return AAUDIO_OK;
Phil Burkf95ed732020-09-30 18:48:17 +0000188 } else
189#endif
190 if (mAtomicStreamTimestamp.isValid()) {
Phil Burka53ffa62018-10-10 16:21:37 -0700191 Timestamp timestamp = mAtomicStreamTimestamp.read();
Phil Burk97350f92017-07-21 15:59:44 -0700192 *positionFrames = timestamp.getPosition();
Phil Burk39f02dd2017-08-04 09:13:31 -0700193 *timeNanos = timestamp.getNanoseconds() + serviceEndpointMMAP->getHardwareTimeOffsetNanos();
Phil Burk97350f92017-07-21 15:59:44 -0700194 return AAUDIO_OK;
195 } else {
196 return AAUDIO_ERROR_UNAVAILABLE;
197 }
198}
199
Phil Burkfbf031e2017-10-12 15:58:31 -0700200// Get an immutable description of the data queue from the HAL.
Phil Burk523b3042017-09-13 13:03:08 -0700201aaudio_result_t AAudioServiceStreamMMAP::getAudioDataDescription(
202 AudioEndpointParcelable &parcelable)
Phil Burkc0c70e32017-02-09 13:18:38 -0800203{
Phil Burk6e2770e2018-05-01 13:03:52 -0700204 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
205 if (endpoint == nullptr) {
206 ALOGE("%s() has no endpoint", __func__);
207 return AAUDIO_ERROR_INVALID_STATE;
208 }
209 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
210 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
Phil Burk39f02dd2017-08-04 09:13:31 -0700211 return serviceEndpointMMAP->getDownDataDescription(parcelable);
Phil Burkec89b2e2017-06-20 15:05:06 -0700212}