blob: 2e7ff8b2625f2810cdcb2ef6e33e20785f99bd83 [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
67 aaudio_result_t result = AAudioServiceStreamBase::open(request,
68 AAUDIO_SHARING_MODE_EXCLUSIVE);
Phil Burkc0c70e32017-02-09 13:18:38 -080069 if (result != AAUDIO_OK) {
Phil Burkc0c70e32017-02-09 13:18:38 -080070 return result;
71 }
72
Phil Burk92c3e262018-05-01 13:03:52 -070073 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
74 if (endpoint == nullptr) {
75 ALOGE("%s() has no endpoint", __func__);
76 return AAUDIO_ERROR_INVALID_STATE;
77 }
78
79 result = endpoint->registerStream(keep);
Phil Burk39f02dd2017-08-04 09:13:31 -070080 if (result != AAUDIO_OK) {
Phil Burk92c3e262018-05-01 13:03:52 -070081 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -080082 }
Phil Burkc0c70e32017-02-09 13:18:38 -080083
Phil Burk5a26e662017-07-07 12:44:48 -070084 setState(AAUDIO_STREAM_STATE_OPEN);
Phil Burk39f02dd2017-08-04 09:13:31 -070085
Phil Burkc0c70e32017-02-09 13:18:38 -080086 return AAUDIO_OK;
87}
88
Phil Burkc0c70e32017-02-09 13:18:38 -080089/**
90 * Start the flow of data.
91 */
Phil Burkbcc36742017-08-31 17:24:51 -070092aaudio_result_t AAudioServiceStreamMMAP::startDevice() {
93 aaudio_result_t result = AAudioServiceStreamBase::startDevice();
Phil Burk39f02dd2017-08-04 09:13:31 -070094 if (!mInService && result == AAUDIO_OK) {
Phil Burkbcc36742017-08-31 17:24:51 -070095 // Note that this can sometimes take 200 to 300 msec for a cold start!
Phil Burk81ad5ec2017-09-01 10:45:41 -070096 result = startClient(mMmapClient, &mClientHandle);
Phil Burkc0c70e32017-02-09 13:18:38 -080097 }
98 return result;
99}
100
101/**
102 * Stop the flow of data such that start() can resume with loss of data.
103 */
104aaudio_result_t AAudioServiceStreamMMAP::pause() {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700105 if (!isRunning()) {
106 return AAUDIO_OK;
107 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700108 aaudio_result_t result = AAudioServiceStreamBase::pause();
109 // TODO put before base::pause()?
Eric Laurentcb4dae22017-07-01 19:39:32 -0700110 if (!mInService) {
Phil Burk81ad5ec2017-09-01 10:45:41 -0700111 (void) stopClient(mClientHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700112 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700113 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800114}
115
Phil Burk71f35bb2017-04-13 16:05:07 -0700116aaudio_result_t AAudioServiceStreamMMAP::stop() {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700117 if (!isRunning()) {
118 return AAUDIO_OK;
119 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700120 aaudio_result_t result = AAudioServiceStreamBase::stop();
121 // TODO put before base::stop()?
Eric Laurentcb4dae22017-07-01 19:39:32 -0700122 if (!mInService) {
Phil Burk81ad5ec2017-09-01 10:45:41 -0700123 (void) stopClient(mClientHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700124 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700125 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800126}
127
Eric Laurentcb4dae22017-07-01 19:39:32 -0700128aaudio_result_t AAudioServiceStreamMMAP::startClient(const android::AudioClient& client,
Phil Burk39f02dd2017-08-04 09:13:31 -0700129 audio_port_handle_t *clientHandle) {
Phil Burk92c3e262018-05-01 13:03:52 -0700130 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
131 if (endpoint == nullptr) {
132 ALOGE("%s() has no endpoint", __func__);
133 return AAUDIO_ERROR_INVALID_STATE;
134 }
Phil Burkbcc36742017-08-31 17:24:51 -0700135 // Start the client on behalf of the application. Generate a new porthandle.
Phil Burk92c3e262018-05-01 13:03:52 -0700136 aaudio_result_t result = endpoint->startClient(client, clientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700137 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700138}
139
140aaudio_result_t AAudioServiceStreamMMAP::stopClient(audio_port_handle_t clientHandle) {
Phil Burk92c3e262018-05-01 13:03:52 -0700141 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
142 if (endpoint == nullptr) {
143 ALOGE("%s() has no endpoint", __func__);
144 return AAUDIO_ERROR_INVALID_STATE;
145 }
146 aaudio_result_t result = endpoint->stopClient(clientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700147 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700148}
149
Phil Burk97350f92017-07-21 15:59:44 -0700150// Get free-running DSP or DMA hardware position from the HAL.
Phil Burkc0c70e32017-02-09 13:18:38 -0800151aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames,
Phil Burk39f02dd2017-08-04 09:13:31 -0700152 int64_t *timeNanos) {
Phil Burk92c3e262018-05-01 13:03:52 -0700153 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
154 if (endpoint == nullptr) {
155 ALOGE("%s() has no endpoint", __func__);
156 return AAUDIO_ERROR_INVALID_STATE;
157 }
158 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
159 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
160
Phil Burk39f02dd2017-08-04 09:13:31 -0700161 aaudio_result_t result = serviceEndpointMMAP->getFreeRunningPosition(positionFrames, timeNanos);
162 if (result == AAUDIO_OK) {
163 Timestamp timestamp(*positionFrames, *timeNanos);
Phil Burk97350f92017-07-21 15:59:44 -0700164 mAtomicTimestamp.write(timestamp);
165 *positionFrames = timestamp.getPosition();
166 *timeNanos = timestamp.getNanoseconds();
Phil Burk39f02dd2017-08-04 09:13:31 -0700167 } else if (result != AAUDIO_ERROR_UNAVAILABLE) {
168 disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800169 }
Phil Burk940083c2017-07-17 17:00:02 -0700170 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800171}
172
Phil Burk97350f92017-07-21 15:59:44 -0700173// Get timestamp that was written by getFreeRunningPosition()
174aaudio_result_t AAudioServiceStreamMMAP::getHardwareTimestamp(int64_t *positionFrames,
175 int64_t *timeNanos) {
Phil Burk92c3e262018-05-01 13:03:52 -0700176
177 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
178 if (endpoint == nullptr) {
179 ALOGE("%s() has no endpoint", __func__);
180 return AAUDIO_ERROR_INVALID_STATE;
181 }
182 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
183 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
184
Phil Burk97350f92017-07-21 15:59:44 -0700185 // TODO Get presentation timestamp from the HAL
186 if (mAtomicTimestamp.isValid()) {
187 Timestamp timestamp = mAtomicTimestamp.read();
188 *positionFrames = timestamp.getPosition();
Phil Burk39f02dd2017-08-04 09:13:31 -0700189 *timeNanos = timestamp.getNanoseconds() + serviceEndpointMMAP->getHardwareTimeOffsetNanos();
Phil Burk97350f92017-07-21 15:59:44 -0700190 return AAUDIO_OK;
191 } else {
192 return AAUDIO_ERROR_UNAVAILABLE;
193 }
194}
195
Phil Burkc0c70e32017-02-09 13:18:38 -0800196/**
197 * Get an immutable description of the data queue from the HAL.
198 */
Phil Burk523b3042017-09-13 13:03:08 -0700199aaudio_result_t AAudioServiceStreamMMAP::getAudioDataDescription(
200 AudioEndpointParcelable &parcelable)
Phil Burkc0c70e32017-02-09 13:18:38 -0800201{
Phil Burk92c3e262018-05-01 13:03:52 -0700202 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
203 if (endpoint == nullptr) {
204 ALOGE("%s() has no endpoint", __func__);
205 return AAUDIO_ERROR_INVALID_STATE;
206 }
207 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
208 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
Phil Burk39f02dd2017-08-04 09:13:31 -0700209 return serviceEndpointMMAP->getDownDataDescription(parcelable);
Phil Burkec89b2e2017-06-20 15:05:06 -0700210}