PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/cts/tests/tests/media/src/android/media/cts/VisualizerTest.java

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk
Java | 336 lines | 264 code | 29 blank | 43 comment | 38 complexity | dc38839b54e616d271286c0a0ea3a60a MD5 | raw file
  1. /*
  2. * Copyright (C) 2010 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. package android.media.cts;
  17. import android.media.audiofx.AudioEffect;
  18. import android.media.AudioFormat;
  19. import android.media.AudioManager;
  20. import android.media.audiofx.Visualizer;
  21. import android.os.Looper;
  22. import android.test.AndroidTestCase;
  23. import android.util.Log;
  24. public class VisualizerTest extends AndroidTestCase {
  25. private String TAG = "VisualizerTest";
  26. private final static int MIN_CAPTURE_RATE_MAX = 10000; // 10Hz
  27. private final static int MIN_CAPTURE_SIZE_MAX = 1024;
  28. private final static int MAX_CAPTURE_SIZE_MIN = 512;
  29. private final static int MAX_LOOPER_WAIT_COUNT = 10;
  30. private Visualizer mVisualizer = null;
  31. private int mSession = -1;
  32. private boolean mInitialized = false;
  33. private Looper mLooper = null;
  34. private final Object mLock = new Object();
  35. private byte[] mWaveform = null;
  36. private byte[] mFft = null;
  37. private boolean mCaptureWaveform = false;
  38. private boolean mCaptureFft = false;
  39. private Thread mListenerThread;
  40. //-----------------------------------------------------------------
  41. // VISUALIZER TESTS:
  42. //----------------------------------
  43. //-----------------------------------------------------------------
  44. // 0 - constructor
  45. //----------------------------------
  46. //Test case 0.0: test constructor and release
  47. public void test0_0ConstructorAndRelease() throws Exception {
  48. Visualizer visualizer = null;
  49. try {
  50. visualizer = new Visualizer(0);
  51. assertNotNull("could not create Visualizer", visualizer);
  52. } catch (IllegalArgumentException e) {
  53. fail("Visualizer not found");
  54. } catch (UnsupportedOperationException e) {
  55. fail("Effect library not loaded");
  56. } finally {
  57. if (visualizer != null) {
  58. visualizer.release();
  59. }
  60. }
  61. }
  62. //-----------------------------------------------------------------
  63. // 1 - get/set parameters
  64. //----------------------------------
  65. //Test case 1.0: capture rates
  66. public void test1_0CaptureRates() throws Exception {
  67. getVisualizer(0);
  68. try {
  69. int captureRate = mVisualizer.getMaxCaptureRate();
  70. assertTrue("insufficient max capture rate",
  71. captureRate >= MIN_CAPTURE_RATE_MAX);
  72. int samplingRate = mVisualizer.getSamplingRate();
  73. } catch (IllegalArgumentException e) {
  74. fail("Bad parameter value");
  75. } catch (UnsupportedOperationException e) {
  76. fail("get parameter() rejected");
  77. } catch (IllegalStateException e) {
  78. fail("get parameter() called in wrong state");
  79. } finally {
  80. releaseVisualizer();
  81. }
  82. }
  83. //Test case 1.1: test capture size
  84. public void test1_1CaptureSize() throws Exception {
  85. getVisualizer(0);
  86. try {
  87. int[] range = mVisualizer.getCaptureSizeRange();
  88. assertTrue("insufficient min capture size",
  89. range[0] <= MAX_CAPTURE_SIZE_MIN);
  90. assertTrue("insufficient min capture size",
  91. range[1] >= MIN_CAPTURE_SIZE_MAX);
  92. mVisualizer.setCaptureSize(range[0]);
  93. assertEquals("insufficient min capture size",
  94. range[0], mVisualizer.getCaptureSize());
  95. mVisualizer.setCaptureSize(range[1]);
  96. assertEquals("insufficient min capture size",
  97. range[1], mVisualizer.getCaptureSize());
  98. } catch (IllegalArgumentException e) {
  99. fail("Bad parameter value");
  100. } catch (UnsupportedOperationException e) {
  101. fail("get parameter() rejected");
  102. } catch (IllegalStateException e) {
  103. fail("get parameter() called in wrong state");
  104. } finally {
  105. releaseVisualizer();
  106. }
  107. }
  108. //-----------------------------------------------------------------
  109. // 2 - check capture
  110. //----------------------------------
  111. //Test case 2.0: test cature in polling mode
  112. public void test2_0PollingCapture() throws Exception {
  113. try {
  114. getVisualizer(0);
  115. mVisualizer.setEnabled(true);
  116. assertTrue("visualizer not enabled", mVisualizer.getEnabled());
  117. Thread.sleep(100);
  118. // check capture on silence
  119. byte[] data = new byte[mVisualizer.getCaptureSize()];
  120. mVisualizer.getWaveForm(data);
  121. int energy = computeEnergy(data, true);
  122. assertEquals("getWaveForm reports energy for silence",
  123. 0, energy);
  124. mVisualizer.getFft(data);
  125. energy = computeEnergy(data, false);
  126. assertEquals("getFft reports energy for silence",
  127. 0, energy);
  128. } catch (IllegalStateException e) {
  129. fail("method called in wrong state");
  130. } catch (InterruptedException e) {
  131. fail("sleep() interrupted");
  132. } finally {
  133. releaseVisualizer();
  134. }
  135. }
  136. //Test case 2.1: test capture with listener
  137. public void test2_1ListenerCapture() throws Exception {
  138. try {
  139. getVisualizer(0);
  140. synchronized(mLock) {
  141. mInitialized = false;
  142. createListenerLooper();
  143. waitForLooperInitialization_l();
  144. }
  145. mVisualizer.setEnabled(true);
  146. assertTrue("visualizer not enabled", mVisualizer.getEnabled());
  147. Thread.sleep(100);
  148. // check capture on silence
  149. synchronized(mLock) {
  150. mCaptureWaveform = true;
  151. int looperWaitCount = MAX_LOOPER_WAIT_COUNT;
  152. while ((mWaveform == null) && (looperWaitCount-- > 0)) {
  153. try {
  154. mLock.wait();
  155. } catch(Exception e) {
  156. }
  157. }
  158. mCaptureWaveform = false;
  159. }
  160. assertNotNull("waveform capture failed", mWaveform);
  161. int energy = computeEnergy(mWaveform, true);
  162. assertEquals("getWaveForm reports energy for silence",
  163. 0, energy);
  164. synchronized(mLock) {
  165. mCaptureFft = true;
  166. int looperWaitCount = MAX_LOOPER_WAIT_COUNT;
  167. while ((mFft == null) && (looperWaitCount-- > 0)) {
  168. try {
  169. mLock.wait();
  170. } catch(Exception e) {
  171. }
  172. }
  173. mCaptureFft = false;
  174. }
  175. assertNotNull("FFT capture failed", mFft);
  176. energy = computeEnergy(mFft, false);
  177. assertEquals("getFft reports energy for silence",
  178. 0, energy);
  179. } catch (IllegalStateException e) {
  180. fail("method called in wrong state");
  181. } catch (InterruptedException e) {
  182. fail("sleep() interrupted");
  183. } finally {
  184. terminateListenerLooper();
  185. releaseVisualizer();
  186. }
  187. }
  188. //-----------------------------------------------------------------
  189. // private methods
  190. //----------------------------------
  191. private int computeEnergy(byte[] data, boolean pcm) {
  192. int energy = 0;
  193. if (data.length != 0) {
  194. if (pcm) {
  195. for (int i = 0; i < data.length; i++) {
  196. int tmp = ((int)data[i] & 0xFF) - 128;
  197. energy += tmp*tmp;
  198. }
  199. } else {
  200. energy = (int)data[0] * (int)data[0];
  201. for (int i = 2; i < data.length; i += 2) {
  202. int real = (int)data[i];
  203. int img = (int)data[i + 1];
  204. energy += real * real + img * img;
  205. }
  206. }
  207. }
  208. return energy;
  209. }
  210. private void getVisualizer(int session) {
  211. if (mVisualizer == null || session != mSession) {
  212. if (session != mSession && mVisualizer != null) {
  213. mVisualizer.release();
  214. mVisualizer = null;
  215. }
  216. try {
  217. mVisualizer = new Visualizer(session);
  218. mSession = session;
  219. } catch (IllegalArgumentException e) {
  220. Log.e(TAG, "getVisualizer() Visualizer not found exception: "+e);
  221. } catch (UnsupportedOperationException e) {
  222. Log.e(TAG, "getVisualizer() Effect library not loaded exception: "+e);
  223. }
  224. }
  225. assertNotNull("could not create mVisualizer", mVisualizer);
  226. }
  227. private void releaseVisualizer() {
  228. if (mVisualizer != null) {
  229. mVisualizer.release();
  230. mVisualizer = null;
  231. }
  232. }
  233. private void waitForLooperInitialization_l() {
  234. int looperWaitCount = MAX_LOOPER_WAIT_COUNT;
  235. while (!mInitialized && (looperWaitCount-- > 0)) {
  236. try {
  237. mLock.wait();
  238. } catch(Exception e) {
  239. }
  240. }
  241. assertTrue(mInitialized);
  242. }
  243. private void createListenerLooper() {
  244. mListenerThread = new Thread() {
  245. @Override
  246. public void run() {
  247. // Set up a looper to be used by mEffect.
  248. Looper.prepare();
  249. // Save the looper so that we can terminate this thread
  250. // after we are done with it.
  251. mLooper = Looper.myLooper();
  252. synchronized(mLock) {
  253. if (mVisualizer != null) {
  254. mVisualizer.setDataCaptureListener(new Visualizer.OnDataCaptureListener() {
  255. public void onWaveFormDataCapture(
  256. Visualizer visualizer, byte[] waveform, int samplingRate) {
  257. synchronized(mLock) {
  258. if (visualizer == mVisualizer) {
  259. if (mCaptureWaveform) {
  260. mWaveform = waveform;
  261. mLock.notify();
  262. }
  263. }
  264. }
  265. }
  266. public void onFftDataCapture(
  267. Visualizer visualizer, byte[] fft, int samplingRate) {
  268. synchronized(mLock) {
  269. Log.e(TAG, "onFftDataCapture 2 mCaptureFft: "+mCaptureFft);
  270. if (visualizer == mVisualizer) {
  271. if (mCaptureFft) {
  272. mFft = fft;
  273. mLock.notify();
  274. }
  275. }
  276. }
  277. }
  278. },
  279. 10000,
  280. true,
  281. true);
  282. }
  283. mInitialized = true;
  284. mLock.notify();
  285. }
  286. Looper.loop(); // Blocks forever until Looper.quit() is called.
  287. }
  288. };
  289. mListenerThread.start();
  290. }
  291. /*
  292. * Terminates the listener looper thread.
  293. */
  294. private void terminateListenerLooper() {
  295. if (mListenerThread != null) {
  296. if (mLooper != null) {
  297. mLooper.quit();
  298. mLooper = null;
  299. }
  300. try {
  301. mListenerThread.join();
  302. } catch(InterruptedException e) {
  303. }
  304. mListenerThread = null;
  305. }
  306. }
  307. }