Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2011 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 android.media.cts;
     18 
     19 import android.content.pm.PackageManager;
     20 import android.media.AudioFormat;
     21 import android.media.AudioRecord;
     22 import android.media.MediaRecorder.AudioSource;
     23 import android.test.AndroidTestCase;
     24 import android.util.Log;
     25 
     26 import com.android.compatibility.common.util.PollingCheck;
     27 
     28 import java.util.ArrayList;
     29 import java.util.List;
     30 
     31 public class AudioRecord_BufferSizeTest extends AndroidTestCase {
     32 
     33     private static final String TAG = AudioRecord_BufferSizeTest.class.getSimpleName();
     34     private static final int CHANNEL_CONFIG = AudioFormat.CHANNEL_IN_MONO;
     35     private static final int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
     36 
     37     private static final int[] SAMPLE_RATES_IN_HZ = new int[] {
     38         8000,
     39         11025,
     40         16000,
     41         44100,
     42     };
     43 
     44     private AudioRecord mAudioRecord;
     45 
     46     public void testGetMinBufferSize() throws Exception {
     47         if (!hasMicrophone()) {
     48             return;
     49         }
     50         List<Integer> failedSampleRates = new ArrayList<Integer>();
     51         for (int i = 0; i < SAMPLE_RATES_IN_HZ.length; i++) {
     52             try {
     53                 record(SAMPLE_RATES_IN_HZ[i]);
     54             } catch (Throwable e) {
     55                 Log.e(TAG, "Sample rate: " + SAMPLE_RATES_IN_HZ[i], e);
     56                 failedSampleRates.add(SAMPLE_RATES_IN_HZ[i]);
     57                 if (mAudioRecord != null) {
     58                     // clean up.  AudioRecords are in scarce supply.
     59                     mAudioRecord.release();
     60                     mAudioRecord = null;
     61                 }
     62             }
     63         }
     64         assertTrue("Failed sample rates: " + failedSampleRates + " See log for more details.",
     65                 failedSampleRates.isEmpty());
     66     }
     67 
     68     private void record(int sampleRateInHz) {
     69         int bufferSize = AudioRecord.getMinBufferSize(sampleRateInHz, CHANNEL_CONFIG, AUDIO_FORMAT);
     70         assertTrue(bufferSize > 0);
     71 
     72         createAudioRecord(sampleRateInHz, bufferSize);
     73         // RecordingState changes are reflected synchronously (no need to poll)
     74         assertEquals(AudioRecord.RECORDSTATE_STOPPED, mAudioRecord.getRecordingState());
     75 
     76         mAudioRecord.startRecording();
     77         assertEquals(AudioRecord.RECORDSTATE_RECORDING, mAudioRecord.getRecordingState());
     78 
     79         // it is preferred to use a short array to read AudioFormat.ENCODING_PCM_16BIT data
     80         // but it's ok to read using using a byte array.  16 bit PCM data will be
     81         // stored as two bytes, native endian.
     82         byte[] buffer = new byte[bufferSize];
     83         assertTrue(mAudioRecord.read(buffer, 0, bufferSize) > 0);
     84 
     85         mAudioRecord.stop();
     86         assertEquals(AudioRecord.RECORDSTATE_STOPPED, mAudioRecord.getRecordingState());
     87 
     88         mAudioRecord.release();
     89         mAudioRecord = null;
     90     }
     91 
     92     private void createAudioRecord(final int sampleRateInHz, final int bufferSize) {
     93         mAudioRecord = new AudioRecord(AudioSource.DEFAULT, sampleRateInHz,
     94                 CHANNEL_CONFIG, AUDIO_FORMAT, bufferSize);
     95         assertNotNull(mAudioRecord);
     96     }
     97 
     98     private boolean hasMicrophone() {
     99         return getContext().getPackageManager().hasSystemFeature(
    100                 PackageManager.FEATURE_MICROPHONE);
    101     }
    102 }
    103