blob: 78a15839372a0b6f7d66bfb5ad07bf1b66bf27c9 [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
17#define LOG_TAG "AAudioService"
18//#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/**
41 * Stream that uses an MMAP buffer.
42 */
43
44AAudioServiceStreamMMAP::AAudioServiceStreamMMAP()
45 : AAudioServiceStreamBase()
46 , mMmapStreamCallback(new MyMmapStreamCallback(*this))
47 , mPreviousFrameCounter(0)
48 , mMmapStream(nullptr) {
49}
50
51AAudioServiceStreamMMAP::~AAudioServiceStreamMMAP() {
52 close();
53}
54
55aaudio_result_t AAudioServiceStreamMMAP::close() {
56 ALOGD("AAudioServiceStreamMMAP::close() called, %p", mMmapStream.get());
57 mMmapStream.clear(); // TODO review. Is that all we have to do?
Phil Burk71f35bb2017-04-13 16:05:07 -070058 // Apparently the above close is asynchronous. An attempt to open a new device
59 // right after a close can fail. Also some callbacks may still be in flight!
60 // FIXME Make closing synchronous.
61 AudioClock::sleepForNanos(100 * AAUDIO_NANOS_PER_MILLISECOND);
62
Phil Burk942bdc02017-05-03 11:36:50 -070063 if (mAudioDataFileDescriptor != -1) {
64 ALOGV("AAudioServiceStreamMMAP: LEAK? close(mAudioDataFileDescriptor = %d)\n",
65 mAudioDataFileDescriptor);
66 ::close(mAudioDataFileDescriptor);
67 mAudioDataFileDescriptor = -1;
68 }
69
Phil Burkc0c70e32017-02-09 13:18:38 -080070 return AAudioServiceStreamBase::close();
71}
72
73// Open stream on HAL and pass information about the shared memory buffer back to the client.
74aaudio_result_t AAudioServiceStreamMMAP::open(const aaudio::AAudioStreamRequest &request,
75 aaudio::AAudioStreamConfiguration &configurationOutput) {
76 const audio_attributes_t attributes = {
77 .content_type = AUDIO_CONTENT_TYPE_MUSIC,
78 .usage = AUDIO_USAGE_MEDIA,
79 .source = AUDIO_SOURCE_DEFAULT,
80 .flags = AUDIO_FLAG_LOW_LATENCY,
81 .tags = ""
82 };
83 audio_config_base_t config;
84
85 aaudio_result_t result = AAudioServiceStreamBase::open(request, configurationOutput);
86 if (result != AAUDIO_OK) {
87 ALOGE("AAudioServiceStreamBase open returned %d", result);
88 return result;
89 }
90
91 const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
92 audio_port_handle_t deviceId = configurationInput.getDeviceId();
93
Phil Burk71f35bb2017-04-13 16:05:07 -070094 // ALOGI("open request dump()");
95 // request.dump();
Phil Burkc0c70e32017-02-09 13:18:38 -080096
97 mMmapClient.clientUid = request.getUserId();
98 mMmapClient.clientPid = request.getProcessId();
99 aaudio_direction_t direction = request.getDirection();
100
101 // Fill in config
102 aaudio_audio_format_t aaudioFormat = configurationInput.getAudioFormat();
103 if (aaudioFormat == AAUDIO_UNSPECIFIED || aaudioFormat == AAUDIO_FORMAT_PCM_FLOAT) {
104 ALOGI("open forcing use of AAUDIO_FORMAT_PCM_I16");
105 aaudioFormat = AAUDIO_FORMAT_PCM_I16;
106 }
107 config.format = AAudioConvert_aaudioToAndroidDataFormat(aaudioFormat);
108
109 int32_t aaudioSampleRate = configurationInput.getSampleRate();
110 if (aaudioSampleRate == AAUDIO_UNSPECIFIED) {
111 aaudioSampleRate = AAUDIO_SAMPLE_RATE_DEFAULT;
112 }
113 config.sample_rate = aaudioSampleRate;
114
115 int32_t aaudioSamplesPerFrame = configurationInput.getSamplesPerFrame();
116
117 if (direction == AAUDIO_DIRECTION_OUTPUT) {
118 config.channel_mask = (aaudioSamplesPerFrame == AAUDIO_UNSPECIFIED)
119 ? AUDIO_CHANNEL_OUT_STEREO
120 : audio_channel_out_mask_from_count(aaudioSamplesPerFrame);
121 } else if (direction == AAUDIO_DIRECTION_INPUT) {
122 config.channel_mask = (aaudioSamplesPerFrame == AAUDIO_UNSPECIFIED)
123 ? AUDIO_CHANNEL_IN_STEREO
124 : audio_channel_in_mask_from_count(aaudioSamplesPerFrame);
125 } else {
126 ALOGE("openMmapStream - invalid direction = %d", direction);
127 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
128 }
129
130 mMmapClient.packageName.setTo(String16("aaudio_service")); // FIXME what should we do here?
131
132 MmapStreamInterface::stream_direction_t streamDirection = (direction == AAUDIO_DIRECTION_OUTPUT)
133 ? MmapStreamInterface::DIRECTION_OUTPUT : MmapStreamInterface::DIRECTION_INPUT;
134
Phil Burk4c5129b2017-04-28 15:17:32 -0700135 ALOGD("AAudioServiceStreamMMAP::open() request devId = %d, sRate = %d",
136 deviceId, config.sample_rate);
137
Phil Burkc0c70e32017-02-09 13:18:38 -0800138 // Open HAL stream.
139 status_t status = MmapStreamInterface::openMmapStream(streamDirection,
140 &attributes,
141 &config,
142 mMmapClient,
143 &deviceId,
144 mMmapStreamCallback,
145 mMmapStream);
146 if (status != OK) {
147 ALOGE("openMmapStream returned status %d", status);
148 return AAUDIO_ERROR_UNAVAILABLE;
149 }
150
151 // Create MMAP/NOIRQ buffer.
152 int32_t minSizeFrames = configurationInput.getBufferCapacity();
153 if (minSizeFrames == 0) { // zero will get rejected
154 minSizeFrames = AAUDIO_BUFFER_CAPACITY_MIN;
155 }
156 status = mMmapStream->createMmapBuffer(minSizeFrames, &mMmapBufferinfo);
157 if (status != OK) {
158 ALOGE("%s: createMmapBuffer() returned status %d, return AAUDIO_ERROR_UNAVAILABLE",
159 __FILE__, status);
160 return AAUDIO_ERROR_UNAVAILABLE;
161 } else {
162 ALOGD("createMmapBuffer status %d shared_address = %p buffer_size %d burst_size %d",
163 status, mMmapBufferinfo.shared_memory_address,
164 mMmapBufferinfo.buffer_size_frames,
165 mMmapBufferinfo.burst_size_frames);
166 }
167
168 // Get information about the stream and pass it back to the caller.
169 mSamplesPerFrame = (direction == AAUDIO_DIRECTION_OUTPUT)
170 ? audio_channel_count_from_out_mask(config.channel_mask)
171 : audio_channel_count_from_in_mask(config.channel_mask);
172
173 mAudioDataFileDescriptor = mMmapBufferinfo.shared_memory_fd;
Phil Burk942bdc02017-05-03 11:36:50 -0700174 ALOGV("AAudioServiceStreamMMAP::open LEAK? mAudioDataFileDescriptor = %d\n",
175 mAudioDataFileDescriptor);
Phil Burkc0c70e32017-02-09 13:18:38 -0800176 mFramesPerBurst = mMmapBufferinfo.burst_size_frames;
177 mCapacityInFrames = mMmapBufferinfo.buffer_size_frames;
178 mAudioFormat = AAudioConvert_androidToAAudioDataFormat(config.format);
179 mSampleRate = config.sample_rate;
180
Phil Burkc8f69a02017-05-11 15:53:06 -0700181 // Scale up the burst size to meet the minimum equivalent in microseconds.
182 // This is to avoid waking the CPU too often when the HW burst is very small
183 // or at high sample rates.
184 int32_t burstMinMicros = AAudioProperty_getHardwareBurstMinMicros();
185 int32_t burstMicros = 0;
186 do {
187 if (burstMicros > 0) { // skip first loop
188 mFramesPerBurst *= 2;
189 }
190 burstMicros = mFramesPerBurst * static_cast<int64_t>(1000000) / mSampleRate;
191 } while (burstMicros < burstMinMicros);
192
193 ALOGD("AAudioServiceStreamMMAP::open() original burst = %d, minMicros = %d, final burst = %d\n",
194 mMmapBufferinfo.burst_size_frames, burstMinMicros, mFramesPerBurst);
195
Phil Burk4c5129b2017-04-28 15:17:32 -0700196 ALOGD("AAudioServiceStreamMMAP::open() got devId = %d, sRate = %d",
197 deviceId, config.sample_rate);
198
Phil Burkc0c70e32017-02-09 13:18:38 -0800199 // Fill in AAudioStreamConfiguration
200 configurationOutput.setSampleRate(mSampleRate);
201 configurationOutput.setSamplesPerFrame(mSamplesPerFrame);
202 configurationOutput.setAudioFormat(mAudioFormat);
203 configurationOutput.setDeviceId(deviceId);
204
205 return AAUDIO_OK;
206}
207
208
209/**
210 * Start the flow of data.
211 */
212aaudio_result_t AAudioServiceStreamMMAP::start() {
213 if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL;
214 aaudio_result_t result = mMmapStream->start(mMmapClient, &mPortHandle);
215 if (result != AAUDIO_OK) {
216 ALOGE("AAudioServiceStreamMMAP::start() mMmapStream->start() returned %d", result);
217 processError();
218 } else {
219 result = AAudioServiceStreamBase::start();
220 }
221 return result;
222}
223
224/**
225 * Stop the flow of data such that start() can resume with loss of data.
226 */
227aaudio_result_t AAudioServiceStreamMMAP::pause() {
228 if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL;
229
230 aaudio_result_t result1 = AAudioServiceStreamBase::pause();
231 aaudio_result_t result2 = mMmapStream->stop(mPortHandle);
232 mFramesRead.reset32();
233 return (result1 != AAUDIO_OK) ? result1 : result2;
234}
235
Phil Burk71f35bb2017-04-13 16:05:07 -0700236aaudio_result_t AAudioServiceStreamMMAP::stop() {
237 if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL;
238
239 aaudio_result_t result1 = AAudioServiceStreamBase::stop();
240 aaudio_result_t result2 = mMmapStream->stop(mPortHandle);
241 mFramesRead.reset32();
242 return (result1 != AAUDIO_OK) ? result1 : result2;
243}
244
Phil Burkc0c70e32017-02-09 13:18:38 -0800245/**
246 * Discard any data held by the underlying HAL or Service.
247 */
248aaudio_result_t AAudioServiceStreamMMAP::flush() {
249 if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL;
250 // TODO how do we flush an MMAP/NOIRQ buffer? sync pointers?
Phil Burk71f35bb2017-04-13 16:05:07 -0700251 ALOGD("AAudioServiceStreamMMAP::flush() send AAUDIO_SERVICE_EVENT_FLUSHED");
Phil Burkc0c70e32017-02-09 13:18:38 -0800252 sendServiceEvent(AAUDIO_SERVICE_EVENT_FLUSHED);
253 mState = AAUDIO_STREAM_STATE_FLUSHED;
Phil Burk71f35bb2017-04-13 16:05:07 -0700254 return AAudioServiceStreamBase::flush();;
Phil Burkc0c70e32017-02-09 13:18:38 -0800255}
256
257
258aaudio_result_t AAudioServiceStreamMMAP::getFreeRunningPosition(int64_t *positionFrames,
259 int64_t *timeNanos) {
260 struct audio_mmap_position position;
261 if (mMmapStream == nullptr) {
262 processError();
263 return AAUDIO_ERROR_NULL;
264 }
265 status_t status = mMmapStream->getMmapPosition(&position);
266 if (status != OK) {
267 ALOGE("sendCurrentTimestamp(): getMmapPosition() returned %d", status);
268 processError();
269 return AAudioConvert_androidToAAudioResult(status);
270 } else {
271 mFramesRead.update32(position.position_frames);
272 *positionFrames = mFramesRead.get();
273 *timeNanos = position.time_nanoseconds;
274 }
275 return AAUDIO_OK;
276}
277
278void AAudioServiceStreamMMAP::onTearDown() {
279 ALOGD("AAudioServiceStreamMMAP::onTearDown() called - TODO");
280};
281
282void AAudioServiceStreamMMAP::onVolumeChanged(audio_channel_mask_t channels,
283 android::Vector<float> values) {
284 // TODO do we really need a different volume for each channel?
285 float volume = values[0];
286 ALOGD("AAudioServiceStreamMMAP::onVolumeChanged() volume[0] = %f", volume);
287 sendServiceEvent(AAUDIO_SERVICE_EVENT_VOLUME, volume);
288};
289
290void AAudioServiceStreamMMAP::onRoutingChanged(audio_port_handle_t deviceId) {
291 ALOGD("AAudioServiceStreamMMAP::onRoutingChanged() called with %d, old = %d",
292 deviceId, mPortHandle);
293 if (mPortHandle > 0 && mPortHandle != deviceId) {
294 sendServiceEvent(AAUDIO_SERVICE_EVENT_DISCONNECTED);
295 }
296 mPortHandle = deviceId;
297};
298
299/**
300 * Get an immutable description of the data queue from the HAL.
301 */
302aaudio_result_t AAudioServiceStreamMMAP::getDownDataDescription(AudioEndpointParcelable &parcelable)
303{
304 // Gather information on the data queue based on HAL info.
305 int32_t bytesPerFrame = calculateBytesPerFrame();
306 int32_t capacityInBytes = mCapacityInFrames * bytesPerFrame;
307 int fdIndex = parcelable.addFileDescriptor(mAudioDataFileDescriptor, capacityInBytes);
308 parcelable.mDownDataQueueParcelable.setupMemory(fdIndex, 0, capacityInBytes);
309 parcelable.mDownDataQueueParcelable.setBytesPerFrame(bytesPerFrame);
310 parcelable.mDownDataQueueParcelable.setFramesPerBurst(mFramesPerBurst);
311 parcelable.mDownDataQueueParcelable.setCapacityInFrames(mCapacityInFrames);
312 return AAUDIO_OK;
313}