blob: dc4603861cc459034754d4060a8ffa6f21950c5d [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
Dima Zavinfce7a472011-04-19 22:30:36 -070027#include <cutils/bitops.h>
28
Eric Laurentda7581b2010-07-02 08:12:41 -070029#include <media/Visualizer.h>
Glenn Kasten3f6448e2012-01-16 13:11:50 -080030#include <audio_utils/fixedfft.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070031#include <utils/Thread.h>
Eric Laurentda7581b2010-07-02 08:12:41 -070032
33namespace android {
34
35// ---------------------------------------------------------------------------
36
Svet Ganovbe71aa22015-04-28 12:06:02 -070037Visualizer::Visualizer (const String16& opPackageName,
38 int32_t priority,
Eric Laurentda7581b2010-07-02 08:12:41 -070039 effect_callback_t cbf,
40 void* user,
41 int sessionId)
Svet Ganovbe71aa22015-04-28 12:06:02 -070042 : AudioEffect(SL_IID_VISUALIZATION, opPackageName, NULL, priority, cbf, user, sessionId),
Eric Laurentda7581b2010-07-02 08:12:41 -070043 mCaptureRate(CAPTURE_RATE_DEF),
44 mCaptureSize(CAPTURE_SIZE_DEF),
45 mSampleRate(44100000),
Jean-Michel Trivi3476de62012-04-15 17:15:07 -070046 mScalingMode(VISUALIZER_SCALING_MODE_NORMALIZED),
Jean-Michel Trivi09647d22013-09-20 11:58:40 -070047 mMeasurementMode(MEASUREMENT_MODE_NONE),
Eric Laurentda7581b2010-07-02 08:12:41 -070048 mCaptureCallBack(NULL),
49 mCaptureCbkUser(NULL)
50{
51 initCaptureSize();
Eric Laurentda7581b2010-07-02 08:12:41 -070052}
53
54Visualizer::~Visualizer()
55{
Haynes Mathew George63f6ffb2014-09-25 10:33:12 -070056 ALOGV("Visualizer::~Visualizer()");
57 if (mCaptureThread != NULL) {
58 mCaptureThread->requestExitAndWait();
59 mCaptureThread.clear();
60 }
61 mCaptureCallBack = NULL;
62 mCaptureFlags = 0;
Eric Laurentda7581b2010-07-02 08:12:41 -070063}
64
65status_t Visualizer::setEnabled(bool enabled)
66{
Glenn Kastena9b21c52012-01-17 10:06:38 -080067 Mutex::Autolock _l(mCaptureLock);
Eric Laurentda7581b2010-07-02 08:12:41 -070068
69 sp<CaptureThread> t = mCaptureThread;
70 if (t != 0) {
71 if (enabled) {
72 if (t->exitPending()) {
73 if (t->requestExitAndWait() == WOULD_BLOCK) {
Steve Block29357bc2012-01-06 19:20:56 +000074 ALOGE("Visualizer::enable() called from thread");
Eric Laurentda7581b2010-07-02 08:12:41 -070075 return INVALID_OPERATION;
76 }
77 }
78 }
79 t->mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -070080 }
Eric Laurentda7581b2010-07-02 08:12:41 -070081
82 status_t status = AudioEffect::setEnabled(enabled);
83
84 if (status == NO_ERROR) {
85 if (t != 0) {
86 if (enabled) {
Glenn Kasten9096f342012-01-19 08:14:08 -080087 t->run("Visualizer");
Eric Laurentda7581b2010-07-02 08:12:41 -070088 } else {
89 t->requestExit();
90 }
91 }
92 }
93
94 if (t != 0) {
95 t->mLock.unlock();
96 }
97
98 return status;
99}
100
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700101status_t Visualizer::setCaptureCallBack(capture_cbk_t cbk, void* user, uint32_t flags,
102 uint32_t rate)
Eric Laurentda7581b2010-07-02 08:12:41 -0700103{
104 if (rate > CAPTURE_RATE_MAX) {
105 return BAD_VALUE;
106 }
Glenn Kastena9b21c52012-01-17 10:06:38 -0800107 Mutex::Autolock _l(mCaptureLock);
Eric Laurentda7581b2010-07-02 08:12:41 -0700108
109 if (mEnabled) {
110 return INVALID_OPERATION;
111 }
112
Haynes Mathew George63f6ffb2014-09-25 10:33:12 -0700113 if (mCaptureThread != 0) {
114 mCaptureLock.unlock();
115 mCaptureThread->requestExitAndWait();
116 mCaptureLock.lock();
Eric Laurentda7581b2010-07-02 08:12:41 -0700117 }
Haynes Mathew George63f6ffb2014-09-25 10:33:12 -0700118
Eric Laurentda7581b2010-07-02 08:12:41 -0700119 mCaptureThread.clear();
120 mCaptureCallBack = cbk;
121 mCaptureCbkUser = user;
122 mCaptureFlags = flags;
123 mCaptureRate = rate;
124
Eric Laurentda7581b2010-07-02 08:12:41 -0700125 if (cbk != NULL) {
126 mCaptureThread = new CaptureThread(*this, rate, ((flags & CAPTURE_CALL_JAVA) != 0));
Eric Laurentda7581b2010-07-02 08:12:41 -0700127 }
Steve Block3856b092011-10-20 11:56:00 +0100128 ALOGV("setCaptureCallBack() rate: %d thread %p flags 0x%08x",
Eric Laurentda7581b2010-07-02 08:12:41 -0700129 rate, mCaptureThread.get(), mCaptureFlags);
130 return NO_ERROR;
131}
132
133status_t Visualizer::setCaptureSize(uint32_t size)
134{
135 if (size > VISUALIZER_CAPTURE_SIZE_MAX ||
136 size < VISUALIZER_CAPTURE_SIZE_MIN ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700137 popcount(size) != 1) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700138 return BAD_VALUE;
139 }
140
Glenn Kastena9b21c52012-01-17 10:06:38 -0800141 Mutex::Autolock _l(mCaptureLock);
Eric Laurentda7581b2010-07-02 08:12:41 -0700142 if (mEnabled) {
143 return INVALID_OPERATION;
144 }
145
146 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
147 effect_param_t *p = (effect_param_t *)buf32;
148
149 p->psize = sizeof(uint32_t);
150 p->vsize = sizeof(uint32_t);
Eric Laurent6d8b6942011-06-24 07:01:31 -0700151 *(int32_t *)p->data = VISUALIZER_PARAM_CAPTURE_SIZE;
Eric Laurentda7581b2010-07-02 08:12:41 -0700152 *((int32_t *)p->data + 1)= size;
153 status_t status = setParameter(p);
154
Steve Block3856b092011-10-20 11:56:00 +0100155 ALOGV("setCaptureSize size %d status %d p->status %d", size, status, p->status);
Eric Laurentda7581b2010-07-02 08:12:41 -0700156
157 if (status == NO_ERROR) {
158 status = p->status;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700159 if (status == NO_ERROR) {
160 mCaptureSize = size;
161 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700162 }
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700163
164 return status;
165}
166
167status_t Visualizer::setScalingMode(uint32_t mode) {
168 if ((mode != VISUALIZER_SCALING_MODE_NORMALIZED)
169 && (mode != VISUALIZER_SCALING_MODE_AS_PLAYED)) {
170 return BAD_VALUE;
171 }
172
173 Mutex::Autolock _l(mCaptureLock);
174
175 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
176 effect_param_t *p = (effect_param_t *)buf32;
177
178 p->psize = sizeof(uint32_t);
179 p->vsize = sizeof(uint32_t);
180 *(int32_t *)p->data = VISUALIZER_PARAM_SCALING_MODE;
181 *((int32_t *)p->data + 1)= mode;
182 status_t status = setParameter(p);
183
184 ALOGV("setScalingMode mode %d status %d p->status %d", mode, status, p->status);
185
Eric Laurentda7581b2010-07-02 08:12:41 -0700186 if (status == NO_ERROR) {
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700187 status = p->status;
188 if (status == NO_ERROR) {
189 mScalingMode = mode;
190 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700191 }
192
193 return status;
194}
195
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700196status_t Visualizer::setMeasurementMode(uint32_t mode) {
197 if ((mode != MEASUREMENT_MODE_NONE)
198 //Note: needs to be handled as a mask when more measurement modes are added
199 && ((mode & MEASUREMENT_MODE_PEAK_RMS) != mode)) {
200 return BAD_VALUE;
201 }
202
203 Mutex::Autolock _l(mCaptureLock);
204
205 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
206 effect_param_t *p = (effect_param_t *)buf32;
207
208 p->psize = sizeof(uint32_t);
209 p->vsize = sizeof(uint32_t);
210 *(int32_t *)p->data = VISUALIZER_PARAM_MEASUREMENT_MODE;
211 *((int32_t *)p->data + 1)= mode;
212 status_t status = setParameter(p);
213
214 ALOGV("setMeasurementMode mode %d status %d p->status %d", mode, status, p->status);
215
216 if (status == NO_ERROR) {
217 status = p->status;
218 if (status == NO_ERROR) {
219 mMeasurementMode = mode;
220 }
221 }
222 return status;
223}
224
225status_t Visualizer::getIntMeasurements(uint32_t type, uint32_t number, int32_t *measurements) {
226 if (mMeasurementMode == MEASUREMENT_MODE_NONE) {
227 ALOGE("Cannot retrieve int measurements, no measurement mode set");
228 return INVALID_OPERATION;
229 }
230 if (!(mMeasurementMode & type)) {
231 // measurement type has not been set on this Visualizer
232 ALOGE("Cannot retrieve int measurements, requested measurement mode 0x%x not set(0x%x)",
233 type, mMeasurementMode);
234 return INVALID_OPERATION;
235 }
236 // only peak+RMS measurement supported
237 if ((type != MEASUREMENT_MODE_PEAK_RMS)
238 // for peak+RMS measurement, the results are 2 int32_t values
239 || (number != 2)) {
240 ALOGE("Cannot retrieve int measurements, MEASUREMENT_MODE_PEAK_RMS returns 2 ints, not %d",
241 number);
242 return BAD_VALUE;
243 }
244
245 status_t status = NO_ERROR;
246 if (mEnabled) {
247 uint32_t replySize = number * sizeof(int32_t);
248 status = command(VISUALIZER_CMD_MEASURE,
249 sizeof(uint32_t) /*cmdSize*/,
250 &type /*cmdData*/,
251 &replySize, measurements);
252 ALOGV("getMeasurements() command returned %d", status);
253 if ((status == NO_ERROR) && (replySize == 0)) {
254 status = NOT_ENOUGH_DATA;
255 }
256 } else {
257 ALOGV("getMeasurements() disabled");
258 return INVALID_OPERATION;
259 }
260 return status;
261}
262
Eric Laurentda7581b2010-07-02 08:12:41 -0700263status_t Visualizer::getWaveForm(uint8_t *waveform)
264{
265 if (waveform == NULL) {
266 return BAD_VALUE;
267 }
268 if (mCaptureSize == 0) {
269 return NO_INIT;
270 }
271
272 status_t status = NO_ERROR;
273 if (mEnabled) {
Eric Laurent25f43952010-07-28 05:40:18 -0700274 uint32_t replySize = mCaptureSize;
Eric Laurent6d8b6942011-06-24 07:01:31 -0700275 status = command(VISUALIZER_CMD_CAPTURE, 0, NULL, &replySize, waveform);
Steve Block3856b092011-10-20 11:56:00 +0100276 ALOGV("getWaveForm() command returned %d", status);
John Grossmanaf7d8182012-01-11 12:23:42 -0800277 if ((status == NO_ERROR) && (replySize == 0)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700278 status = NOT_ENOUGH_DATA;
279 }
280 } else {
Steve Block3856b092011-10-20 11:56:00 +0100281 ALOGV("getWaveForm() disabled");
Eric Laurentda7581b2010-07-02 08:12:41 -0700282 memset(waveform, 0x80, mCaptureSize);
283 }
284 return status;
285}
286
287status_t Visualizer::getFft(uint8_t *fft)
288{
289 if (fft == NULL) {
290 return BAD_VALUE;
291 }
292 if (mCaptureSize == 0) {
293 return NO_INIT;
294 }
295
296 status_t status = NO_ERROR;
297 if (mEnabled) {
298 uint8_t buf[mCaptureSize];
Eric Laurent0fa449c2010-09-24 11:52:04 -0700299 status = getWaveForm(buf);
Eric Laurentda7581b2010-07-02 08:12:41 -0700300 if (status == NO_ERROR) {
301 status = doFft(fft, buf);
302 }
303 } else {
304 memset(fft, 0, mCaptureSize);
305 }
306 return status;
307}
308
309status_t Visualizer::doFft(uint8_t *fft, uint8_t *waveform)
310{
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800311 int32_t workspace[mCaptureSize >> 1];
312 int32_t nonzero = 0;
313
314 for (uint32_t i = 0; i < mCaptureSize; i += 2) {
Chia-chi Yeh6b6a7362010-11-01 10:56:45 +0800315 workspace[i >> 1] =
316 ((waveform[i] ^ 0x80) << 24) | ((waveform[i + 1] ^ 0x80) << 8);
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800317 nonzero |= workspace[i >> 1];
Eric Laurentda7581b2010-07-02 08:12:41 -0700318 }
319
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800320 if (nonzero) {
321 fixed_fft_real(mCaptureSize >> 1, workspace);
Eric Laurentda7581b2010-07-02 08:12:41 -0700322 }
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800323
324 for (uint32_t i = 0; i < mCaptureSize; i += 2) {
Marco Nelissen209821c2011-01-18 16:44:28 -0800325 short tmp = workspace[i >> 1] >> 21;
326 while (tmp > 127 || tmp < -128) tmp >>= 1;
327 fft[i] = tmp;
328 tmp = workspace[i >> 1];
329 tmp >>= 5;
330 while (tmp > 127 || tmp < -128) tmp >>= 1;
331 fft[i + 1] = tmp;
Eric Laurentda7581b2010-07-02 08:12:41 -0700332 }
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800333
Eric Laurentda7581b2010-07-02 08:12:41 -0700334 return NO_ERROR;
335}
336
337void Visualizer::periodicCapture()
338{
Glenn Kastena9b21c52012-01-17 10:06:38 -0800339 Mutex::Autolock _l(mCaptureLock);
Steve Block3856b092011-10-20 11:56:00 +0100340 ALOGV("periodicCapture() %p mCaptureCallBack %p mCaptureFlags 0x%08x",
Eric Laurentda7581b2010-07-02 08:12:41 -0700341 this, mCaptureCallBack, mCaptureFlags);
342 if (mCaptureCallBack != NULL &&
343 (mCaptureFlags & (CAPTURE_WAVEFORM|CAPTURE_FFT)) &&
344 mCaptureSize != 0) {
345 uint8_t waveform[mCaptureSize];
346 status_t status = getWaveForm(waveform);
347 if (status != NO_ERROR) {
348 return;
349 }
350 uint8_t fft[mCaptureSize];
351 if (mCaptureFlags & CAPTURE_FFT) {
352 status = doFft(fft, waveform);
353 }
354 if (status != NO_ERROR) {
355 return;
356 }
357 uint8_t *wavePtr = NULL;
358 uint8_t *fftPtr = NULL;
359 uint32_t waveSize = 0;
360 uint32_t fftSize = 0;
361 if (mCaptureFlags & CAPTURE_WAVEFORM) {
362 wavePtr = waveform;
363 waveSize = mCaptureSize;
364 }
365 if (mCaptureFlags & CAPTURE_FFT) {
366 fftPtr = fft;
367 fftSize = mCaptureSize;
368 }
369 mCaptureCallBack(mCaptureCbkUser, waveSize, wavePtr, fftSize, fftPtr, mSampleRate);
370 }
371}
372
373uint32_t Visualizer::initCaptureSize()
374{
375 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
376 effect_param_t *p = (effect_param_t *)buf32;
377
378 p->psize = sizeof(uint32_t);
379 p->vsize = sizeof(uint32_t);
Eric Laurent6d8b6942011-06-24 07:01:31 -0700380 *(int32_t *)p->data = VISUALIZER_PARAM_CAPTURE_SIZE;
Eric Laurentda7581b2010-07-02 08:12:41 -0700381 status_t status = getParameter(p);
382
383 if (status == NO_ERROR) {
384 status = p->status;
385 }
386
387 uint32_t size = 0;
388 if (status == NO_ERROR) {
389 size = *((int32_t *)p->data + 1);
390 }
391 mCaptureSize = size;
392
Steve Block3856b092011-10-20 11:56:00 +0100393 ALOGV("initCaptureSize size %d status %d", mCaptureSize, status);
Eric Laurentda7581b2010-07-02 08:12:41 -0700394
395 return size;
396}
397
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700398void Visualizer::controlStatusChanged(bool controlGranted) {
399 if (controlGranted) {
400 // this Visualizer instance regained control of the effect, reset the scaling mode
401 // and capture size as has been cached through it.
402 ALOGV("controlStatusChanged(true) causes effect parameter reset:");
403 ALOGV(" scaling mode reset to %d", mScalingMode);
404 setScalingMode(mScalingMode);
405 ALOGV(" capture size reset to %d", mCaptureSize);
406 setCaptureSize(mCaptureSize);
407 }
408 AudioEffect::controlStatusChanged(controlGranted);
409}
410
Eric Laurentda7581b2010-07-02 08:12:41 -0700411//-------------------------------------------------------------------------
412
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700413Visualizer::CaptureThread::CaptureThread(Visualizer& receiver, uint32_t captureRate,
414 bool bCanCallJava)
Eric Laurentda7581b2010-07-02 08:12:41 -0700415 : Thread(bCanCallJava), mReceiver(receiver)
416{
417 mSleepTimeUs = 1000000000 / captureRate;
Steve Block3856b092011-10-20 11:56:00 +0100418 ALOGV("CaptureThread cstor %p captureRate %d mSleepTimeUs %d", this, captureRate, mSleepTimeUs);
Eric Laurentda7581b2010-07-02 08:12:41 -0700419}
420
421bool Visualizer::CaptureThread::threadLoop()
422{
Steve Block3856b092011-10-20 11:56:00 +0100423 ALOGV("CaptureThread %p enter", this);
Eric Laurentda7581b2010-07-02 08:12:41 -0700424 while (!exitPending())
425 {
426 usleep(mSleepTimeUs);
427 mReceiver.periodicCapture();
428 }
Steve Block3856b092011-10-20 11:56:00 +0100429 ALOGV("CaptureThread %p exiting", this);
Eric Laurentda7581b2010-07-02 08:12:41 -0700430 return false;
431}
432
Glenn Kasten40bc9062015-03-20 09:09:33 -0700433} // namespace android