1/*
2 *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11// The functions in this file are called from native code. They can still be
12// accessed even though they are declared private.
13
14package org.webrtc.voiceengine;
15
16import android.content.Context;
17import android.content.pm.PackageManager;
18import android.media.AudioManager;
19
20class AudioManagerAndroid {
21  // Most of Google lead devices use 44.1K as the default sampling rate, 44.1K
22  // is also widely used on other android devices.
23  private static final int DEFAULT_SAMPLING_RATE = 44100;
24  // Randomly picked up frame size which is close to return value on N4.
25  // Return this default value when
26  // getProperty(PROPERTY_OUTPUT_FRAMES_PER_BUFFER) fails.
27  private static final int DEFAULT_FRAMES_PER_BUFFER = 256;
28
29  private int mNativeOutputSampleRate;
30  private boolean mAudioLowLatencySupported;
31  private int mAudioLowLatencyOutputFrameSize;
32
33
34  @SuppressWarnings("unused")
35  private AudioManagerAndroid(Context context) {
36    AudioManager audioManager = (AudioManager)
37        context.getSystemService(Context.AUDIO_SERVICE);
38
39    mNativeOutputSampleRate = DEFAULT_SAMPLING_RATE;
40    mAudioLowLatencyOutputFrameSize = DEFAULT_FRAMES_PER_BUFFER;
41    if (android.os.Build.VERSION.SDK_INT >=
42        android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
43      String sampleRateString = audioManager.getProperty(
44          AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE);
45      if (sampleRateString != null) {
46        mNativeOutputSampleRate = Integer.parseInt(sampleRateString);
47      }
48      String framesPerBuffer = audioManager.getProperty(
49          AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER);
50      if (framesPerBuffer != null) {
51          mAudioLowLatencyOutputFrameSize = Integer.parseInt(framesPerBuffer);
52      }
53    }
54    mAudioLowLatencySupported = context.getPackageManager().hasSystemFeature(
55        PackageManager.FEATURE_AUDIO_LOW_LATENCY);
56  }
57
58    @SuppressWarnings("unused")
59    private int getNativeOutputSampleRate() {
60      return mNativeOutputSampleRate;
61    }
62
63    @SuppressWarnings("unused")
64    private boolean isAudioLowLatencySupported() {
65        return mAudioLowLatencySupported;
66    }
67
68    @SuppressWarnings("unused")
69    private int getAudioLowLatencyOutputFrameSize() {
70        return mAudioLowLatencyOutputFrameSize;
71    }
72}