Home | History | Annotate | Download | only in media
      1 /*
      2  * Copyright (C) 2015 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.support.car.media;
     17 
     18 import static androidx.annotation.RestrictTo.Scope.GROUP_ID;
     19 
     20 import android.media.AudioFormat;
     21 import android.media.AudioRecord;
     22 import android.support.car.CarNotConnectedException;
     23 
     24 import androidx.annotation.RestrictTo;
     25 
     26 /**
     27  * @hide
     28  */
     29 @RestrictTo(GROUP_ID)
     30 public class CarAudioManagerEmbedded extends CarAudioManager {
     31 
     32     private static final int MAX_BUFFER_SIZE_BYTE = 512 * 1024;
     33     private static final int SAMPLING_RATE = 16000;
     34     private static final AudioFormat AUDIO_RECORD_FORMAT = new AudioFormat.Builder()
     35             .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
     36             .setChannelMask(AudioFormat.CHANNEL_IN_MONO)
     37             .setSampleRate(SAMPLING_RATE)
     38             .build();
     39 
     40     private final android.car.media.CarAudioManager mManager;
     41 
     42     public CarAudioManagerEmbedded(Object manager) {
     43         mManager = (android.car.media.CarAudioManager) manager;
     44     }
     45 
     46     @Override
     47     public boolean isAudioRecordSupported() throws CarNotConnectedException {
     48         //always true in embedded
     49         return true;
     50     }
     51 
     52     @Override
     53     public AudioFormat getAudioRecordAudioFormat() throws CarNotConnectedException {
     54         return AUDIO_RECORD_FORMAT;
     55     }
     56 
     57     @Override
     58     public int getAudioRecordMinBufferSize() throws CarNotConnectedException {
     59         return AudioRecord.getMinBufferSize(SAMPLING_RATE, AUDIO_RECORD_FORMAT.getChannelMask(),
     60                 AUDIO_RECORD_FORMAT.getEncoding());
     61     }
     62 
     63     @Override
     64     public int getAudioRecordMaxBufferSize() throws CarNotConnectedException {
     65         return Math.max(getAudioRecordMinBufferSize(), MAX_BUFFER_SIZE_BYTE);
     66     }
     67 
     68     @Override
     69     public CarAudioRecord createCarAudioRecord(int bufferSize)
     70             throws CarNotConnectedException, SecurityException {
     71         if (bufferSize < getAudioRecordMinBufferSize() ||
     72             bufferSize > getAudioRecordMaxBufferSize()) {
     73             throw new IllegalArgumentException("Bad bufferSize value");
     74         }
     75         return new CarAudioRecordEmbedded(AUDIO_RECORD_FORMAT, bufferSize);
     76     }
     77 
     78     @Override
     79     public void onCarDisconnected() {
     80         //nothing to do
     81     }
     82 }
     83