blob: 639a0a89f41de711df233537c1b53df1ae1c2f05 [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 Burk39f02dd2017-08-04 09:13:31 -070022#include <iomanip>
23#include <iostream>
Phil Burkc0c70e32017-02-09 13:18:38 -080024#include <stdint.h>
25
26#include <utils/String16.h>
27#include <media/nbaio/AudioStreamOutSink.h>
28#include <media/MmapStreamInterface.h>
29
Phil Burk39f02dd2017-08-04 09:13:31 -070030#include "binding/AudioEndpointParcelable.h"
31#include "utility/AAudioUtilities.h"
32
33#include "AAudioServiceEndpointMMAP.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080034#include "AAudioServiceStreamBase.h"
35#include "AAudioServiceStreamMMAP.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080036#include "SharedMemoryProxy.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080037
Phil Burke72481c2017-08-08 13:20:45 -070038using android::base::unique_fd;
Phil Burkc0c70e32017-02-09 13:18:38 -080039using namespace android;
40using namespace aaudio;
41
Phil Burkc0c70e32017-02-09 13:18:38 -080042/**
Phil Burk11e8d332017-05-24 09:59:02 -070043 * Service Stream that uses an MMAP buffer.
Phil Burkc0c70e32017-02-09 13:18:38 -080044 */
45
Phil Burk39f02dd2017-08-04 09:13:31 -070046AAudioServiceStreamMMAP::AAudioServiceStreamMMAP(android::AAudioService &aAudioService,
Eric Laurentcb4dae22017-07-01 19:39:32 -070047 bool inService)
Phil Burk39f02dd2017-08-04 09:13:31 -070048 : AAudioServiceStreamBase(aAudioService)
Eric Laurentcb4dae22017-07-01 19:39:32 -070049 , mInService(inService) {
Phil Burkc0c70e32017-02-09 13:18:38 -080050}
51
Phil Burkc0c70e32017-02-09 13:18:38 -080052// Open stream on HAL and pass information about the shared memory buffer back to the client.
Phil Burk39f02dd2017-08-04 09:13:31 -070053aaudio_result_t AAudioServiceStreamMMAP::open(const aaudio::AAudioStreamRequest &request) {
Phil Burkc0c70e32017-02-09 13:18:38 -080054
Phil Burk39f02dd2017-08-04 09:13:31 -070055 sp<AAudioServiceStreamMMAP> keep(this);
56
Phil Burk15f97c92018-09-04 14:06:27 -070057 if (request.getConstantConfiguration().getSharingMode() != AAUDIO_SHARING_MODE_EXCLUSIVE) {
58 ALOGE("%s() sharingMode mismatch %d", __func__,
59 request.getConstantConfiguration().getSharingMode());
60 return AAUDIO_ERROR_INTERNAL;
61 }
62
63 aaudio_result_t result = AAudioServiceStreamBase::open(request);
Phil Burkc0c70e32017-02-09 13:18:38 -080064 if (result != AAUDIO_OK) {
Phil Burkc0c70e32017-02-09 13:18:38 -080065 return result;
66 }
67
Phil Burk6e2770e2018-05-01 13:03:52 -070068 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
69 if (endpoint == nullptr) {
70 ALOGE("%s() has no endpoint", __func__);
71 return AAUDIO_ERROR_INVALID_STATE;
72 }
73
74 result = endpoint->registerStream(keep);
Phil Burk39f02dd2017-08-04 09:13:31 -070075 if (result != AAUDIO_OK) {
Phil Burk6e2770e2018-05-01 13:03:52 -070076 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -080077 }
Phil Burkc0c70e32017-02-09 13:18:38 -080078
Phil Burk5a26e662017-07-07 12:44:48 -070079 setState(AAUDIO_STREAM_STATE_OPEN);
Phil Burk39f02dd2017-08-04 09:13:31 -070080
Phil Burkc0c70e32017-02-09 13:18:38 -080081 return AAUDIO_OK;
82}
83
Phil Burkfbf031e2017-10-12 15:58:31 -070084// Start the flow of data.
Phil Burkbcc36742017-08-31 17:24:51 -070085aaudio_result_t AAudioServiceStreamMMAP::startDevice() {
86 aaudio_result_t result = AAudioServiceStreamBase::startDevice();
Phil Burk39f02dd2017-08-04 09:13:31 -070087 if (!mInService && result == AAUDIO_OK) {
Phil Burkbcc36742017-08-31 17:24:51 -070088 // Note that this can sometimes take 200 to 300 msec for a cold start!
jiabind1f1cb62020-03-24 11:57:57 -070089 result = startClient(mMmapClient, nullptr /*const audio_attributes_t* */, &mClientHandle);
Phil Burkc0c70e32017-02-09 13:18:38 -080090 }
91 return result;
92}
93
Phil Burkfbf031e2017-10-12 15:58:31 -070094// Stop the flow of data such that start() can resume with loss of data.
Phil Burkc0c70e32017-02-09 13:18:38 -080095aaudio_result_t AAudioServiceStreamMMAP::pause() {
Eric Laurentcb4dae22017-07-01 19:39:32 -070096 if (!isRunning()) {
97 return AAUDIO_OK;
98 }
Phil Burk39f02dd2017-08-04 09:13:31 -070099 aaudio_result_t result = AAudioServiceStreamBase::pause();
100 // TODO put before base::pause()?
Eric Laurentcb4dae22017-07-01 19:39:32 -0700101 if (!mInService) {
Phil Burk81ad5ec2017-09-01 10:45:41 -0700102 (void) stopClient(mClientHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700103 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700104 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800105}
106
Phil Burk71f35bb2017-04-13 16:05:07 -0700107aaudio_result_t AAudioServiceStreamMMAP::stop() {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700108 if (!isRunning()) {
109 return AAUDIO_OK;
110 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700111 aaudio_result_t result = AAudioServiceStreamBase::stop();
112 // TODO put before base::stop()?
Eric Laurentcb4dae22017-07-01 19:39:32 -0700113 if (!mInService) {
Phil Burk81ad5ec2017-09-01 10:45:41 -0700114 (void) stopClient(mClientHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700115 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700116 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800117}
118
Eric Laurentcb4dae22017-07-01 19:39:32 -0700119aaudio_result_t AAudioServiceStreamMMAP::startClient(const android::AudioClient& client,
jiabind1f1cb62020-03-24 11:57:57 -0700120 const audio_attributes_t *attr,
121 audio_port_handle_t *clientHandle) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700122 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
123 if (endpoint == nullptr) {
124 ALOGE("%s() has no endpoint", __func__);
125 return AAUDIO_ERROR_INVALID_STATE;
126 }
Phil Burkbcc36742017-08-31 17:24:51 -0700127 // Start the client on behalf of the application. Generate a new porthandle.
jiabind1f1cb62020-03-24 11:57:57 -0700128 aaudio_result_t result = endpoint->startClient(client, attr, clientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700129 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700130}
131
132aaudio_result_t AAudioServiceStreamMMAP::stopClient(audio_port_handle_t clientHandle) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700133 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
134 if (endpoint == nullptr) {
135 ALOGE("%s() has no endpoint", __func__);
136 return AAUDIO_ERROR_INVALID_STATE;
137 }
138 aaudio_result_t result = endpoint->stopClient(clientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700139 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700140}
141
Phil Burk97350f92017-07-21 15:59:44 -0700142// Get free-running DSP or DMA hardware position from the HAL.
Phil Burkc0c70e32017-02-09 13:18:38 -0800143aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames,
Phil Burk39f02dd2017-08-04 09:13:31 -0700144 int64_t *timeNanos) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700145 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
146 if (endpoint == nullptr) {
147 ALOGE("%s() has no endpoint", __func__);
148 return AAUDIO_ERROR_INVALID_STATE;
149 }
150 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
151 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
152
Phil Burk39f02dd2017-08-04 09:13:31 -0700153 aaudio_result_t result = serviceEndpointMMAP->getFreeRunningPosition(positionFrames, timeNanos);
154 if (result == AAUDIO_OK) {
155 Timestamp timestamp(*positionFrames, *timeNanos);
Phil Burka53ffa62018-10-10 16:21:37 -0700156 mAtomicStreamTimestamp.write(timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700157 *positionFrames = timestamp.getPosition();
158 *timeNanos = timestamp.getNanoseconds();
Phil Burk39f02dd2017-08-04 09:13:31 -0700159 } else if (result != AAUDIO_ERROR_UNAVAILABLE) {
160 disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800161 }
Phil Burk940083c2017-07-17 17:00:02 -0700162 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800163}
164
Phil Burk97350f92017-07-21 15:59:44 -0700165// Get timestamp that was written by getFreeRunningPosition()
166aaudio_result_t AAudioServiceStreamMMAP::getHardwareTimestamp(int64_t *positionFrames,
167 int64_t *timeNanos) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700168
169 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
170 if (endpoint == nullptr) {
171 ALOGE("%s() has no endpoint", __func__);
172 return AAUDIO_ERROR_INVALID_STATE;
173 }
174 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
175 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
176
Phil Burk97350f92017-07-21 15:59:44 -0700177 // TODO Get presentation timestamp from the HAL
Phil Burka53ffa62018-10-10 16:21:37 -0700178 if (mAtomicStreamTimestamp.isValid()) {
179 Timestamp timestamp = mAtomicStreamTimestamp.read();
Phil Burk97350f92017-07-21 15:59:44 -0700180 *positionFrames = timestamp.getPosition();
Phil Burk39f02dd2017-08-04 09:13:31 -0700181 *timeNanos = timestamp.getNanoseconds() + serviceEndpointMMAP->getHardwareTimeOffsetNanos();
Phil Burk97350f92017-07-21 15:59:44 -0700182 return AAUDIO_OK;
183 } else {
184 return AAUDIO_ERROR_UNAVAILABLE;
185 }
186}
187
Phil Burkfbf031e2017-10-12 15:58:31 -0700188// Get an immutable description of the data queue from the HAL.
Phil Burk523b3042017-09-13 13:03:08 -0700189aaudio_result_t AAudioServiceStreamMMAP::getAudioDataDescription(
190 AudioEndpointParcelable &parcelable)
Phil Burkc0c70e32017-02-09 13:18:38 -0800191{
Phil Burk6e2770e2018-05-01 13:03:52 -0700192 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
193 if (endpoint == nullptr) {
194 ALOGE("%s() has no endpoint", __func__);
195 return AAUDIO_ERROR_INVALID_STATE;
196 }
197 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
198 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
Phil Burk39f02dd2017-08-04 09:13:31 -0700199 return serviceEndpointMMAP->getDownDataDescription(parcelable);
Phil Burkec89b2e2017-06-20 15:05:06 -0700200}