blob: f5c1b1f0108a910e58ff59b80106e3aaff98fa63 [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()");
Ricardo Garcia9b030df2015-06-18 21:01:53 -070057 setEnabled(false);
58 setCaptureCallBack(NULL, NULL, 0, 0, true);
Eric Laurentda7581b2010-07-02 08:12:41 -070059}
60
61status_t Visualizer::setEnabled(bool enabled)
62{
Glenn Kastena9b21c52012-01-17 10:06:38 -080063 Mutex::Autolock _l(mCaptureLock);
Eric Laurentda7581b2010-07-02 08:12:41 -070064
65 sp<CaptureThread> t = mCaptureThread;
66 if (t != 0) {
67 if (enabled) {
68 if (t->exitPending()) {
69 if (t->requestExitAndWait() == WOULD_BLOCK) {
Steve Block29357bc2012-01-06 19:20:56 +000070 ALOGE("Visualizer::enable() called from thread");
Eric Laurentda7581b2010-07-02 08:12:41 -070071 return INVALID_OPERATION;
72 }
73 }
74 }
75 t->mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -070076 }
Eric Laurentda7581b2010-07-02 08:12:41 -070077
78 status_t status = AudioEffect::setEnabled(enabled);
79
80 if (status == NO_ERROR) {
81 if (t != 0) {
82 if (enabled) {
Glenn Kasten9096f342012-01-19 08:14:08 -080083 t->run("Visualizer");
Eric Laurentda7581b2010-07-02 08:12:41 -070084 } else {
85 t->requestExit();
86 }
87 }
88 }
89
90 if (t != 0) {
91 t->mLock.unlock();
92 }
93
94 return status;
95}
96
Glenn Kasten85ab62c2012-11-01 11:11:38 -070097status_t Visualizer::setCaptureCallBack(capture_cbk_t cbk, void* user, uint32_t flags,
Ricardo Garcia9b030df2015-06-18 21:01:53 -070098 uint32_t rate, bool force)
Eric Laurentda7581b2010-07-02 08:12:41 -070099{
100 if (rate > CAPTURE_RATE_MAX) {
101 return BAD_VALUE;
102 }
Glenn Kastena9b21c52012-01-17 10:06:38 -0800103 Mutex::Autolock _l(mCaptureLock);
Eric Laurentda7581b2010-07-02 08:12:41 -0700104
Ricardo Garcia9b030df2015-06-18 21:01:53 -0700105 if (force || mEnabled) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700106 return INVALID_OPERATION;
107 }
108
Haynes Mathew George63f6ffb2014-09-25 10:33:12 -0700109 if (mCaptureThread != 0) {
110 mCaptureLock.unlock();
111 mCaptureThread->requestExitAndWait();
112 mCaptureLock.lock();
Eric Laurentda7581b2010-07-02 08:12:41 -0700113 }
Haynes Mathew George63f6ffb2014-09-25 10:33:12 -0700114
Eric Laurentda7581b2010-07-02 08:12:41 -0700115 mCaptureThread.clear();
116 mCaptureCallBack = cbk;
117 mCaptureCbkUser = user;
118 mCaptureFlags = flags;
119 mCaptureRate = rate;
120
Eric Laurentda7581b2010-07-02 08:12:41 -0700121 if (cbk != NULL) {
122 mCaptureThread = new CaptureThread(*this, rate, ((flags & CAPTURE_CALL_JAVA) != 0));
Eric Laurentda7581b2010-07-02 08:12:41 -0700123 }
Steve Block3856b092011-10-20 11:56:00 +0100124 ALOGV("setCaptureCallBack() rate: %d thread %p flags 0x%08x",
Eric Laurentda7581b2010-07-02 08:12:41 -0700125 rate, mCaptureThread.get(), mCaptureFlags);
126 return NO_ERROR;
127}
128
129status_t Visualizer::setCaptureSize(uint32_t size)
130{
131 if (size > VISUALIZER_CAPTURE_SIZE_MAX ||
132 size < VISUALIZER_CAPTURE_SIZE_MIN ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700133 popcount(size) != 1) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700134 return BAD_VALUE;
135 }
136
Glenn Kastena9b21c52012-01-17 10:06:38 -0800137 Mutex::Autolock _l(mCaptureLock);
Eric Laurentda7581b2010-07-02 08:12:41 -0700138 if (mEnabled) {
139 return INVALID_OPERATION;
140 }
141
142 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
143 effect_param_t *p = (effect_param_t *)buf32;
144
145 p->psize = sizeof(uint32_t);
146 p->vsize = sizeof(uint32_t);
Eric Laurent6d8b6942011-06-24 07:01:31 -0700147 *(int32_t *)p->data = VISUALIZER_PARAM_CAPTURE_SIZE;
Eric Laurentda7581b2010-07-02 08:12:41 -0700148 *((int32_t *)p->data + 1)= size;
149 status_t status = setParameter(p);
150
Steve Block3856b092011-10-20 11:56:00 +0100151 ALOGV("setCaptureSize size %d status %d p->status %d", size, status, p->status);
Eric Laurentda7581b2010-07-02 08:12:41 -0700152
153 if (status == NO_ERROR) {
154 status = p->status;
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700155 if (status == NO_ERROR) {
156 mCaptureSize = size;
157 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700158 }
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700159
160 return status;
161}
162
163status_t Visualizer::setScalingMode(uint32_t mode) {
164 if ((mode != VISUALIZER_SCALING_MODE_NORMALIZED)
165 && (mode != VISUALIZER_SCALING_MODE_AS_PLAYED)) {
166 return BAD_VALUE;
167 }
168
169 Mutex::Autolock _l(mCaptureLock);
170
171 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
172 effect_param_t *p = (effect_param_t *)buf32;
173
174 p->psize = sizeof(uint32_t);
175 p->vsize = sizeof(uint32_t);
176 *(int32_t *)p->data = VISUALIZER_PARAM_SCALING_MODE;
177 *((int32_t *)p->data + 1)= mode;
178 status_t status = setParameter(p);
179
180 ALOGV("setScalingMode mode %d status %d p->status %d", mode, status, p->status);
181
Eric Laurentda7581b2010-07-02 08:12:41 -0700182 if (status == NO_ERROR) {
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700183 status = p->status;
184 if (status == NO_ERROR) {
185 mScalingMode = mode;
186 }
Eric Laurentda7581b2010-07-02 08:12:41 -0700187 }
188
189 return status;
190}
191
Jean-Michel Trivi09647d22013-09-20 11:58:40 -0700192status_t Visualizer::setMeasurementMode(uint32_t mode) {
193 if ((mode != MEASUREMENT_MODE_NONE)
194 //Note: needs to be handled as a mask when more measurement modes are added
195 && ((mode & MEASUREMENT_MODE_PEAK_RMS) != mode)) {
196 return BAD_VALUE;
197 }
198
199 Mutex::Autolock _l(mCaptureLock);
200
201 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
202 effect_param_t *p = (effect_param_t *)buf32;
203
204 p->psize = sizeof(uint32_t);
205 p->vsize = sizeof(uint32_t);
206 *(int32_t *)p->data = VISUALIZER_PARAM_MEASUREMENT_MODE;
207 *((int32_t *)p->data + 1)= mode;
208 status_t status = setParameter(p);
209
210 ALOGV("setMeasurementMode mode %d status %d p->status %d", mode, status, p->status);
211
212 if (status == NO_ERROR) {
213 status = p->status;
214 if (status == NO_ERROR) {
215 mMeasurementMode = mode;
216 }
217 }
218 return status;
219}
220
221status_t Visualizer::getIntMeasurements(uint32_t type, uint32_t number, int32_t *measurements) {
222 if (mMeasurementMode == MEASUREMENT_MODE_NONE) {
223 ALOGE("Cannot retrieve int measurements, no measurement mode set");
224 return INVALID_OPERATION;
225 }
226 if (!(mMeasurementMode & type)) {
227 // measurement type has not been set on this Visualizer
228 ALOGE("Cannot retrieve int measurements, requested measurement mode 0x%x not set(0x%x)",
229 type, mMeasurementMode);
230 return INVALID_OPERATION;
231 }
232 // only peak+RMS measurement supported
233 if ((type != MEASUREMENT_MODE_PEAK_RMS)
234 // for peak+RMS measurement, the results are 2 int32_t values
235 || (number != 2)) {
236 ALOGE("Cannot retrieve int measurements, MEASUREMENT_MODE_PEAK_RMS returns 2 ints, not %d",
237 number);
238 return BAD_VALUE;
239 }
240
241 status_t status = NO_ERROR;
242 if (mEnabled) {
243 uint32_t replySize = number * sizeof(int32_t);
244 status = command(VISUALIZER_CMD_MEASURE,
245 sizeof(uint32_t) /*cmdSize*/,
246 &type /*cmdData*/,
247 &replySize, measurements);
248 ALOGV("getMeasurements() command returned %d", status);
249 if ((status == NO_ERROR) && (replySize == 0)) {
250 status = NOT_ENOUGH_DATA;
251 }
252 } else {
253 ALOGV("getMeasurements() disabled");
254 return INVALID_OPERATION;
255 }
256 return status;
257}
258
Eric Laurentda7581b2010-07-02 08:12:41 -0700259status_t Visualizer::getWaveForm(uint8_t *waveform)
260{
261 if (waveform == NULL) {
262 return BAD_VALUE;
263 }
264 if (mCaptureSize == 0) {
265 return NO_INIT;
266 }
267
268 status_t status = NO_ERROR;
269 if (mEnabled) {
Eric Laurent25f43952010-07-28 05:40:18 -0700270 uint32_t replySize = mCaptureSize;
Eric Laurent6d8b6942011-06-24 07:01:31 -0700271 status = command(VISUALIZER_CMD_CAPTURE, 0, NULL, &replySize, waveform);
Steve Block3856b092011-10-20 11:56:00 +0100272 ALOGV("getWaveForm() command returned %d", status);
John Grossmanaf7d8182012-01-11 12:23:42 -0800273 if ((status == NO_ERROR) && (replySize == 0)) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700274 status = NOT_ENOUGH_DATA;
275 }
276 } else {
Steve Block3856b092011-10-20 11:56:00 +0100277 ALOGV("getWaveForm() disabled");
Eric Laurentda7581b2010-07-02 08:12:41 -0700278 memset(waveform, 0x80, mCaptureSize);
279 }
280 return status;
281}
282
283status_t Visualizer::getFft(uint8_t *fft)
284{
285 if (fft == NULL) {
286 return BAD_VALUE;
287 }
288 if (mCaptureSize == 0) {
289 return NO_INIT;
290 }
291
292 status_t status = NO_ERROR;
293 if (mEnabled) {
294 uint8_t buf[mCaptureSize];
Eric Laurent0fa449c2010-09-24 11:52:04 -0700295 status = getWaveForm(buf);
Eric Laurentda7581b2010-07-02 08:12:41 -0700296 if (status == NO_ERROR) {
297 status = doFft(fft, buf);
298 }
299 } else {
300 memset(fft, 0, mCaptureSize);
301 }
302 return status;
303}
304
305status_t Visualizer::doFft(uint8_t *fft, uint8_t *waveform)
306{
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800307 int32_t workspace[mCaptureSize >> 1];
308 int32_t nonzero = 0;
309
310 for (uint32_t i = 0; i < mCaptureSize; i += 2) {
Chia-chi Yeh6b6a7362010-11-01 10:56:45 +0800311 workspace[i >> 1] =
312 ((waveform[i] ^ 0x80) << 24) | ((waveform[i + 1] ^ 0x80) << 8);
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800313 nonzero |= workspace[i >> 1];
Eric Laurentda7581b2010-07-02 08:12:41 -0700314 }
315
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800316 if (nonzero) {
317 fixed_fft_real(mCaptureSize >> 1, workspace);
Eric Laurentda7581b2010-07-02 08:12:41 -0700318 }
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800319
320 for (uint32_t i = 0; i < mCaptureSize; i += 2) {
Marco Nelissen209821c2011-01-18 16:44:28 -0800321 short tmp = workspace[i >> 1] >> 21;
322 while (tmp > 127 || tmp < -128) tmp >>= 1;
323 fft[i] = tmp;
324 tmp = workspace[i >> 1];
325 tmp >>= 5;
326 while (tmp > 127 || tmp < -128) tmp >>= 1;
327 fft[i + 1] = tmp;
Eric Laurentda7581b2010-07-02 08:12:41 -0700328 }
Chia-chi Yehdbd2b7e2010-08-19 15:34:10 +0800329
Eric Laurentda7581b2010-07-02 08:12:41 -0700330 return NO_ERROR;
331}
332
333void Visualizer::periodicCapture()
334{
Glenn Kastena9b21c52012-01-17 10:06:38 -0800335 Mutex::Autolock _l(mCaptureLock);
Steve Block3856b092011-10-20 11:56:00 +0100336 ALOGV("periodicCapture() %p mCaptureCallBack %p mCaptureFlags 0x%08x",
Eric Laurentda7581b2010-07-02 08:12:41 -0700337 this, mCaptureCallBack, mCaptureFlags);
338 if (mCaptureCallBack != NULL &&
339 (mCaptureFlags & (CAPTURE_WAVEFORM|CAPTURE_FFT)) &&
340 mCaptureSize != 0) {
341 uint8_t waveform[mCaptureSize];
342 status_t status = getWaveForm(waveform);
343 if (status != NO_ERROR) {
344 return;
345 }
346 uint8_t fft[mCaptureSize];
347 if (mCaptureFlags & CAPTURE_FFT) {
348 status = doFft(fft, waveform);
349 }
350 if (status != NO_ERROR) {
351 return;
352 }
353 uint8_t *wavePtr = NULL;
354 uint8_t *fftPtr = NULL;
355 uint32_t waveSize = 0;
356 uint32_t fftSize = 0;
357 if (mCaptureFlags & CAPTURE_WAVEFORM) {
358 wavePtr = waveform;
359 waveSize = mCaptureSize;
360 }
361 if (mCaptureFlags & CAPTURE_FFT) {
362 fftPtr = fft;
363 fftSize = mCaptureSize;
364 }
365 mCaptureCallBack(mCaptureCbkUser, waveSize, wavePtr, fftSize, fftPtr, mSampleRate);
366 }
367}
368
369uint32_t Visualizer::initCaptureSize()
370{
371 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
372 effect_param_t *p = (effect_param_t *)buf32;
373
374 p->psize = sizeof(uint32_t);
375 p->vsize = sizeof(uint32_t);
Eric Laurent6d8b6942011-06-24 07:01:31 -0700376 *(int32_t *)p->data = VISUALIZER_PARAM_CAPTURE_SIZE;
Eric Laurentda7581b2010-07-02 08:12:41 -0700377 status_t status = getParameter(p);
378
379 if (status == NO_ERROR) {
380 status = p->status;
381 }
382
383 uint32_t size = 0;
384 if (status == NO_ERROR) {
385 size = *((int32_t *)p->data + 1);
386 }
387 mCaptureSize = size;
388
Steve Block3856b092011-10-20 11:56:00 +0100389 ALOGV("initCaptureSize size %d status %d", mCaptureSize, status);
Eric Laurentda7581b2010-07-02 08:12:41 -0700390
391 return size;
392}
393
Jean-Michel Trivi3476de62012-04-15 17:15:07 -0700394void Visualizer::controlStatusChanged(bool controlGranted) {
395 if (controlGranted) {
396 // this Visualizer instance regained control of the effect, reset the scaling mode
397 // and capture size as has been cached through it.
398 ALOGV("controlStatusChanged(true) causes effect parameter reset:");
399 ALOGV(" scaling mode reset to %d", mScalingMode);
400 setScalingMode(mScalingMode);
401 ALOGV(" capture size reset to %d", mCaptureSize);
402 setCaptureSize(mCaptureSize);
403 }
404 AudioEffect::controlStatusChanged(controlGranted);
405}
406
Eric Laurentda7581b2010-07-02 08:12:41 -0700407//-------------------------------------------------------------------------
408
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700409Visualizer::CaptureThread::CaptureThread(Visualizer& receiver, uint32_t captureRate,
410 bool bCanCallJava)
Eric Laurentda7581b2010-07-02 08:12:41 -0700411 : Thread(bCanCallJava), mReceiver(receiver)
412{
413 mSleepTimeUs = 1000000000 / captureRate;
Steve Block3856b092011-10-20 11:56:00 +0100414 ALOGV("CaptureThread cstor %p captureRate %d mSleepTimeUs %d", this, captureRate, mSleepTimeUs);
Eric Laurentda7581b2010-07-02 08:12:41 -0700415}
416
417bool Visualizer::CaptureThread::threadLoop()
418{
Steve Block3856b092011-10-20 11:56:00 +0100419 ALOGV("CaptureThread %p enter", this);
Eric Laurentda7581b2010-07-02 08:12:41 -0700420 while (!exitPending())
421 {
422 usleep(mSleepTimeUs);
423 mReceiver.periodicCapture();
424 }
Steve Block3856b092011-10-20 11:56:00 +0100425 ALOGV("CaptureThread %p exiting", this);
Eric Laurentda7581b2010-07-02 08:12:41 -0700426 return false;
427}
428
Glenn Kasten40bc9062015-03-20 09:09:33 -0700429} // namespace android