blob: c8453091409097060f13e3cfa350a9313b85cb86 [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 Burk6e2770e2018-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 Burk6e2770e2018-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 Burkfbf031e2017-10-12 15:58:31 -070089// Start the flow of data.
Phil Burkbcc36742017-08-31 17:24:51 -070090aaudio_result_t AAudioServiceStreamMMAP::startDevice() {
91 aaudio_result_t result = AAudioServiceStreamBase::startDevice();
Phil Burk39f02dd2017-08-04 09:13:31 -070092 if (!mInService && result == AAUDIO_OK) {
Phil Burkbcc36742017-08-31 17:24:51 -070093 // Note that this can sometimes take 200 to 300 msec for a cold start!
Phil Burk81ad5ec2017-09-01 10:45:41 -070094 result = startClient(mMmapClient, &mClientHandle);
Phil Burkc0c70e32017-02-09 13:18:38 -080095 }
96 return result;
97}
98
Phil Burkfbf031e2017-10-12 15:58:31 -070099// Stop the flow of data such that start() can resume with loss of data.
Phil Burkc0c70e32017-02-09 13:18:38 -0800100aaudio_result_t AAudioServiceStreamMMAP::pause() {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700101 if (!isRunning()) {
102 return AAUDIO_OK;
103 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700104 aaudio_result_t result = AAudioServiceStreamBase::pause();
105 // TODO put before base::pause()?
Eric Laurentcb4dae22017-07-01 19:39:32 -0700106 if (!mInService) {
Phil Burk81ad5ec2017-09-01 10:45:41 -0700107 (void) stopClient(mClientHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700108 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700109 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800110}
111
Phil Burk71f35bb2017-04-13 16:05:07 -0700112aaudio_result_t AAudioServiceStreamMMAP::stop() {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700113 if (!isRunning()) {
114 return AAUDIO_OK;
115 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700116 aaudio_result_t result = AAudioServiceStreamBase::stop();
117 // TODO put before base::stop()?
Eric Laurentcb4dae22017-07-01 19:39:32 -0700118 if (!mInService) {
Phil Burk81ad5ec2017-09-01 10:45:41 -0700119 (void) stopClient(mClientHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700120 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700121 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800122}
123
Eric Laurentcb4dae22017-07-01 19:39:32 -0700124aaudio_result_t AAudioServiceStreamMMAP::startClient(const android::AudioClient& client,
Phil Burk39f02dd2017-08-04 09:13:31 -0700125 audio_port_handle_t *clientHandle) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700126 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
127 if (endpoint == nullptr) {
128 ALOGE("%s() has no endpoint", __func__);
129 return AAUDIO_ERROR_INVALID_STATE;
130 }
Phil Burkbcc36742017-08-31 17:24:51 -0700131 // Start the client on behalf of the application. Generate a new porthandle.
Phil Burk6e2770e2018-05-01 13:03:52 -0700132 aaudio_result_t result = endpoint->startClient(client, clientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700133 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700134}
135
136aaudio_result_t AAudioServiceStreamMMAP::stopClient(audio_port_handle_t clientHandle) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700137 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
138 if (endpoint == nullptr) {
139 ALOGE("%s() has no endpoint", __func__);
140 return AAUDIO_ERROR_INVALID_STATE;
141 }
142 aaudio_result_t result = endpoint->stopClient(clientHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700143 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700144}
145
Phil Burk97350f92017-07-21 15:59:44 -0700146// Get free-running DSP or DMA hardware position from the HAL.
Phil Burkc0c70e32017-02-09 13:18:38 -0800147aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames,
Phil Burk39f02dd2017-08-04 09:13:31 -0700148 int64_t *timeNanos) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700149 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
150 if (endpoint == nullptr) {
151 ALOGE("%s() has no endpoint", __func__);
152 return AAUDIO_ERROR_INVALID_STATE;
153 }
154 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
155 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
156
Phil Burk39f02dd2017-08-04 09:13:31 -0700157 aaudio_result_t result = serviceEndpointMMAP->getFreeRunningPosition(positionFrames, timeNanos);
158 if (result == AAUDIO_OK) {
159 Timestamp timestamp(*positionFrames, *timeNanos);
Phil Burk97350f92017-07-21 15:59:44 -0700160 mAtomicTimestamp.write(timestamp);
161 *positionFrames = timestamp.getPosition();
162 *timeNanos = timestamp.getNanoseconds();
Phil Burk39f02dd2017-08-04 09:13:31 -0700163 } else if (result != AAUDIO_ERROR_UNAVAILABLE) {
164 disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800165 }
Phil Burk940083c2017-07-17 17:00:02 -0700166 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800167}
168
Phil Burk97350f92017-07-21 15:59:44 -0700169// Get timestamp that was written by getFreeRunningPosition()
170aaudio_result_t AAudioServiceStreamMMAP::getHardwareTimestamp(int64_t *positionFrames,
171 int64_t *timeNanos) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700172
173 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
174 if (endpoint == nullptr) {
175 ALOGE("%s() has no endpoint", __func__);
176 return AAUDIO_ERROR_INVALID_STATE;
177 }
178 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
179 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
180
Phil Burk97350f92017-07-21 15:59:44 -0700181 // TODO Get presentation timestamp from the HAL
182 if (mAtomicTimestamp.isValid()) {
183 Timestamp timestamp = mAtomicTimestamp.read();
184 *positionFrames = timestamp.getPosition();
Phil Burk39f02dd2017-08-04 09:13:31 -0700185 *timeNanos = timestamp.getNanoseconds() + serviceEndpointMMAP->getHardwareTimeOffsetNanos();
Phil Burk97350f92017-07-21 15:59:44 -0700186 return AAUDIO_OK;
187 } else {
188 return AAUDIO_ERROR_UNAVAILABLE;
189 }
190}
191
Phil Burkfbf031e2017-10-12 15:58:31 -0700192// Get an immutable description of the data queue from the HAL.
Phil Burk523b3042017-09-13 13:03:08 -0700193aaudio_result_t AAudioServiceStreamMMAP::getAudioDataDescription(
194 AudioEndpointParcelable &parcelable)
Phil Burkc0c70e32017-02-09 13:18:38 -0800195{
Phil Burk6e2770e2018-05-01 13:03:52 -0700196 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
197 if (endpoint == nullptr) {
198 ALOGE("%s() has no endpoint", __func__);
199 return AAUDIO_ERROR_INVALID_STATE;
200 }
201 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
202 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
Phil Burk39f02dd2017-08-04 09:13:31 -0700203 return serviceEndpointMMAP->getDownDataDescription(parcelable);
Phil Burkec89b2e2017-06-20 15:05:06 -0700204}