blob: 837b080fae758fd34cd4defd804b5a2f5564349b [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 -080052aaudio_result_t AAudioServiceStreamMMAP::close() {
Phil Burkbcc36742017-08-31 17:24:51 -070053 if (getState() == AAUDIO_STREAM_STATE_CLOSED) {
Phil Burk98d6d922017-07-06 11:52:45 -070054 return AAUDIO_OK;
55 }
Phil Burk39f02dd2017-08-04 09:13:31 -070056
Eric Laurentcb4dae22017-07-01 19:39:32 -070057 stop();
Phil Burk71f35bb2017-04-13 16:05:07 -070058
Phil Burkc0c70e32017-02-09 13:18:38 -080059 return AAudioServiceStreamBase::close();
60}
61
62// Open stream on HAL and pass information about the shared memory buffer back to the client.
Phil Burk39f02dd2017-08-04 09:13:31 -070063aaudio_result_t AAudioServiceStreamMMAP::open(const aaudio::AAudioStreamRequest &request) {
Phil Burkc0c70e32017-02-09 13:18:38 -080064
Phil Burk39f02dd2017-08-04 09:13:31 -070065 sp<AAudioServiceStreamMMAP> keep(this);
66
Phil Burk15f97c92018-09-04 14:06:27 -070067 if (request.getConstantConfiguration().getSharingMode() != AAUDIO_SHARING_MODE_EXCLUSIVE) {
68 ALOGE("%s() sharingMode mismatch %d", __func__,
69 request.getConstantConfiguration().getSharingMode());
70 return AAUDIO_ERROR_INTERNAL;
71 }
72
73 aaudio_result_t result = AAudioServiceStreamBase::open(request);
Phil Burkc0c70e32017-02-09 13:18:38 -080074 if (result != AAUDIO_OK) {
Phil Burkc0c70e32017-02-09 13:18:38 -080075 return result;
76 }
77
Phil Burk6e2770e2018-05-01 13:03:52 -070078 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
79 if (endpoint == nullptr) {
80 ALOGE("%s() has no endpoint", __func__);
81 return AAUDIO_ERROR_INVALID_STATE;
82 }
83
84 result = endpoint->registerStream(keep);
Phil Burk39f02dd2017-08-04 09:13:31 -070085 if (result != AAUDIO_OK) {
Phil Burk6e2770e2018-05-01 13:03:52 -070086 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -080087 }
Phil Burkc0c70e32017-02-09 13:18:38 -080088
Phil Burk5a26e662017-07-07 12:44:48 -070089 setState(AAUDIO_STREAM_STATE_OPEN);
Phil Burk39f02dd2017-08-04 09:13:31 -070090
Phil Burkc0c70e32017-02-09 13:18:38 -080091 return AAUDIO_OK;
92}
93
Phil Burkfbf031e2017-10-12 15:58:31 -070094// Start the flow of data.
Phil Burkbcc36742017-08-31 17:24:51 -070095aaudio_result_t AAudioServiceStreamMMAP::startDevice() {
96 aaudio_result_t result = AAudioServiceStreamBase::startDevice();
Phil Burk39f02dd2017-08-04 09:13:31 -070097 if (!mInService && result == AAUDIO_OK) {
Phil Burkbcc36742017-08-31 17:24:51 -070098 // Note that this can sometimes take 200 to 300 msec for a cold start!
Phil Burk81ad5ec2017-09-01 10:45:41 -070099 result = startClient(mMmapClient, &mClientHandle);
Phil Burkc0c70e32017-02-09 13:18:38 -0800100 }
101 return result;
102}
103
Phil Burkfbf031e2017-10-12 15:58:31 -0700104// Stop the flow of data such that start() can resume with loss of data.
Phil Burkc0c70e32017-02-09 13:18:38 -0800105aaudio_result_t AAudioServiceStreamMMAP::pause() {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700106 if (!isRunning()) {
107 return AAUDIO_OK;
108 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700109 aaudio_result_t result = AAudioServiceStreamBase::pause();
110 // TODO put before base::pause()?
Eric Laurentcb4dae22017-07-01 19:39:32 -0700111 if (!mInService) {
Phil Burk81ad5ec2017-09-01 10:45:41 -0700112 (void) stopClient(mClientHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700113 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700114 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800115}
116
Phil Burk71f35bb2017-04-13 16:05:07 -0700117aaudio_result_t AAudioServiceStreamMMAP::stop() {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700118 if (!isRunning()) {
119 return AAUDIO_OK;
120 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700121 aaudio_result_t result = AAudioServiceStreamBase::stop();
122 // TODO put before base::stop()?
Eric Laurentcb4dae22017-07-01 19:39:32 -0700123 if (!mInService) {
Phil Burk81ad5ec2017-09-01 10:45:41 -0700124 (void) stopClient(mClientHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700125 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700126 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800127}
128
Eric Laurentcb4dae22017-07-01 19:39:32 -0700129aaudio_result_t AAudioServiceStreamMMAP::startClient(const android::AudioClient& client,
Phil Burk39f02dd2017-08-04 09:13:31 -0700130 audio_port_handle_t *clientHandle) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700131 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
132 if (endpoint == nullptr) {
133 ALOGE("%s() has no endpoint", __func__);
134 return AAUDIO_ERROR_INVALID_STATE;
135 }
Phil Burkbcc36742017-08-31 17:24:51 -0700136 // Start the client on behalf of the application. Generate a new porthandle.
Phil Burk6e2770e2018-05-01 13:03:52 -0700137 aaudio_result_t result = endpoint->startClient(client, clientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700138 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700139}
140
141aaudio_result_t AAudioServiceStreamMMAP::stopClient(audio_port_handle_t clientHandle) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700142 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
143 if (endpoint == nullptr) {
144 ALOGE("%s() has no endpoint", __func__);
145 return AAUDIO_ERROR_INVALID_STATE;
146 }
147 aaudio_result_t result = endpoint->stopClient(clientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700148 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700149}
150
Phil Burk97350f92017-07-21 15:59:44 -0700151// Get free-running DSP or DMA hardware position from the HAL.
Phil Burkc0c70e32017-02-09 13:18:38 -0800152aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames,
Phil Burk39f02dd2017-08-04 09:13:31 -0700153 int64_t *timeNanos) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700154 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
155 if (endpoint == nullptr) {
156 ALOGE("%s() has no endpoint", __func__);
157 return AAUDIO_ERROR_INVALID_STATE;
158 }
159 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
160 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
161
Phil Burk39f02dd2017-08-04 09:13:31 -0700162 aaudio_result_t result = serviceEndpointMMAP->getFreeRunningPosition(positionFrames, timeNanos);
163 if (result == AAUDIO_OK) {
164 Timestamp timestamp(*positionFrames, *timeNanos);
Phil Burka53ffa62018-10-10 16:21:37 -0700165 mAtomicStreamTimestamp.write(timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700166 *positionFrames = timestamp.getPosition();
167 *timeNanos = timestamp.getNanoseconds();
Phil Burk39f02dd2017-08-04 09:13:31 -0700168 } else if (result != AAUDIO_ERROR_UNAVAILABLE) {
169 disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800170 }
Phil Burk940083c2017-07-17 17:00:02 -0700171 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800172}
173
Phil Burk97350f92017-07-21 15:59:44 -0700174// Get timestamp that was written by getFreeRunningPosition()
175aaudio_result_t AAudioServiceStreamMMAP::getHardwareTimestamp(int64_t *positionFrames,
176 int64_t *timeNanos) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700177
178 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
179 if (endpoint == nullptr) {
180 ALOGE("%s() has no endpoint", __func__);
181 return AAUDIO_ERROR_INVALID_STATE;
182 }
183 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
184 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
185
Phil Burk97350f92017-07-21 15:59:44 -0700186 // TODO Get presentation timestamp from the HAL
Phil Burka53ffa62018-10-10 16:21:37 -0700187 if (mAtomicStreamTimestamp.isValid()) {
188 Timestamp timestamp = mAtomicStreamTimestamp.read();
Phil Burk97350f92017-07-21 15:59:44 -0700189 *positionFrames = timestamp.getPosition();
Phil Burk39f02dd2017-08-04 09:13:31 -0700190 *timeNanos = timestamp.getNanoseconds() + serviceEndpointMMAP->getHardwareTimeOffsetNanos();
Phil Burk97350f92017-07-21 15:59:44 -0700191 return AAUDIO_OK;
192 } else {
193 return AAUDIO_ERROR_UNAVAILABLE;
194 }
195}
196
Phil Burkfbf031e2017-10-12 15:58:31 -0700197// Get an immutable description of the data queue from the HAL.
Phil Burk523b3042017-09-13 13:03:08 -0700198aaudio_result_t AAudioServiceStreamMMAP::getAudioDataDescription(
199 AudioEndpointParcelable &parcelable)
Phil Burkc0c70e32017-02-09 13:18:38 -0800200{
Phil Burk6e2770e2018-05-01 13:03:52 -0700201 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
202 if (endpoint == nullptr) {
203 ALOGE("%s() has no endpoint", __func__);
204 return AAUDIO_ERROR_INVALID_STATE;
205 }
206 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
207 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
Phil Burk39f02dd2017-08-04 09:13:31 -0700208 return serviceEndpointMMAP->getDownDataDescription(parcelable);
Phil Burkec89b2e2017-06-20 15:05:06 -0700209}