1 /*
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
17 #define LOG_TAG "AAudioServiceStreamMMAP"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
20
21 #include <atomic>
22 #include <inttypes.h>
23 #include <iomanip>
24 #include <iostream>
25 #include <stdint.h>
26
27 #include <utils/String16.h>
28 #include <media/nbaio/AudioStreamOutSink.h>
29 #include <media/MmapStreamInterface.h>
30
31 #include "binding/AudioEndpointParcelable.h"
32 #include "utility/AAudioUtilities.h"
33
34 #include "AAudioServiceEndpointMMAP.h"
35 #include "AAudioServiceStreamBase.h"
36 #include "AAudioServiceStreamMMAP.h"
37 #include "SharedMemoryProxy.h"
38
39 using android::base::unique_fd;
40 using namespace android;
41 using namespace aaudio;
42
43 /**
44 * Service Stream that uses an MMAP buffer.
45 */
46
AAudioServiceStreamMMAP(android::AAudioService & aAudioService,bool inService)47 AAudioServiceStreamMMAP::AAudioServiceStreamMMAP(android::AAudioService &aAudioService,
48 bool inService)
49 : AAudioServiceStreamBase(aAudioService)
50 , mInService(inService) {
51 }
52
53 // Open stream on HAL and pass information about the shared memory buffer back to the client.
open(const aaudio::AAudioStreamRequest & request)54 aaudio_result_t AAudioServiceStreamMMAP::open(const aaudio::AAudioStreamRequest &request) {
55
56 sp<AAudioServiceStreamMMAP> keep(this);
57
58 if (request.getConstantConfiguration().getSharingMode() != AAUDIO_SHARING_MODE_EXCLUSIVE) {
59 ALOGE("%s() sharingMode mismatch %d", __func__,
60 request.getConstantConfiguration().getSharingMode());
61 return AAUDIO_ERROR_INTERNAL;
62 }
63
64 aaudio_result_t result = AAudioServiceStreamBase::open(request);
65 if (result != AAUDIO_OK) {
66 return result;
67 }
68
69 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
70 if (endpoint == nullptr) {
71 ALOGE("%s() has no endpoint", __func__);
72 return AAUDIO_ERROR_INVALID_STATE;
73 }
74
75 result = endpoint->registerStream(keep);
76 if (result != AAUDIO_OK) {
77 return result;
78 }
79
80 setState(AAUDIO_STREAM_STATE_OPEN);
81
82 return AAUDIO_OK;
83 }
84
85 // Start the flow of data.
startDevice()86 aaudio_result_t AAudioServiceStreamMMAP::startDevice() {
87 aaudio_result_t result = AAudioServiceStreamBase::startDevice();
88 if (!mInService && result == AAUDIO_OK) {
89 // Note that this can sometimes take 200 to 300 msec for a cold start!
90 result = startClient(mMmapClient, nullptr /*const audio_attributes_t* */, &mClientHandle);
91 }
92 return result;
93 }
94
95 // Stop the flow of data such that start() can resume with loss of data.
pause_l()96 aaudio_result_t AAudioServiceStreamMMAP::pause_l() {
97 if (!isRunning()) {
98 return AAUDIO_OK;
99 }
100 aaudio_result_t result = AAudioServiceStreamBase::pause_l();
101 // TODO put before base::pause()?
102 if (!mInService) {
103 (void) stopClient(mClientHandle);
104 }
105 return result;
106 }
107
stop_l()108 aaudio_result_t AAudioServiceStreamMMAP::stop_l() {
109 if (!isRunning()) {
110 return AAUDIO_OK;
111 }
112 aaudio_result_t result = AAudioServiceStreamBase::stop_l();
113 // TODO put before base::stop()?
114 if (!mInService) {
115 (void) stopClient(mClientHandle);
116 }
117 return result;
118 }
119
startClient(const android::AudioClient & client,const audio_attributes_t * attr,audio_port_handle_t * clientHandle)120 aaudio_result_t AAudioServiceStreamMMAP::startClient(const android::AudioClient& client,
121 const audio_attributes_t *attr,
122 audio_port_handle_t *clientHandle) {
123 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
124 if (endpoint == nullptr) {
125 ALOGE("%s() has no endpoint", __func__);
126 return AAUDIO_ERROR_INVALID_STATE;
127 }
128 // Start the client on behalf of the application. Generate a new porthandle.
129 aaudio_result_t result = endpoint->startClient(client, attr, clientHandle);
130 return result;
131 }
132
stopClient(audio_port_handle_t clientHandle)133 aaudio_result_t AAudioServiceStreamMMAP::stopClient(audio_port_handle_t clientHandle) {
134 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
135 if (endpoint == nullptr) {
136 ALOGE("%s() has no endpoint", __func__);
137 return AAUDIO_ERROR_INVALID_STATE;
138 }
139 aaudio_result_t result = endpoint->stopClient(clientHandle);
140 return result;
141 }
142
143 // Get free-running DSP or DMA hardware position from the HAL.
getFreeRunningPosition(int64_t * positionFrames,int64_t * timeNanos)144 aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames,
145 int64_t *timeNanos) {
146 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
147 if (endpoint == nullptr) {
148 ALOGE("%s() has no endpoint", __func__);
149 return AAUDIO_ERROR_INVALID_STATE;
150 }
151 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
152 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
153
154 aaudio_result_t result = serviceEndpointMMAP->getFreeRunningPosition(positionFrames, timeNanos);
155 if (result == AAUDIO_OK) {
156 Timestamp timestamp(*positionFrames, *timeNanos);
157 mAtomicStreamTimestamp.write(timestamp);
158 *positionFrames = timestamp.getPosition();
159 *timeNanos = timestamp.getNanoseconds();
160 } else if (result != AAUDIO_ERROR_UNAVAILABLE) {
161 disconnect();
162 }
163 return result;
164 }
165
166 // Get timestamp from presentation position.
167 // If it fails, get timestamp that was written by getFreeRunningPosition()
getHardwareTimestamp(int64_t * positionFrames,int64_t * timeNanos)168 aaudio_result_t AAudioServiceStreamMMAP::getHardwareTimestamp(int64_t *positionFrames,
169 int64_t *timeNanos) {
170
171 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
172 if (endpoint == nullptr) {
173 ALOGE("%s() has no endpoint", __func__);
174 return AAUDIO_ERROR_INVALID_STATE;
175 }
176 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
177 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
178
179 // Disable this code temporarily because the HAL is not returning
180 // a useful result.
181 #if 0
182 uint64_t position;
183 if (serviceEndpointMMAP->getExternalPosition(&position, timeNanos) == AAUDIO_OK) {
184 ALOGD("%s() getExternalPosition() says pos = %" PRIi64 ", time = %" PRIi64,
185 __func__, position, *timeNanos);
186 *positionFrames = (int64_t) position;
187 return AAUDIO_OK;
188 } else
189 #endif
190 if (mAtomicStreamTimestamp.isValid()) {
191 Timestamp timestamp = mAtomicStreamTimestamp.read();
192 *positionFrames = timestamp.getPosition();
193 *timeNanos = timestamp.getNanoseconds() + serviceEndpointMMAP->getHardwareTimeOffsetNanos();
194 return AAUDIO_OK;
195 } else {
196 return AAUDIO_ERROR_UNAVAILABLE;
197 }
198 }
199
200 // Get an immutable description of the data queue from the HAL.
getAudioDataDescription(AudioEndpointParcelable & parcelable)201 aaudio_result_t AAudioServiceStreamMMAP::getAudioDataDescription(
202 AudioEndpointParcelable &parcelable)
203 {
204 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
205 if (endpoint == nullptr) {
206 ALOGE("%s() has no endpoint", __func__);
207 return AAUDIO_ERROR_INVALID_STATE;
208 }
209 sp<AAudioServiceEndpointMMAP> serviceEndpointMMAP =
210 static_cast<AAudioServiceEndpointMMAP *>(endpoint.get());
211 return serviceEndpointMMAP->getDownDataDescription(parcelable);
212 }
213