blob: 2bf08020b3fb1d9be2cdbc840c10164750975307 [file] [log] [blame]
Eric Laurentda7581b2010-07-02 08:12:41 -07001/*
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 <media/Visualizer.h>
Glenn Kasten3f6448e2012-01-16 13:11:50 -080028#include <audio_utils/fixedfft.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070029#include <utils/Thread.h>
Eric Laurentda7581b2010-07-02 08:12:41 -070030
31namespace android {
32
33// ---------------------------------------------------------------------------
34
Svet Ganovbe71aa22015-04-28 12:06:02 -070035Visualizer::Visualizer (const String16& opPackageName,
36 int32_t priority,
Eric Laurentda7581b2010-07-02 08:12:41 -070037 effect_callback_t cbf,
38 void* user,
Glenn Kastend848eb42016-03-08 13:42:11 -080039 audio_session_t sessionId)
Svet Ganovbe71aa22015-04-28 12:06:02 -070040 : AudioEffect(SL_IID_VISUALIZATION, opPackageName, NULL, priority, cbf, user, sessionId),
Eric Laurentda7581b2010-07-02 08:12:41 -070041 mCaptureRate(CAPTURE_RATE_DEF),
42 mCaptureSize(CAPTURE_SIZE_DEF),
43 mSampleRate(44100000),
Jean-Michel Trivi3476de62012-04-15 17:15:07 -070044 mScalingMode(VISUALIZER_SCALING_MODE_NORMALIZED),
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070045 mMeasurementMode(MEASUREMENT_MODE_NONE),
Eric Laurentda7581b2010-07-02 08:12:41 -070046 mCaptureCallBack(NULL),
47 mCaptureCbkUser(NULL)
48{
49 initCaptureSize();
Eric Laurentda7581b2010-07-02 08:12:41 -070050}
51
52Visualizer::~Visualizer()
53{
Haynes Mathew George63f6ffb2014-09-25 10:33:12 -070054 ALOGV("Visualizer::~Visualizer()");
Ricardo Garcia9b030df2015-06-18 21:01:53 -070055 setEnabled(false);
ganxiaolin2e5b5702016-01-29 19:57:57 +080056 setCaptureCallBack(NULL, NULL, 0, 0);
Eric Laurentda7581b2010-07-02 08:12:41 -070057}
58
zengjing704c5772018-09-29 13:25:35 +080059void Visualizer::release()
60{
61 ALOGV("Visualizer::release()");
62 setEnabled(false);
63 Mutex::Autolock _l(mCaptureLock);
64
65 mCaptureThread.clear();
66 mCaptureCallBack = NULL;
67 mCaptureCbkUser = NULL;
68 mCaptureFlags = 0;
69 mCaptureRate = 0;
70}
71
Eric Laurentda7581b2010-07-02 08:12:41 -070072status_t Visualizer::setEnabled(bool enabled)
73{
Glenn Kastena9b21c52012-01-17 10:06:38 -080074 Mutex::Autolock _l(mCaptureLock);
Eric Laurentda7581b2010-07-02 08:12:41 -070075
76 sp<CaptureThread> t = mCaptureThread;
77 if (t != 0) {
78 if (enabled) {
79 if (t->exitPending()) {
Aniket Kumar Latae8e1c692019-07-03 10:44:47 -070080 mCaptureLock.unlock();
Eric Laurentda7581b2010-07-02 08:12:41 -070081 if (t->requestExitAndWait() == WOULD_BLOCK) {
Aniket Kumar Latae8e1c692019-07-03 10:44:47 -070082 mCaptureLock.lock();
Steve Block29357bc2012-01-06 19:20:56 +000083 ALOGE("Visualizer::enable() called from thread");
Eric Laurentda7581b2010-07-02 08:12:41 -070084 return INVALID_OPERATION;
85 }
Aniket Kumar Latae8e1c692019-07-03 10:44:47 -070086 mCaptureLock.lock();
Eric Laurentda7581b2010-07-02 08:12:41 -070087 }
88 }
89 t->mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -070090 }
Eric Laurentda7581b2010-07-02 08:12:41 -070091
92 status_t status = AudioEffect::setEnabled(enabled);
93
ganxiaolin2e5b5702016-01-29 19:57:57 +080094 if (t != 0) {
95 if (enabled && status == NO_ERROR) {
96 t->run("Visualizer");
97 } else {
98 t->requestExit();
Eric Laurentda7581b2010-07-02 08:12:41 -070099 }
100 }
101
102 if (t != 0) {
103 t->mLock.unlock();
104 }
105
106 return status;
107}
108
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700109status_t Visualizer::setCaptureCallBack(capture_cbk_t cbk, void* user, uint32_t flags,
ganxiaolin2e5b5702016-01-29 19:57:57 +0800110 uint32_t rate)
Eric Laurentda7581b2010-07-02 08:12:41 -0700111{
112 if (rate > CAPTURE_RATE_MAX) {
113 return BAD_VALUE;
114 }
Glenn Kastena9b21c52012-01-17 10:06:38 -0800115 Mutex::Autolock _l(mCaptureLock);
Eric Laurentda7581b2010-07-02 08:12:41 -0700116
ganxiaolin2e5b5702016-01-29 19:57:57 +0800117 if (mEnabled) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700118 return INVALID_OPERATION;
119 }
120
Haynes Mathew George63f6ffb2014-09-25 10:33:12 -0700121 if (mCaptureThread != 0) {
122 mCaptureLock.unlock();
123 mCaptureThread->requestExitAndWait();
124 mCaptureLock.lock();
Eric Laurentda7581b2010-07-02 08:12:41 -0700125 }
Haynes Mathew George63f6ffb2014-09-25 10:33:12 -0700126
Eric Laurentda7581b2010-07-02 08:12:41 -0700127 mCaptureThread.clear();
128 mCaptureCallBack = cbk;
129 mCaptureCbkUser = user;
130 mCaptureFlags = flags;
131 mCaptureRate = rate;
132
Eric Laurentda7581b2010-07-02 08:12:41 -0700133 if (cbk != NULL) {
zengjing704c5772018-09-29 13:25:35 +0800134 mCaptureThread = new CaptureThread(this, rate, ((flags & CAPTURE_CALL_JAVA) != 0));
Eric Laurentda7581b2010-07-02 08:12:41 -0700135 }
Steve Block3856b092011-10-20 11:56:00 +0100136 ALOGV("setCaptureCallBack() rate: %d thread %p flags 0x%08x",
Eric Laurentda7581b2010-07-02 08:12:41 -0700137 rate, mCaptureThread.get(), mCaptureFlags);
138 return NO_ERROR;
139}
140
141status_t Visualizer::setCaptureSize(uint32_t size)
142{
143 if (size > VISUALIZER_CAPTURE_SIZE_MAX ||
144 size < VISUALIZER_CAPTURE_SIZE_MIN ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700145 popcount(size) != 1) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700146 return BAD_VALUE;
147 }
148
Glenn Kastena9b21c52012-01-17 10:06:38 -0800149 Mutex::Autolock _l(mCaptureLock);
Eric Laurentda7581b2010-07-02 08:12:41 -0700150 if (mEnabled) {
151 return INVALID_OPERATION;
152 }
153
154 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
155 effect_param_t *p = (effect_param_t *)buf32;
156
157 p->psize = sizeof(uint32_t);
158 p->vsize = sizeof(uint32_t);
Eric Laurent6d8b6942011-06-24 07:01:31 -0700159 *(int32_t *)p->data = VISUALIZER_PARAM_CAPTURE_SIZE;
Eric Laurentda7581b2010-07-02 08:12:41 -0700160 *((int32_t *)p->data + 1)= size;
161 status_t status = setParameter(p);
162
Steve Block3856b092011-10-20 11:56:00 +0100163 ALOGV("setCaptureSize size %d status %d p->status %d", size, status, p->status);
Eric Laurentda7581b2010-07-02 08:12:41 -0700164
165 if (status == NO_ERROR) {
166 status = p->status;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700167 if (status == NO_ERROR) {
168 mCaptureSize = size;
169 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700170 }
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700171
172 return status;
173}
174
175status_t Visualizer::setScalingMode(uint32_t mode) {
176 if ((mode != VISUALIZER_SCALING_MODE_NORMALIZED)
177 && (mode != VISUALIZER_SCALING_MODE_AS_PLAYED)) {
178 return BAD_VALUE;
179 }
180
181 Mutex::Autolock _l(mCaptureLock);
182
183 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
184 effect_param_t *p = (effect_param_t *)buf32;
185
186 p->psize = sizeof(uint32_t);
187 p->vsize = sizeof(uint32_t);
188 *(int32_t *)p->data = VISUALIZER_PARAM_SCALING_MODE;
189 *((int32_t *)p->data + 1)= mode;
190 status_t status = setParameter(p);
191
192 ALOGV("setScalingMode mode %d status %d p->status %d", mode, status, p->status);
193
Eric Laurentda7581b2010-07-02 08:12:41 -0700194 if (status == NO_ERROR) {
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700195 status = p->status;
196 if (status == NO_ERROR) {
197 mScalingMode = mode;
198 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700199 }
200
201 return status;
202}
203
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700204status_t Visualizer::setMeasurementMode(uint32_t mode) {
205 if ((mode != MEASUREMENT_MODE_NONE)
206 //Note: needs to be handled as a mask when more measurement modes are added
207 && ((mode & MEASUREMENT_MODE_PEAK_RMS) != mode)) {
208 return BAD_VALUE;
209 }
210
211 Mutex::Autolock _l(mCaptureLock);
212
213 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
214 effect_param_t *p = (effect_param_t *)buf32;
215
216 p->psize = sizeof(uint32_t);
217 p->vsize = sizeof(uint32_t);
218 *(int32_t *)p->data = VISUALIZER_PARAM_MEASUREMENT_MODE;
219 *((int32_t *)p->data + 1)= mode;
220 status_t status = setParameter(p);
221
222 ALOGV("setMeasurementMode mode %d status %d p->status %d", mode, status, p->status);
223
224 if (status == NO_ERROR) {
225 status = p->status;
226 if (status == NO_ERROR) {
227 mMeasurementMode = mode;
228 }
229 }
230 return status;
231}
232
233status_t Visualizer::getIntMeasurements(uint32_t type, uint32_t number, int32_t *measurements) {
234 if (mMeasurementMode == MEASUREMENT_MODE_NONE) {
235 ALOGE("Cannot retrieve int measurements, no measurement mode set");
236 return INVALID_OPERATION;
237 }
238 if (!(mMeasurementMode & type)) {
239 // measurement type has not been set on this Visualizer
240 ALOGE("Cannot retrieve int measurements, requested measurement mode 0x%x not set(0x%x)",
241 type, mMeasurementMode);
242 return INVALID_OPERATION;
243 }
244 // only peak+RMS measurement supported
245 if ((type != MEASUREMENT_MODE_PEAK_RMS)
246 // for peak+RMS measurement, the results are 2 int32_t values
247 || (number != 2)) {
248 ALOGE("Cannot retrieve int measurements, MEASUREMENT_MODE_PEAK_RMS returns 2 ints, not %d",
249 number);
250 return BAD_VALUE;
251 }
252
253 status_t status = NO_ERROR;
254 if (mEnabled) {
255 uint32_t replySize = number * sizeof(int32_t);
256 status = command(VISUALIZER_CMD_MEASURE,
257 sizeof(uint32_t) /*cmdSize*/,
258 &type /*cmdData*/,
259 &replySize, measurements);
260 ALOGV("getMeasurements() command returned %d", status);
261 if ((status == NO_ERROR) && (replySize == 0)) {
262 status = NOT_ENOUGH_DATA;
263 }
264 } else {
265 ALOGV("getMeasurements() disabled");
266 return INVALID_OPERATION;
267 }
268 return status;
269}
270
Eric Laurentda7581b2010-07-02 08:12:41 -0700271status_t Visualizer::getWaveForm(uint8_t *waveform)
272{
273 if (waveform == NULL) {
274 return BAD_VALUE;
275 }
276 if (mCaptureSize == 0) {
277 return NO_INIT;
278 }
279
280 status_t status = NO_ERROR;
281 if (mEnabled) {
Eric Laurent25f43952010-07-28 05:40:18 -0700282 uint32_t replySize = mCaptureSize;
Eric Laurent6d8b6942011-06-24 07:01:31 -0700283 status = command(VISUALIZER_CMD_CAPTURE, 0, NULL, &replySize, waveform);
Steve Block3856b092011-10-20 11:56:00 +0100284 ALOGV("getWaveForm() command returned %d", status);
John Grossmanaf7d8182012-01-11 12:23:42 -0800285 if ((status == NO_ERROR) && (replySize == 0)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700286 status = NOT_ENOUGH_DATA;
287 }
288 } else {
Steve Block3856b092011-10-20 11:56:00 +0100289 ALOGV("getWaveForm() disabled");
Eric Laurentda7581b2010-07-02 08:12:41 -0700290 memset(waveform, 0x80, mCaptureSize);
291 }
292 return status;
293}
294
295status_t Visualizer::getFft(uint8_t *fft)
296{
297 if (fft == NULL) {
298 return BAD_VALUE;
299 }
300 if (mCaptureSize == 0) {
301 return NO_INIT;
302 }
303
304 status_t status = NO_ERROR;
305 if (mEnabled) {
306 uint8_t buf[mCaptureSize];
Eric Laurent0fa449c2010-09-24 11:52:04 -0700307 status = getWaveForm(buf);
Eric Laurentda7581b2010-07-02 08:12:41 -0700308 if (status == NO_ERROR) {
309 status = doFft(fft, buf);
310 }
311 } else {
312 memset(fft, 0, mCaptureSize);
313 }
314 return status;
315}
316
317status_t Visualizer::doFft(uint8_t *fft, uint8_t *waveform)
318{
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800319 int32_t workspace[mCaptureSize >> 1];
320 int32_t nonzero = 0;
321
322 for (uint32_t i = 0; i < mCaptureSize; i += 2) {
Chia-chi Yeh6b6a7362010-11-01 10:56:45 +0800323 workspace[i >> 1] =
324 ((waveform[i] ^ 0x80) << 24) | ((waveform[i + 1] ^ 0x80) << 8);
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800325 nonzero |= workspace[i >> 1];
Eric Laurentda7581b2010-07-02 08:12:41 -0700326 }
327
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800328 if (nonzero) {
329 fixed_fft_real(mCaptureSize >> 1, workspace);
Eric Laurentda7581b2010-07-02 08:12:41 -0700330 }
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800331
332 for (uint32_t i = 0; i < mCaptureSize; i += 2) {
Marco Nelissen209821c2011-01-18 16:44:28 -0800333 short tmp = workspace[i >> 1] >> 21;
334 while (tmp > 127 || tmp < -128) tmp >>= 1;
335 fft[i] = tmp;
336 tmp = workspace[i >> 1];
337 tmp >>= 5;
338 while (tmp > 127 || tmp < -128) tmp >>= 1;
339 fft[i + 1] = tmp;
Eric Laurentda7581b2010-07-02 08:12:41 -0700340 }
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800341
Eric Laurentda7581b2010-07-02 08:12:41 -0700342 return NO_ERROR;
343}
344
345void Visualizer::periodicCapture()
346{
Glenn Kastena9b21c52012-01-17 10:06:38 -0800347 Mutex::Autolock _l(mCaptureLock);
Steve Block3856b092011-10-20 11:56:00 +0100348 ALOGV("periodicCapture() %p mCaptureCallBack %p mCaptureFlags 0x%08x",
Eric Laurentda7581b2010-07-02 08:12:41 -0700349 this, mCaptureCallBack, mCaptureFlags);
350 if (mCaptureCallBack != NULL &&
351 (mCaptureFlags & (CAPTURE_WAVEFORM|CAPTURE_FFT)) &&
352 mCaptureSize != 0) {
353 uint8_t waveform[mCaptureSize];
354 status_t status = getWaveForm(waveform);
355 if (status != NO_ERROR) {
356 return;
357 }
358 uint8_t fft[mCaptureSize];
359 if (mCaptureFlags & CAPTURE_FFT) {
360 status = doFft(fft, waveform);
361 }
362 if (status != NO_ERROR) {
363 return;
364 }
365 uint8_t *wavePtr = NULL;
366 uint8_t *fftPtr = NULL;
367 uint32_t waveSize = 0;
368 uint32_t fftSize = 0;
369 if (mCaptureFlags & CAPTURE_WAVEFORM) {
370 wavePtr = waveform;
371 waveSize = mCaptureSize;
372 }
373 if (mCaptureFlags & CAPTURE_FFT) {
374 fftPtr = fft;
375 fftSize = mCaptureSize;
376 }
377 mCaptureCallBack(mCaptureCbkUser, waveSize, wavePtr, fftSize, fftPtr, mSampleRate);
378 }
379}
380
381uint32_t Visualizer::initCaptureSize()
382{
383 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
384 effect_param_t *p = (effect_param_t *)buf32;
385
386 p->psize = sizeof(uint32_t);
387 p->vsize = sizeof(uint32_t);
Eric Laurent6d8b6942011-06-24 07:01:31 -0700388 *(int32_t *)p->data = VISUALIZER_PARAM_CAPTURE_SIZE;
Eric Laurentda7581b2010-07-02 08:12:41 -0700389 status_t status = getParameter(p);
390
391 if (status == NO_ERROR) {
392 status = p->status;
393 }
394
395 uint32_t size = 0;
396 if (status == NO_ERROR) {
397 size = *((int32_t *)p->data + 1);
398 }
399 mCaptureSize = size;
400
Steve Block3856b092011-10-20 11:56:00 +0100401 ALOGV("initCaptureSize size %d status %d", mCaptureSize, status);
Eric Laurentda7581b2010-07-02 08:12:41 -0700402
403 return size;
404}
405
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700406void Visualizer::controlStatusChanged(bool controlGranted) {
407 if (controlGranted) {
408 // this Visualizer instance regained control of the effect, reset the scaling mode
409 // and capture size as has been cached through it.
410 ALOGV("controlStatusChanged(true) causes effect parameter reset:");
411 ALOGV(" scaling mode reset to %d", mScalingMode);
412 setScalingMode(mScalingMode);
413 ALOGV(" capture size reset to %d", mCaptureSize);
414 setCaptureSize(mCaptureSize);
415 }
416 AudioEffect::controlStatusChanged(controlGranted);
417}
418
Eric Laurentda7581b2010-07-02 08:12:41 -0700419//-------------------------------------------------------------------------
420
zengjing704c5772018-09-29 13:25:35 +0800421Visualizer::CaptureThread::CaptureThread(Visualizer* receiver, uint32_t captureRate,
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700422 bool bCanCallJava)
Eric Laurentda7581b2010-07-02 08:12:41 -0700423 : Thread(bCanCallJava), mReceiver(receiver)
424{
425 mSleepTimeUs = 1000000000 / captureRate;
Steve Block3856b092011-10-20 11:56:00 +0100426 ALOGV("CaptureThread cstor %p captureRate %d mSleepTimeUs %d", this, captureRate, mSleepTimeUs);
Eric Laurentda7581b2010-07-02 08:12:41 -0700427}
428
429bool Visualizer::CaptureThread::threadLoop()
430{
Steve Block3856b092011-10-20 11:56:00 +0100431 ALOGV("CaptureThread %p enter", this);
zengjing704c5772018-09-29 13:25:35 +0800432 sp<Visualizer> receiver = mReceiver.promote();
433 if (receiver == NULL) {
434 return false;
435 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700436 while (!exitPending())
437 {
438 usleep(mSleepTimeUs);
zengjing704c5772018-09-29 13:25:35 +0800439 receiver->periodicCapture();
Eric Laurentda7581b2010-07-02 08:12:41 -0700440 }
Steve Block3856b092011-10-20 11:56:00 +0100441 ALOGV("CaptureThread %p exiting", this);
Eric Laurentda7581b2010-07-02 08:12:41 -0700442 return false;
443}
444
Glenn Kasten40bc9062015-03-20 09:09:33 -0700445} // namespace android