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 android.media.AudioAttributes;
     19 import android.media.AudioFormat;
     20 import android.media.AudioManager.OnAudioFocusChangeListener;
     21 import android.media.AudioRecord;
     22 import android.support.annotation.RestrictTo;
     23 import android.support.car.CarNotConnectedException;
     24 
     25 import static android.support.annotation.RestrictTo.Scope.GROUP_ID;
     26 
     27 /**
     28  * @hide
     29  */
     30 @RestrictTo(GROUP_ID)
     31 public class CarAudioManagerEmbedded extends CarAudioManager {
     32 
     33     private static final int MAX_BUFFER_SIZE_BYTE = 512 * 1024;
     34     private static final int SAMPLING_RATE = 16000;
     35     private static final AudioFormat AUDIO_RECORD_FORMAT = new AudioFormat.Builder()
     36             .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
     37             .setChannelMask(AudioFormat.CHANNEL_IN_MONO)
     38             .setSampleRate(SAMPLING_RATE)
     39             .build();
     40 
     41     private final android.car.media.CarAudioManager mManager;
     42 
     43     public CarAudioManagerEmbedded(Object manager) {
     44         mManager = (android.car.media.CarAudioManager) manager;
     45     }
     46 
     47     @Override
     48     public AudioAttributes getAudioAttributesForCarUsage(@CarAudioUsage int carUsage)
     49             throws CarNotConnectedException {
     50         try {
     51             return mManager.getAudioAttributesForCarUsage(carUsage);
     52         } catch (android.car.CarNotConnectedException e) {
     53             throw new CarNotConnectedException(e);
     54         }
     55     }
     56 
     57     @Override
     58     public int requestAudioFocus(OnAudioFocusChangeListener listener,
     59             AudioAttributes requestAttributes,
     60             int durationHint,
     61             int flags) throws CarNotConnectedException, IllegalArgumentException {
     62         try {
     63             return mManager.requestAudioFocus(listener, requestAttributes, durationHint, flags);
     64         } catch (android.car.CarNotConnectedException e) {
     65             throw new CarNotConnectedException(e);
     66         }
     67     }
     68 
     69     @Override
     70     public int requestAudioFocus(OnAudioFocusChangeListener listener,
     71             AudioAttributes requestAttributes,
     72             int durationHint) throws CarNotConnectedException, IllegalArgumentException {
     73         try {
     74             return mManager.requestAudioFocus(listener, requestAttributes, durationHint,
     75                     0 /*flags*/);
     76         } catch (android.car.CarNotConnectedException e) {
     77             throw new CarNotConnectedException(e);
     78         }
     79     }
     80 
     81     @Override
     82     public void abandonAudioFocus(OnAudioFocusChangeListener listener, AudioAttributes aa) {
     83         mManager.abandonAudioFocus(listener, aa);
     84     }
     85 
     86     @Override
     87     public boolean isAudioRecordSupported() throws CarNotConnectedException {
     88         //always true in embedded
     89         return true;
     90     }
     91 
     92     @Override
     93     public AudioFormat getAudioRecordAudioFormat() throws CarNotConnectedException {
     94         return AUDIO_RECORD_FORMAT;
     95     }
     96 
     97     @Override
     98     public int getAudioRecordMinBufferSize() throws CarNotConnectedException {
     99         return AudioRecord.getMinBufferSize(SAMPLING_RATE, AUDIO_RECORD_FORMAT.getChannelMask(),
    100                 AUDIO_RECORD_FORMAT.getEncoding());
    101     }
    102 
    103     @Override
    104     public int getAudioRecordMaxBufferSize() throws CarNotConnectedException {
    105         return Math.max(getAudioRecordMinBufferSize(), MAX_BUFFER_SIZE_BYTE);
    106     }
    107 
    108     @Override
    109     public CarAudioRecord createCarAudioRecord(int bufferSize)
    110             throws CarNotConnectedException, SecurityException {
    111         if (bufferSize < getAudioRecordMinBufferSize() ||
    112             bufferSize > getAudioRecordMaxBufferSize()) {
    113             throw new IllegalArgumentException("Bad bufferSize value");
    114         }
    115         return new CarAudioRecordEmbedded(AUDIO_RECORD_FORMAT, bufferSize);
    116     }
    117 
    118     @Override
    119     public boolean isMediaMuted() throws CarNotConnectedException {
    120         try {
    121             return mManager.isMediaMuted();
    122         } catch (android.car.CarNotConnectedException e) {
    123             throw new CarNotConnectedException(e);
    124         }
    125     }
    126 
    127     @Override
    128     public boolean setMediaMute(boolean mute) throws CarNotConnectedException {
    129         try {
    130             return mManager.setMediaMute(mute);
    131         } catch (android.car.CarNotConnectedException e) {
    132             throw new CarNotConnectedException(e);
    133         }
    134     }
    135 
    136     @Override
    137     public void onCarDisconnected() {
    138         //nothing to do
    139     }
    140 }
    141