blob: 68be3c3d1d9cb843282d16fd9a41254cea858921 [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>
22#include <stdint.h>
23
24#include <utils/String16.h>
25#include <media/nbaio/AudioStreamOutSink.h>
26#include <media/MmapStreamInterface.h>
27
28#include "AAudioServiceStreamBase.h"
29#include "AAudioServiceStreamMMAP.h"
30#include "binding/AudioEndpointParcelable.h"
31#include "SharedMemoryProxy.h"
32#include "utility/AAudioUtilities.h"
33
34using namespace android;
35using namespace aaudio;
36
37#define AAUDIO_BUFFER_CAPACITY_MIN 4 * 512
38#define AAUDIO_SAMPLE_RATE_DEFAULT 48000
39
40/**
Phil Burk11e8d332017-05-24 09:59:02 -070041 * Service Stream that uses an MMAP buffer.
Phil Burkc0c70e32017-02-09 13:18:38 -080042 */
43
Eric Laurentcb4dae22017-07-01 19:39:32 -070044AAudioServiceStreamMMAP::AAudioServiceStreamMMAP(const android::AudioClient& serviceClient,
45 bool inService)
Phil Burkc0c70e32017-02-09 13:18:38 -080046 : AAudioServiceStreamBase()
47 , mMmapStreamCallback(new MyMmapStreamCallback(*this))
48 , mPreviousFrameCounter(0)
Eric Laurentd51329e2017-06-30 16:06:16 -070049 , mMmapStream(nullptr)
Eric Laurentcb4dae22017-07-01 19:39:32 -070050 , mServiceClient(serviceClient)
51 , mInService(inService) {
Phil Burkc0c70e32017-02-09 13:18:38 -080052}
53
Phil Burkc0c70e32017-02-09 13:18:38 -080054aaudio_result_t AAudioServiceStreamMMAP::close() {
Phil Burk98d6d922017-07-06 11:52:45 -070055 if (mState == AAUDIO_STREAM_STATE_CLOSED) {
56 return AAUDIO_OK;
57 }
Eric Laurentcb4dae22017-07-01 19:39:32 -070058 stop();
Phil Burk98d6d922017-07-06 11:52:45 -070059 if (mMmapStream != 0) {
60 mMmapStream.clear(); // TODO review. Is that all we have to do?
61 // Apparently the above close is asynchronous. An attempt to open a new device
62 // right after a close can fail. Also some callbacks may still be in flight!
63 // FIXME Make closing synchronous.
64 AudioClock::sleepForNanos(100 * AAUDIO_NANOS_PER_MILLISECOND);
65 }
Phil Burk71f35bb2017-04-13 16:05:07 -070066
Phil Burk942bdc02017-05-03 11:36:50 -070067 if (mAudioDataFileDescriptor != -1) {
Phil Burk942bdc02017-05-03 11:36:50 -070068 ::close(mAudioDataFileDescriptor);
69 mAudioDataFileDescriptor = -1;
70 }
71
Phil Burkc0c70e32017-02-09 13:18:38 -080072 return AAudioServiceStreamBase::close();
73}
74
75// Open stream on HAL and pass information about the shared memory buffer back to the client.
76aaudio_result_t AAudioServiceStreamMMAP::open(const aaudio::AAudioStreamRequest &request,
77 aaudio::AAudioStreamConfiguration &configurationOutput) {
78 const audio_attributes_t attributes = {
79 .content_type = AUDIO_CONTENT_TYPE_MUSIC,
80 .usage = AUDIO_USAGE_MEDIA,
Phil Burk87c9f642017-05-17 07:22:39 -070081 .source = AUDIO_SOURCE_VOICE_RECOGNITION,
Phil Burkc0c70e32017-02-09 13:18:38 -080082 .flags = AUDIO_FLAG_LOW_LATENCY,
83 .tags = ""
84 };
85 audio_config_base_t config;
86
87 aaudio_result_t result = AAudioServiceStreamBase::open(request, configurationOutput);
88 if (result != AAUDIO_OK) {
89 ALOGE("AAudioServiceStreamBase open returned %d", result);
90 return result;
91 }
92
93 const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
94 audio_port_handle_t deviceId = configurationInput.getDeviceId();
Phil Burkc0c70e32017-02-09 13:18:38 -080095 aaudio_direction_t direction = request.getDirection();
96
97 // Fill in config
Phil Burk9dca9822017-05-26 14:27:43 -070098 aaudio_format_t aaudioFormat = configurationInput.getAudioFormat();
Phil Burkc0c70e32017-02-09 13:18:38 -080099 if (aaudioFormat == AAUDIO_UNSPECIFIED || aaudioFormat == AAUDIO_FORMAT_PCM_FLOAT) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800100 aaudioFormat = AAUDIO_FORMAT_PCM_I16;
101 }
102 config.format = AAudioConvert_aaudioToAndroidDataFormat(aaudioFormat);
103
104 int32_t aaudioSampleRate = configurationInput.getSampleRate();
105 if (aaudioSampleRate == AAUDIO_UNSPECIFIED) {
106 aaudioSampleRate = AAUDIO_SAMPLE_RATE_DEFAULT;
107 }
108 config.sample_rate = aaudioSampleRate;
109
110 int32_t aaudioSamplesPerFrame = configurationInput.getSamplesPerFrame();
111
112 if (direction == AAUDIO_DIRECTION_OUTPUT) {
113 config.channel_mask = (aaudioSamplesPerFrame == AAUDIO_UNSPECIFIED)
114 ? AUDIO_CHANNEL_OUT_STEREO
115 : audio_channel_out_mask_from_count(aaudioSamplesPerFrame);
116 } else if (direction == AAUDIO_DIRECTION_INPUT) {
117 config.channel_mask = (aaudioSamplesPerFrame == AAUDIO_UNSPECIFIED)
118 ? AUDIO_CHANNEL_IN_STEREO
119 : audio_channel_in_mask_from_count(aaudioSamplesPerFrame);
120 } else {
121 ALOGE("openMmapStream - invalid direction = %d", direction);
122 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
123 }
124
Phil Burkc0c70e32017-02-09 13:18:38 -0800125 MmapStreamInterface::stream_direction_t streamDirection = (direction == AAUDIO_DIRECTION_OUTPUT)
126 ? MmapStreamInterface::DIRECTION_OUTPUT : MmapStreamInterface::DIRECTION_INPUT;
127
128 // Open HAL stream.
129 status_t status = MmapStreamInterface::openMmapStream(streamDirection,
130 &attributes,
131 &config,
132 mMmapClient,
133 &deviceId,
134 mMmapStreamCallback,
Eric Laurentcb4dae22017-07-01 19:39:32 -0700135 mMmapStream,
136 &mPortHandle);
Phil Burkc0c70e32017-02-09 13:18:38 -0800137 if (status != OK) {
138 ALOGE("openMmapStream returned status %d", status);
139 return AAUDIO_ERROR_UNAVAILABLE;
140 }
141
Phil Burkec89b2e2017-06-20 15:05:06 -0700142 if (deviceId == AAUDIO_UNSPECIFIED) {
143 ALOGW("AAudioServiceStreamMMAP::open() - openMmapStream() failed to set deviceId");
144 }
145
Phil Burkc0c70e32017-02-09 13:18:38 -0800146 // Create MMAP/NOIRQ buffer.
147 int32_t minSizeFrames = configurationInput.getBufferCapacity();
Phil Burkec89b2e2017-06-20 15:05:06 -0700148 if (minSizeFrames <= 0) { // zero will get rejected
Phil Burkc0c70e32017-02-09 13:18:38 -0800149 minSizeFrames = AAUDIO_BUFFER_CAPACITY_MIN;
150 }
151 status = mMmapStream->createMmapBuffer(minSizeFrames, &mMmapBufferinfo);
152 if (status != OK) {
Phil Burkec89b2e2017-06-20 15:05:06 -0700153 ALOGE("AAudioServiceStreamMMAP::open() - createMmapBuffer() returned status %d",
154 status);
Phil Burkc0c70e32017-02-09 13:18:38 -0800155 return AAUDIO_ERROR_UNAVAILABLE;
156 } else {
Eric Laurentd51329e2017-06-30 16:06:16 -0700157 ALOGD("createMmapBuffer status %d shared_address = %p buffer_size %d burst_size %d"
158 "Sharable FD: %s",
Phil Burkc0c70e32017-02-09 13:18:38 -0800159 status, mMmapBufferinfo.shared_memory_address,
Eric Laurentd51329e2017-06-30 16:06:16 -0700160 abs(mMmapBufferinfo.buffer_size_frames),
161 mMmapBufferinfo.burst_size_frames,
162 mMmapBufferinfo.buffer_size_frames < 0 ? "Yes" : "No");
163 }
164
165 mCapacityInFrames = mMmapBufferinfo.buffer_size_frames;
166 // FIXME: the audio HAL indicates if the shared memory fd can be shared outside of audioserver
167 // by returning a negative buffer size
168 if (mCapacityInFrames < 0) {
169 // Exclusive mode is possible from any client
170 mCapacityInFrames = -mCapacityInFrames;
171 } else {
172 // exclusive mode is only possible if the final fd destination is inside audioserver
Eric Laurentcb4dae22017-07-01 19:39:32 -0700173 if ((mMmapClient.clientUid != mServiceClient.clientUid) &&
Eric Laurentd51329e2017-06-30 16:06:16 -0700174 configurationInput.getSharingMode() == AAUDIO_SHARING_MODE_EXCLUSIVE) {
175 // Fallback is handled by caller but indicate what is possible in case
176 // this is used in the future
177 configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_SHARED);
178 return AAUDIO_ERROR_UNAVAILABLE;
179 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800180 }
181
182 // Get information about the stream and pass it back to the caller.
183 mSamplesPerFrame = (direction == AAUDIO_DIRECTION_OUTPUT)
184 ? audio_channel_count_from_out_mask(config.channel_mask)
185 : audio_channel_count_from_in_mask(config.channel_mask);
186
187 mAudioDataFileDescriptor = mMmapBufferinfo.shared_memory_fd;
188 mFramesPerBurst = mMmapBufferinfo.burst_size_frames;
Phil Burkc0c70e32017-02-09 13:18:38 -0800189 mAudioFormat = AAudioConvert_androidToAAudioDataFormat(config.format);
190 mSampleRate = config.sample_rate;
191
Phil Burkc8f69a02017-05-11 15:53:06 -0700192 // Scale up the burst size to meet the minimum equivalent in microseconds.
193 // This is to avoid waking the CPU too often when the HW burst is very small
194 // or at high sample rates.
195 int32_t burstMinMicros = AAudioProperty_getHardwareBurstMinMicros();
196 int32_t burstMicros = 0;
197 do {
198 if (burstMicros > 0) { // skip first loop
199 mFramesPerBurst *= 2;
200 }
201 burstMicros = mFramesPerBurst * static_cast<int64_t>(1000000) / mSampleRate;
202 } while (burstMicros < burstMinMicros);
203
204 ALOGD("AAudioServiceStreamMMAP::open() original burst = %d, minMicros = %d, final burst = %d\n",
205 mMmapBufferinfo.burst_size_frames, burstMinMicros, mFramesPerBurst);
206
Phil Burkec89b2e2017-06-20 15:05:06 -0700207 ALOGD("AAudioServiceStreamMMAP::open() actual rate = %d, channels = %d, deviceId = %d\n",
208 mSampleRate, mSamplesPerFrame, deviceId);
209
Phil Burkc0c70e32017-02-09 13:18:38 -0800210 // Fill in AAudioStreamConfiguration
211 configurationOutput.setSampleRate(mSampleRate);
212 configurationOutput.setSamplesPerFrame(mSamplesPerFrame);
213 configurationOutput.setAudioFormat(mAudioFormat);
214 configurationOutput.setDeviceId(deviceId);
215
Phil Burk5a26e662017-07-07 12:44:48 -0700216 setState(AAUDIO_STREAM_STATE_OPEN);
Phil Burkc0c70e32017-02-09 13:18:38 -0800217 return AAUDIO_OK;
218}
219
Phil Burkc0c70e32017-02-09 13:18:38 -0800220/**
221 * Start the flow of data.
222 */
223aaudio_result_t AAudioServiceStreamMMAP::start() {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700224 if (isRunning()) {
225 return AAUDIO_OK;
226 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800227 if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL;
Phil Burk87c9f642017-05-17 07:22:39 -0700228 aaudio_result_t result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700229 status_t status = mMmapStream->start(mServiceClient, &mPortHandle);
Phil Burk87c9f642017-05-17 07:22:39 -0700230 if (status != OK) {
231 ALOGE("AAudioServiceStreamMMAP::start() mMmapStream->start() returned %d", status);
Phil Burk5ef003b2017-06-30 11:43:37 -0700232 disconnect();
Phil Burk87c9f642017-05-17 07:22:39 -0700233 result = AAudioConvert_androidToAAudioResult(status);
Phil Burkc0c70e32017-02-09 13:18:38 -0800234 } else {
235 result = AAudioServiceStreamBase::start();
Eric Laurentcb4dae22017-07-01 19:39:32 -0700236 if (!mInService && result == AAUDIO_OK) {
237 startClient(mMmapClient, &mClientHandle);
238 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800239 }
240 return result;
241}
242
243/**
244 * Stop the flow of data such that start() can resume with loss of data.
245 */
246aaudio_result_t AAudioServiceStreamMMAP::pause() {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700247 if (!isRunning()) {
248 return AAUDIO_OK;
249 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800250 if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL;
Phil Burkc0c70e32017-02-09 13:18:38 -0800251 aaudio_result_t result1 = AAudioServiceStreamBase::pause();
Eric Laurentcb4dae22017-07-01 19:39:32 -0700252 if (!mInService) {
253 stopClient(mClientHandle);
254 }
Phil Burk87c9f642017-05-17 07:22:39 -0700255 status_t status = mMmapStream->stop(mPortHandle);
Phil Burkc0c70e32017-02-09 13:18:38 -0800256 mFramesRead.reset32();
Phil Burk87c9f642017-05-17 07:22:39 -0700257 return (result1 != AAUDIO_OK) ? result1 : AAudioConvert_androidToAAudioResult(status);
Phil Burkc0c70e32017-02-09 13:18:38 -0800258}
259
Phil Burk71f35bb2017-04-13 16:05:07 -0700260aaudio_result_t AAudioServiceStreamMMAP::stop() {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700261 if (!isRunning()) {
262 return AAUDIO_OK;
263 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700264 if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL;
Phil Burk71f35bb2017-04-13 16:05:07 -0700265 aaudio_result_t result1 = AAudioServiceStreamBase::stop();
Eric Laurentcb4dae22017-07-01 19:39:32 -0700266 if (!mInService) {
267 stopClient(mClientHandle);
268 }
Phil Burk87c9f642017-05-17 07:22:39 -0700269 aaudio_result_t status = mMmapStream->stop(mPortHandle);
Phil Burk71f35bb2017-04-13 16:05:07 -0700270 mFramesRead.reset32();
Phil Burk87c9f642017-05-17 07:22:39 -0700271 return (result1 != AAUDIO_OK) ? result1 : AAudioConvert_androidToAAudioResult(status);
Phil Burk71f35bb2017-04-13 16:05:07 -0700272}
273
Phil Burkc0c70e32017-02-09 13:18:38 -0800274/**
275 * Discard any data held by the underlying HAL or Service.
276 */
277aaudio_result_t AAudioServiceStreamMMAP::flush() {
278 if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL;
279 // TODO how do we flush an MMAP/NOIRQ buffer? sync pointers?
Phil Burk71f35bb2017-04-13 16:05:07 -0700280 return AAudioServiceStreamBase::flush();;
Phil Burkc0c70e32017-02-09 13:18:38 -0800281}
282
Eric Laurentcb4dae22017-07-01 19:39:32 -0700283aaudio_result_t AAudioServiceStreamMMAP::startClient(const android::AudioClient& client,
284 audio_port_handle_t *clientHandle) {
285 return AAudioConvert_androidToAAudioResult(mMmapStream->start(client, clientHandle));
286}
287
288aaudio_result_t AAudioServiceStreamMMAP::stopClient(audio_port_handle_t clientHandle) {
289 return AAudioConvert_androidToAAudioResult(mMmapStream->stop(clientHandle));
290}
291
Phil Burkc0c70e32017-02-09 13:18:38 -0800292aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames,
293 int64_t *timeNanos) {
294 struct audio_mmap_position position;
295 if (mMmapStream == nullptr) {
Phil Burk5ef003b2017-06-30 11:43:37 -0700296 disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800297 return AAUDIO_ERROR_NULL;
298 }
299 status_t status = mMmapStream->getMmapPosition(&position);
300 if (status != OK) {
301 ALOGE("sendCurrentTimestamp(): getMmapPosition() returned %d", status);
Phil Burk5ef003b2017-06-30 11:43:37 -0700302 disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800303 return AAudioConvert_androidToAAudioResult(status);
304 } else {
305 mFramesRead.update32(position.position_frames);
306 *positionFrames = mFramesRead.get();
307 *timeNanos = position.time_nanoseconds;
308 }
309 return AAUDIO_OK;
310}
311
312void AAudioServiceStreamMMAP::onTearDown() {
Phil Burk5ef003b2017-06-30 11:43:37 -0700313 ALOGD("AAudioServiceStreamMMAP::onTearDown() called");
314 disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800315};
316
317void AAudioServiceStreamMMAP::onVolumeChanged(audio_channel_mask_t channels,
318 android::Vector<float> values) {
319 // TODO do we really need a different volume for each channel?
320 float volume = values[0];
321 ALOGD("AAudioServiceStreamMMAP::onVolumeChanged() volume[0] = %f", volume);
322 sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume);
323};
324
325void AAudioServiceStreamMMAP::onRoutingChanged(audio_port_handle_t deviceId) {
326 ALOGD("AAudioServiceStreamMMAP::onRoutingChanged() called with %d, old = %d",
Eric Laurentcb4dae22017-07-01 19:39:32 -0700327 deviceId, mDeviceId);
328 if (mDeviceId != AUDIO_PORT_HANDLE_NONE && mDeviceId != deviceId) {
Phil Burk5ef003b2017-06-30 11:43:37 -0700329 disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800330 }
Eric Laurentcb4dae22017-07-01 19:39:32 -0700331 mDeviceId = deviceId;
Phil Burkc0c70e32017-02-09 13:18:38 -0800332};
333
334/**
335 * Get an immutable description of the data queue from the HAL.
336 */
337aaudio_result_t AAudioServiceStreamMMAP::getDownDataDescription(AudioEndpointParcelable &parcelable)
338{
339 // Gather information on the data queue based on HAL info.
340 int32_t bytesPerFrame = calculateBytesPerFrame();
341 int32_t capacityInBytes = mCapacityInFrames * bytesPerFrame;
342 int fdIndex = parcelable.addFileDescriptor(mAudioDataFileDescriptor, capacityInBytes);
343 parcelable.mDownDataQueueParcelable.setupMemory(fdIndex, 0, capacityInBytes);
344 parcelable.mDownDataQueueParcelable.setBytesPerFrame(bytesPerFrame);
345 parcelable.mDownDataQueueParcelable.setFramesPerBurst(mFramesPerBurst);
346 parcelable.mDownDataQueueParcelable.setCapacityInFrames(mCapacityInFrames);
347 return AAUDIO_OK;
Phil Burkec89b2e2017-06-20 15:05:06 -0700348}