blob: f4e72b7bd3c1c9677d04ca6eb657e4e967e13994 [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!
Phil Burk81ad5ec2017-09-01 10:45:41 -070089 result = startClient(mMmapClient, &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,
Phil Burk39f02dd2017-08-04 09:13:31 -0700120 audio_port_handle_t *clientHandle) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700121 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
122 if (endpoint == nullptr) {
123 ALOGE("%s() has no endpoint", __func__);
124 return AAUDIO_ERROR_INVALID_STATE;
125 }
Phil Burkbcc36742017-08-31 17:24:51 -0700126 // Start the client on behalf of the application. Generate a new porthandle.
Phil Burk6e2770e2018-05-01 13:03:52 -0700127 aaudio_result_t result = endpoint->startClient(client, clientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700128 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700129}
130
131aaudio_result_t AAudioServiceStreamMMAP::stopClient(audio_port_handle_t clientHandle) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700132 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
133 if (endpoint == nullptr) {
134 ALOGE("%s() has no endpoint", __func__);
135 return AAUDIO_ERROR_INVALID_STATE;
136 }
137 aaudio_result_t result = endpoint->stopClient(clientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700138 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700139}
140
Phil Burk97350f92017-07-21 15:59:44 -0700141// Get free-running DSP or DMA hardware position from the HAL.
Phil Burkc0c70e32017-02-09 13:18:38 -0800142aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames,
Phil Burk39f02dd2017-08-04 09:13:31 -0700143 int64_t *timeNanos) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700144 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
145 if (endpoint == nullptr) {
146 ALOGE("%s() has no endpoint", __func__);
147 return AAUDIO_ERROR_INVALID_STATE;
148 }
149 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
150 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
151
Phil Burk39f02dd2017-08-04 09:13:31 -0700152 aaudio_result_t result = serviceEndpointMMAP->getFreeRunningPosition(positionFrames, timeNanos);
153 if (result == AAUDIO_OK) {
154 Timestamp timestamp(*positionFrames, *timeNanos);
Phil Burka53ffa62018-10-10 16:21:37 -0700155 mAtomicStreamTimestamp.write(timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700156 *positionFrames = timestamp.getPosition();
157 *timeNanos = timestamp.getNanoseconds();
Phil Burk39f02dd2017-08-04 09:13:31 -0700158 } else if (result != AAUDIO_ERROR_UNAVAILABLE) {
159 disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800160 }
Phil Burk940083c2017-07-17 17:00:02 -0700161 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800162}
163
Phil Burk97350f92017-07-21 15:59:44 -0700164// Get timestamp that was written by getFreeRunningPosition()
165aaudio_result_t AAudioServiceStreamMMAP::getHardwareTimestamp(int64_t *positionFrames,
166 int64_t *timeNanos) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700167
168 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
169 if (endpoint == nullptr) {
170 ALOGE("%s() has no endpoint", __func__);
171 return AAUDIO_ERROR_INVALID_STATE;
172 }
173 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
174 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
175
Phil Burk97350f92017-07-21 15:59:44 -0700176 // TODO Get presentation timestamp from the HAL
Phil Burka53ffa62018-10-10 16:21:37 -0700177 if (mAtomicStreamTimestamp.isValid()) {
178 Timestamp timestamp = mAtomicStreamTimestamp.read();
Phil Burk97350f92017-07-21 15:59:44 -0700179 *positionFrames = timestamp.getPosition();
Phil Burk39f02dd2017-08-04 09:13:31 -0700180 *timeNanos = timestamp.getNanoseconds() + serviceEndpointMMAP->getHardwareTimeOffsetNanos();
Phil Burk97350f92017-07-21 15:59:44 -0700181 return AAUDIO_OK;
182 } else {
183 return AAUDIO_ERROR_UNAVAILABLE;
184 }
185}
186
Phil Burkfbf031e2017-10-12 15:58:31 -0700187// Get an immutable description of the data queue from the HAL.
Phil Burk523b3042017-09-13 13:03:08 -0700188aaudio_result_t AAudioServiceStreamMMAP::getAudioDataDescription(
189 AudioEndpointParcelable &parcelable)
Phil Burkc0c70e32017-02-09 13:18:38 -0800190{
Phil Burk6e2770e2018-05-01 13:03:52 -0700191 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
192 if (endpoint == nullptr) {
193 ALOGE("%s() has no endpoint", __func__);
194 return AAUDIO_ERROR_INVALID_STATE;
195 }
196 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
197 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
Phil Burk39f02dd2017-08-04 09:13:31 -0700198 return serviceEndpointMMAP->getDownDataDescription(parcelable);
Phil Burkec89b2e2017-06-20 15:05:06 -0700199}