Home | History | Annotate | Download | only in functional
      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 
     17 package com.android.mediaframeworktest.functional;
     18 
     19 import com.android.mediaframeworktest.MediaFrameworkTest;
     20 import com.android.mediaframeworktest.MediaNames;
     21 import android.content.Context;
     22 import android.content.res.AssetFileDescriptor;
     23 import android.media.audiofx.AudioEffect;
     24 import android.media.AudioManager;
     25 import android.media.audiofx.Virtualizer;
     26 import android.media.audiofx.Visualizer;
     27 import android.media.MediaPlayer;
     28 
     29 import android.os.Looper;
     30 import android.test.suitebuilder.annotation.LargeTest;
     31 import android.test.suitebuilder.annotation.MediumTest;
     32 import android.test.suitebuilder.annotation.Suppress;
     33 import android.test.ActivityInstrumentationTestCase2;
     34 import android.util.Log;
     35 
     36 import java.nio.ByteOrder;
     37 import java.nio.ByteBuffer;
     38 import java.util.UUID;
     39 
     40 /**
     41  * Junit / Instrumentation test case for the media AudioTrack api
     42 
     43  */
     44 public class MediaVirtualizerTest extends ActivityInstrumentationTestCase2<MediaFrameworkTest> {
     45     private String TAG = "MediaVirtualizerTest";
     46     private final static int MIN_ENERGY_RATIO_2 = 3;
     47     private final static short TEST_STRENGTH = 500;
     48     private final static int TEST_VOLUME = 4;
     49 
     50     private Virtualizer mVirtualizer = null;
     51     private int mSession = -1;
     52 
     53     public MediaVirtualizerTest() {
     54         super("com.android.mediaframeworktest", MediaFrameworkTest.class);
     55     }
     56 
     57     @Override
     58     protected void setUp() throws Exception {
     59       super.setUp();
     60     }
     61 
     62     @Override
     63     protected void tearDown() throws Exception {
     64         super.tearDown();
     65         releaseVirtualizer();
     66     }
     67 
     68     private static void assumeTrue(String message, boolean cond) {
     69         assertTrue("(assume)"+message, cond);
     70     }
     71 
     72     private void log(String testName, String message) {
     73         Log.v(TAG, "["+testName+"] "+message);
     74     }
     75 
     76     private void loge(String testName, String message) {
     77         Log.e(TAG, "["+testName+"] "+message);
     78     }
     79 
     80     //-----------------------------------------------------------------
     81     // VIRTUALIZER TESTS:
     82     //----------------------------------
     83 
     84 
     85     //-----------------------------------------------------------------
     86     // 0 - constructor
     87     //----------------------------------
     88 
     89     //Test case 0.0: test constructor and release
     90     @LargeTest
     91     public void test0_0ConstructorAndRelease() throws Exception {
     92         boolean result = false;
     93         String msg = "test1_0ConstructorAndRelease()";
     94         Virtualizer virtualizer = null;
     95          try {
     96             virtualizer = new Virtualizer(0, 0);
     97             assertNotNull(msg + ": could not create Virtualizer", virtualizer);
     98             try {
     99                 assertTrue(msg +": invalid effect ID", (virtualizer.getId() != 0));
    100             } catch (IllegalStateException e) {
    101                 msg = msg.concat(": Virtualizer not initialized");
    102             }
    103             result = true;
    104         } catch (IllegalArgumentException e) {
    105             msg = msg.concat(": Virtualizer not found");
    106         } catch (UnsupportedOperationException e) {
    107             msg = msg.concat(": Effect library not loaded");
    108         } finally {
    109             if (virtualizer != null) {
    110                 virtualizer.release();
    111             }
    112         }
    113         assertTrue(msg, result);
    114     }
    115 
    116 
    117     //-----------------------------------------------------------------
    118     // 1 - get/set parameters
    119     //----------------------------------
    120 
    121     //Test case 1.0: test strength
    122     @LargeTest
    123     public void test1_0Strength() throws Exception {
    124         boolean result = false;
    125         String msg = "test1_0Strength()";
    126         getVirtualizer(0);
    127         try {
    128             if (mVirtualizer.getStrengthSupported()) {
    129                 mVirtualizer.setStrength((short)TEST_STRENGTH);
    130                 short strength = mVirtualizer.getRoundedStrength();
    131                 // allow 10% difference between set strength and rounded strength
    132                 assertTrue(msg +": got incorrect strength",
    133                         ((float)strength > (float)TEST_STRENGTH * 0.9f) &&
    134                         ((float)strength < (float)TEST_STRENGTH * 1.1f));
    135             } else {
    136                 short strength = mVirtualizer.getRoundedStrength();
    137                 assertTrue(msg +": got incorrect strength", strength >= 0 && strength <= 1000);
    138             }
    139             result = true;
    140         } catch (IllegalArgumentException e) {
    141             msg = msg.concat(": Bad parameter value");
    142             loge(msg, "Bad parameter value");
    143         } catch (UnsupportedOperationException e) {
    144             msg = msg.concat(": get parameter() rejected");
    145             loge(msg, "get parameter() rejected");
    146         } catch (IllegalStateException e) {
    147             msg = msg.concat("get parameter() called in wrong state");
    148             loge(msg, "get parameter() called in wrong state");
    149         } finally {
    150             releaseVirtualizer();
    151         }
    152         assertTrue(msg, result);
    153     }
    154 
    155     //Test case 1.1: test properties
    156     @LargeTest
    157     public void test1_1Properties() throws Exception {
    158         boolean result = false;
    159         String msg = "test1_1Properties()";
    160         getVirtualizer(0);
    161         try {
    162             Virtualizer.Settings settings = mVirtualizer.getProperties();
    163             String str = settings.toString();
    164             settings = new Virtualizer.Settings(str);
    165             mVirtualizer.setProperties(settings);
    166             result = true;
    167         } catch (IllegalArgumentException e) {
    168             msg = msg.concat(": Bad parameter value");
    169             loge(msg, "Bad parameter value");
    170         } catch (UnsupportedOperationException e) {
    171             msg = msg.concat(": get parameter() rejected");
    172             loge(msg, "get parameter() rejected");
    173         } catch (IllegalStateException e) {
    174             msg = msg.concat("get parameter() called in wrong state");
    175             loge(msg, "get parameter() called in wrong state");
    176         } finally {
    177             releaseVirtualizer();
    178         }
    179         assertTrue(msg, result);
    180     }
    181 
    182     //-----------------------------------------------------------------
    183     // 2 - Effect action
    184     //----------------------------------
    185 
    186     //Test case 2.0: test actual virtualizer influence on sound
    187     @LargeTest
    188     public void test2_0SoundModification() throws Exception {
    189         boolean result = false;
    190         String msg = "test2_0SoundModification()";
    191         EnergyProbe probe = null;
    192         AudioEffect vc = null;
    193         MediaPlayer mp = null;
    194         AudioManager am = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
    195         int volume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    196         am.setStreamVolume(AudioManager.STREAM_MUSIC,
    197                            TEST_VOLUME,
    198                            0);
    199 
    200         try {
    201             probe = new EnergyProbe(0);
    202             // creating a volume controller on output mix ensures that ro.audio.silent mutes
    203             // audio after the effects and not before
    204             vc = new AudioEffect(
    205                     AudioEffect.EFFECT_TYPE_NULL,
    206                     UUID.fromString("119341a0-8469-11df-81f9-0002a5d5c51b"),
    207                       0,
    208                       0);
    209             vc.setEnabled(true);
    210 
    211             mp = new MediaPlayer();
    212             mp.setDataSource(MediaNames.SINE_200_1000);
    213             mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
    214             getVirtualizer(mp.getAudioSessionId());
    215             mp.prepare();
    216             mp.start();
    217             Thread.sleep(200);
    218             // measure reference energy around 1kHz
    219             int refEnergy200 = probe.capture(200);
    220             int refEnergy1000 = probe.capture(1000);
    221             mVirtualizer.setStrength((short)1000);
    222             mVirtualizer.setEnabled(true);
    223             Thread.sleep(500);
    224             // measure energy around 1kHz with band level at min
    225             int energy200 = probe.capture(200);
    226             int energy1000 = probe.capture(1000);
    227             // verify that the energy ration between low and high frequencies is at least
    228             // MIN_ENERGY_RATIO_2 times higher with virtualizer on.
    229             // NOTE: this is what is observed with current virtualizer implementation and the test
    230             // audio file but is not the primary effect of the virtualizer. A better way would
    231             // be to have a stereo PCM capture and check that a strongly paned input is centered
    232             // when output. However, we cannot capture stereo with the visualizer.
    233             assertTrue(msg + ": virtiualizer has no effect",
    234                     ((float)energy200/(float)energy1000) >
    235                     (MIN_ENERGY_RATIO_2 * ((float)refEnergy200/(float)refEnergy1000)));
    236             result = true;
    237         } catch (IllegalArgumentException e) {
    238             msg = msg.concat(": Bad parameter value");
    239             loge(msg, "Bad parameter value");
    240         } catch (UnsupportedOperationException e) {
    241             msg = msg.concat(": get parameter() rejected");
    242             loge(msg, "get parameter() rejected");
    243         } catch (IllegalStateException e) {
    244             msg = msg.concat("get parameter() called in wrong state");
    245             loge(msg, "get parameter() called in wrong state");
    246         } catch (InterruptedException e) {
    247             loge(msg, "sleep() interrupted");
    248         }
    249         finally {
    250             releaseVirtualizer();
    251             if (mp != null) {
    252                 mp.release();
    253             }
    254             if (vc != null) {
    255                 vc.release();
    256             }
    257             if (probe != null) {
    258                 probe.release();
    259             }
    260             am.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
    261         }
    262         assertTrue(msg, result);
    263     }
    264     //-----------------------------------------------------------------
    265     // private methods
    266     //----------------------------------
    267 
    268     private void getVirtualizer(int session) {
    269          if (mVirtualizer == null || session != mSession) {
    270              if (session != mSession && mVirtualizer != null) {
    271                  mVirtualizer.release();
    272                  mVirtualizer = null;
    273              }
    274              try {
    275                 mVirtualizer = new Virtualizer(0, session);
    276                 mSession = session;
    277             } catch (IllegalArgumentException e) {
    278                 Log.e(TAG, "getVirtualizer() Virtualizer not found exception: "+e);
    279             } catch (UnsupportedOperationException e) {
    280                 Log.e(TAG, "getVirtualizer() Effect library not loaded exception: "+e);
    281             }
    282          }
    283          assertNotNull("could not create mVirtualizer", mVirtualizer);
    284     }
    285 
    286     private void releaseVirtualizer() {
    287         if (mVirtualizer != null) {
    288             mVirtualizer.release();
    289             mVirtualizer = null;
    290         }
    291    }
    292 
    293 }
    294 
    295