Marco Nelissen | 10c9b0f | 2019-10-23 09:21:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2010, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | #define LOG_TAG "Visualizer" |
| 21 | #include <utils/Log.h> |
| 22 | |
| 23 | #include <stdint.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <limits.h> |
| 26 | |
| 27 | #include <audio_utils/fixedfft.h> |
| 28 | #include <utils/Thread.h> |
| 29 | |
| 30 | #include "Visualizer.h" |
| 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | // --------------------------------------------------------------------------- |
| 35 | |
| 36 | Visualizer::Visualizer (const String16& opPackageName, |
| 37 | int32_t priority, |
| 38 | effect_callback_t cbf, |
| 39 | void* user, |
| 40 | audio_session_t sessionId) |
| 41 | : AudioEffect(SL_IID_VISUALIZATION, opPackageName, NULL, priority, cbf, user, sessionId), |
| 42 | mCaptureRate(CAPTURE_RATE_DEF), |
| 43 | mCaptureSize(CAPTURE_SIZE_DEF), |
| 44 | mSampleRate(44100000), |
| 45 | mScalingMode(VISUALIZER_SCALING_MODE_NORMALIZED), |
| 46 | mMeasurementMode(MEASUREMENT_MODE_NONE), |
| 47 | mCaptureCallBack(NULL), |
| 48 | mCaptureCbkUser(NULL) |
| 49 | { |
| 50 | initCaptureSize(); |
| 51 | } |
| 52 | |
| 53 | Visualizer::~Visualizer() |
| 54 | { |
| 55 | ALOGV("Visualizer::~Visualizer()"); |
| 56 | setEnabled(false); |
| 57 | setCaptureCallBack(NULL, NULL, 0, 0); |
| 58 | } |
| 59 | |
| 60 | void Visualizer::release() |
| 61 | { |
| 62 | ALOGV("Visualizer::release()"); |
| 63 | setEnabled(false); |
| 64 | Mutex::Autolock _l(mCaptureLock); |
| 65 | |
| 66 | mCaptureThread.clear(); |
| 67 | mCaptureCallBack = NULL; |
| 68 | mCaptureCbkUser = NULL; |
| 69 | mCaptureFlags = 0; |
| 70 | mCaptureRate = 0; |
| 71 | } |
| 72 | |
| 73 | status_t Visualizer::setEnabled(bool enabled) |
| 74 | { |
| 75 | Mutex::Autolock _l(mCaptureLock); |
| 76 | |
| 77 | sp<CaptureThread> t = mCaptureThread; |
| 78 | if (t != 0) { |
| 79 | if (enabled) { |
| 80 | if (t->exitPending()) { |
Aniket Kumar Lata | b2f11e6 | 2019-07-03 10:44:47 -0700 | [diff] [blame] | 81 | mCaptureLock.unlock(); |
Marco Nelissen | 10c9b0f | 2019-10-23 09:21:55 -0700 | [diff] [blame] | 82 | if (t->requestExitAndWait() == WOULD_BLOCK) { |
Aniket Kumar Lata | b2f11e6 | 2019-07-03 10:44:47 -0700 | [diff] [blame] | 83 | mCaptureLock.lock(); |
Marco Nelissen | 10c9b0f | 2019-10-23 09:21:55 -0700 | [diff] [blame] | 84 | ALOGE("Visualizer::enable() called from thread"); |
| 85 | return INVALID_OPERATION; |
| 86 | } |
Aniket Kumar Lata | b2f11e6 | 2019-07-03 10:44:47 -0700 | [diff] [blame] | 87 | mCaptureLock.lock(); |
Marco Nelissen | 10c9b0f | 2019-10-23 09:21:55 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | t->mLock.lock(); |
| 91 | } |
| 92 | |
| 93 | status_t status = AudioEffect::setEnabled(enabled); |
| 94 | |
| 95 | if (t != 0) { |
| 96 | if (enabled && status == NO_ERROR) { |
| 97 | t->run("Visualizer"); |
| 98 | } else { |
| 99 | t->requestExit(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if (t != 0) { |
| 104 | t->mLock.unlock(); |
| 105 | } |
| 106 | |
| 107 | return status; |
| 108 | } |
| 109 | |
| 110 | status_t Visualizer::setCaptureCallBack(capture_cbk_t cbk, void* user, uint32_t flags, |
| 111 | uint32_t rate) |
| 112 | { |
| 113 | if (rate > CAPTURE_RATE_MAX) { |
| 114 | return BAD_VALUE; |
| 115 | } |
| 116 | Mutex::Autolock _l(mCaptureLock); |
| 117 | |
| 118 | if (mEnabled) { |
| 119 | return INVALID_OPERATION; |
| 120 | } |
| 121 | |
| 122 | if (mCaptureThread != 0) { |
Mikhail Naganov | fcfa734 | 2020-03-12 17:55:29 -0700 | [diff] [blame^] | 123 | sp<CaptureThread> t = mCaptureThread; |
Marco Nelissen | 10c9b0f | 2019-10-23 09:21:55 -0700 | [diff] [blame] | 124 | mCaptureLock.unlock(); |
Mikhail Naganov | fcfa734 | 2020-03-12 17:55:29 -0700 | [diff] [blame^] | 125 | t->requestExitAndWait(); |
Marco Nelissen | 10c9b0f | 2019-10-23 09:21:55 -0700 | [diff] [blame] | 126 | mCaptureLock.lock(); |
| 127 | } |
| 128 | |
| 129 | mCaptureThread.clear(); |
| 130 | mCaptureCallBack = cbk; |
| 131 | mCaptureCbkUser = user; |
| 132 | mCaptureFlags = flags; |
| 133 | mCaptureRate = rate; |
| 134 | |
| 135 | if (cbk != NULL) { |
| 136 | mCaptureThread = new CaptureThread(this, rate, ((flags & CAPTURE_CALL_JAVA) != 0)); |
| 137 | } |
| 138 | ALOGV("setCaptureCallBack() rate: %d thread %p flags 0x%08x", |
| 139 | rate, mCaptureThread.get(), mCaptureFlags); |
| 140 | return NO_ERROR; |
| 141 | } |
| 142 | |
| 143 | status_t Visualizer::setCaptureSize(uint32_t size) |
| 144 | { |
| 145 | if (size > VISUALIZER_CAPTURE_SIZE_MAX || |
| 146 | size < VISUALIZER_CAPTURE_SIZE_MIN || |
| 147 | popcount(size) != 1) { |
| 148 | return BAD_VALUE; |
| 149 | } |
| 150 | |
| 151 | Mutex::Autolock _l(mCaptureLock); |
| 152 | if (mEnabled) { |
| 153 | return INVALID_OPERATION; |
| 154 | } |
| 155 | |
| 156 | uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2]; |
| 157 | effect_param_t *p = (effect_param_t *)buf32; |
| 158 | |
| 159 | p->psize = sizeof(uint32_t); |
| 160 | p->vsize = sizeof(uint32_t); |
| 161 | *(int32_t *)p->data = VISUALIZER_PARAM_CAPTURE_SIZE; |
| 162 | *((int32_t *)p->data + 1)= size; |
| 163 | status_t status = setParameter(p); |
| 164 | |
| 165 | ALOGV("setCaptureSize size %d status %d p->status %d", size, status, p->status); |
| 166 | |
| 167 | if (status == NO_ERROR) { |
| 168 | status = p->status; |
| 169 | if (status == NO_ERROR) { |
| 170 | mCaptureSize = size; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return status; |
| 175 | } |
| 176 | |
| 177 | status_t Visualizer::setScalingMode(uint32_t mode) { |
| 178 | if ((mode != VISUALIZER_SCALING_MODE_NORMALIZED) |
| 179 | && (mode != VISUALIZER_SCALING_MODE_AS_PLAYED)) { |
| 180 | return BAD_VALUE; |
| 181 | } |
| 182 | |
| 183 | Mutex::Autolock _l(mCaptureLock); |
| 184 | |
| 185 | uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2]; |
| 186 | effect_param_t *p = (effect_param_t *)buf32; |
| 187 | |
| 188 | p->psize = sizeof(uint32_t); |
| 189 | p->vsize = sizeof(uint32_t); |
| 190 | *(int32_t *)p->data = VISUALIZER_PARAM_SCALING_MODE; |
| 191 | *((int32_t *)p->data + 1)= mode; |
| 192 | status_t status = setParameter(p); |
| 193 | |
| 194 | ALOGV("setScalingMode mode %d status %d p->status %d", mode, status, p->status); |
| 195 | |
| 196 | if (status == NO_ERROR) { |
| 197 | status = p->status; |
| 198 | if (status == NO_ERROR) { |
| 199 | mScalingMode = mode; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | return status; |
| 204 | } |
| 205 | |
| 206 | status_t Visualizer::setMeasurementMode(uint32_t mode) { |
| 207 | if ((mode != MEASUREMENT_MODE_NONE) |
| 208 | //Note: needs to be handled as a mask when more measurement modes are added |
| 209 | && ((mode & MEASUREMENT_MODE_PEAK_RMS) != mode)) { |
| 210 | return BAD_VALUE; |
| 211 | } |
| 212 | |
| 213 | Mutex::Autolock _l(mCaptureLock); |
| 214 | |
| 215 | uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2]; |
| 216 | effect_param_t *p = (effect_param_t *)buf32; |
| 217 | |
| 218 | p->psize = sizeof(uint32_t); |
| 219 | p->vsize = sizeof(uint32_t); |
| 220 | *(int32_t *)p->data = VISUALIZER_PARAM_MEASUREMENT_MODE; |
| 221 | *((int32_t *)p->data + 1)= mode; |
| 222 | status_t status = setParameter(p); |
| 223 | |
| 224 | ALOGV("setMeasurementMode mode %d status %d p->status %d", mode, status, p->status); |
| 225 | |
| 226 | if (status == NO_ERROR) { |
| 227 | status = p->status; |
| 228 | if (status == NO_ERROR) { |
| 229 | mMeasurementMode = mode; |
| 230 | } |
| 231 | } |
| 232 | return status; |
| 233 | } |
| 234 | |
| 235 | status_t Visualizer::getIntMeasurements(uint32_t type, uint32_t number, int32_t *measurements) { |
| 236 | if (mMeasurementMode == MEASUREMENT_MODE_NONE) { |
| 237 | ALOGE("Cannot retrieve int measurements, no measurement mode set"); |
| 238 | return INVALID_OPERATION; |
| 239 | } |
| 240 | if (!(mMeasurementMode & type)) { |
| 241 | // measurement type has not been set on this Visualizer |
| 242 | ALOGE("Cannot retrieve int measurements, requested measurement mode 0x%x not set(0x%x)", |
| 243 | type, mMeasurementMode); |
| 244 | return INVALID_OPERATION; |
| 245 | } |
| 246 | // only peak+RMS measurement supported |
| 247 | if ((type != MEASUREMENT_MODE_PEAK_RMS) |
| 248 | // for peak+RMS measurement, the results are 2 int32_t values |
| 249 | || (number != 2)) { |
| 250 | ALOGE("Cannot retrieve int measurements, MEASUREMENT_MODE_PEAK_RMS returns 2 ints, not %d", |
| 251 | number); |
| 252 | return BAD_VALUE; |
| 253 | } |
| 254 | |
| 255 | status_t status = NO_ERROR; |
| 256 | if (mEnabled) { |
| 257 | uint32_t replySize = number * sizeof(int32_t); |
| 258 | status = command(VISUALIZER_CMD_MEASURE, |
| 259 | sizeof(uint32_t) /*cmdSize*/, |
| 260 | &type /*cmdData*/, |
| 261 | &replySize, measurements); |
| 262 | ALOGV("getMeasurements() command returned %d", status); |
| 263 | if ((status == NO_ERROR) && (replySize == 0)) { |
| 264 | status = NOT_ENOUGH_DATA; |
| 265 | } |
| 266 | } else { |
| 267 | ALOGV("getMeasurements() disabled"); |
| 268 | return INVALID_OPERATION; |
| 269 | } |
| 270 | return status; |
| 271 | } |
| 272 | |
| 273 | status_t Visualizer::getWaveForm(uint8_t *waveform) |
| 274 | { |
| 275 | if (waveform == NULL) { |
| 276 | return BAD_VALUE; |
| 277 | } |
| 278 | if (mCaptureSize == 0) { |
| 279 | return NO_INIT; |
| 280 | } |
| 281 | |
| 282 | status_t status = NO_ERROR; |
| 283 | if (mEnabled) { |
| 284 | uint32_t replySize = mCaptureSize; |
| 285 | status = command(VISUALIZER_CMD_CAPTURE, 0, NULL, &replySize, waveform); |
| 286 | ALOGV("getWaveForm() command returned %d", status); |
| 287 | if ((status == NO_ERROR) && (replySize == 0)) { |
| 288 | status = NOT_ENOUGH_DATA; |
| 289 | } |
| 290 | } else { |
| 291 | ALOGV("getWaveForm() disabled"); |
| 292 | memset(waveform, 0x80, mCaptureSize); |
| 293 | } |
| 294 | return status; |
| 295 | } |
| 296 | |
| 297 | status_t Visualizer::getFft(uint8_t *fft) |
| 298 | { |
| 299 | if (fft == NULL) { |
| 300 | return BAD_VALUE; |
| 301 | } |
| 302 | if (mCaptureSize == 0) { |
| 303 | return NO_INIT; |
| 304 | } |
| 305 | |
| 306 | status_t status = NO_ERROR; |
| 307 | if (mEnabled) { |
| 308 | uint8_t buf[mCaptureSize]; |
| 309 | status = getWaveForm(buf); |
| 310 | if (status == NO_ERROR) { |
| 311 | status = doFft(fft, buf); |
| 312 | } |
| 313 | } else { |
| 314 | memset(fft, 0, mCaptureSize); |
| 315 | } |
| 316 | return status; |
| 317 | } |
| 318 | |
| 319 | status_t Visualizer::doFft(uint8_t *fft, uint8_t *waveform) |
| 320 | { |
| 321 | int32_t workspace[mCaptureSize >> 1]; |
| 322 | int32_t nonzero = 0; |
| 323 | |
| 324 | for (uint32_t i = 0; i < mCaptureSize; i += 2) { |
| 325 | workspace[i >> 1] = |
| 326 | ((waveform[i] ^ 0x80) << 24) | ((waveform[i + 1] ^ 0x80) << 8); |
| 327 | nonzero |= workspace[i >> 1]; |
| 328 | } |
| 329 | |
| 330 | if (nonzero) { |
| 331 | fixed_fft_real(mCaptureSize >> 1, workspace); |
| 332 | } |
| 333 | |
| 334 | for (uint32_t i = 0; i < mCaptureSize; i += 2) { |
| 335 | short tmp = workspace[i >> 1] >> 21; |
| 336 | while (tmp > 127 || tmp < -128) tmp >>= 1; |
| 337 | fft[i] = tmp; |
| 338 | tmp = workspace[i >> 1]; |
| 339 | tmp >>= 5; |
| 340 | while (tmp > 127 || tmp < -128) tmp >>= 1; |
| 341 | fft[i + 1] = tmp; |
| 342 | } |
| 343 | |
| 344 | return NO_ERROR; |
| 345 | } |
| 346 | |
| 347 | void Visualizer::periodicCapture() |
| 348 | { |
| 349 | Mutex::Autolock _l(mCaptureLock); |
| 350 | ALOGV("periodicCapture() %p mCaptureCallBack %p mCaptureFlags 0x%08x", |
| 351 | this, mCaptureCallBack, mCaptureFlags); |
| 352 | if (mCaptureCallBack != NULL && |
| 353 | (mCaptureFlags & (CAPTURE_WAVEFORM|CAPTURE_FFT)) && |
| 354 | mCaptureSize != 0) { |
| 355 | uint8_t waveform[mCaptureSize]; |
| 356 | status_t status = getWaveForm(waveform); |
| 357 | if (status != NO_ERROR) { |
| 358 | return; |
| 359 | } |
| 360 | uint8_t fft[mCaptureSize]; |
| 361 | if (mCaptureFlags & CAPTURE_FFT) { |
| 362 | status = doFft(fft, waveform); |
| 363 | } |
| 364 | if (status != NO_ERROR) { |
| 365 | return; |
| 366 | } |
| 367 | uint8_t *wavePtr = NULL; |
| 368 | uint8_t *fftPtr = NULL; |
| 369 | uint32_t waveSize = 0; |
| 370 | uint32_t fftSize = 0; |
| 371 | if (mCaptureFlags & CAPTURE_WAVEFORM) { |
| 372 | wavePtr = waveform; |
| 373 | waveSize = mCaptureSize; |
| 374 | } |
| 375 | if (mCaptureFlags & CAPTURE_FFT) { |
| 376 | fftPtr = fft; |
| 377 | fftSize = mCaptureSize; |
| 378 | } |
| 379 | mCaptureCallBack(mCaptureCbkUser, waveSize, wavePtr, fftSize, fftPtr, mSampleRate); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | uint32_t Visualizer::initCaptureSize() |
| 384 | { |
| 385 | uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2]; |
| 386 | effect_param_t *p = (effect_param_t *)buf32; |
| 387 | |
| 388 | p->psize = sizeof(uint32_t); |
| 389 | p->vsize = sizeof(uint32_t); |
| 390 | *(int32_t *)p->data = VISUALIZER_PARAM_CAPTURE_SIZE; |
| 391 | status_t status = getParameter(p); |
| 392 | |
| 393 | if (status == NO_ERROR) { |
| 394 | status = p->status; |
| 395 | } |
| 396 | |
| 397 | uint32_t size = 0; |
| 398 | if (status == NO_ERROR) { |
| 399 | size = *((int32_t *)p->data + 1); |
| 400 | } |
| 401 | mCaptureSize = size; |
| 402 | |
| 403 | ALOGV("initCaptureSize size %d status %d", mCaptureSize, status); |
| 404 | |
| 405 | return size; |
| 406 | } |
| 407 | |
| 408 | void Visualizer::controlStatusChanged(bool controlGranted) { |
| 409 | if (controlGranted) { |
| 410 | // this Visualizer instance regained control of the effect, reset the scaling mode |
| 411 | // and capture size as has been cached through it. |
| 412 | ALOGV("controlStatusChanged(true) causes effect parameter reset:"); |
| 413 | ALOGV(" scaling mode reset to %d", mScalingMode); |
| 414 | setScalingMode(mScalingMode); |
| 415 | ALOGV(" capture size reset to %d", mCaptureSize); |
| 416 | setCaptureSize(mCaptureSize); |
| 417 | } |
| 418 | AudioEffect::controlStatusChanged(controlGranted); |
| 419 | } |
| 420 | |
| 421 | //------------------------------------------------------------------------- |
| 422 | |
| 423 | Visualizer::CaptureThread::CaptureThread(Visualizer* receiver, uint32_t captureRate, |
| 424 | bool bCanCallJava) |
| 425 | : Thread(bCanCallJava), mReceiver(receiver) |
| 426 | { |
| 427 | mSleepTimeUs = 1000000000 / captureRate; |
| 428 | ALOGV("CaptureThread cstor %p captureRate %d mSleepTimeUs %d", this, captureRate, mSleepTimeUs); |
| 429 | } |
| 430 | |
| 431 | bool Visualizer::CaptureThread::threadLoop() |
| 432 | { |
| 433 | ALOGV("CaptureThread %p enter", this); |
| 434 | sp<Visualizer> receiver = mReceiver.promote(); |
| 435 | if (receiver == NULL) { |
| 436 | return false; |
| 437 | } |
| 438 | while (!exitPending()) |
| 439 | { |
| 440 | usleep(mSleepTimeUs); |
| 441 | receiver->periodicCapture(); |
| 442 | } |
| 443 | ALOGV("CaptureThread %p exiting", this); |
| 444 | return false; |
| 445 | } |
| 446 | |
| 447 | } // namespace android |