blob: 0bc82508f44654084895ec50950bc55f8bd5b1cc [file] [log] [blame]
henrika8324b522015-03-27 10:56:23 +01001/*
2 * Copyright (c) 2015 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#ifndef WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_MANAGER_H_
12#define WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_MANAGER_H_
13
14#include <jni.h>
15
henrikab2619892015-05-18 16:49:16 +020016#include "webrtc/base/scoped_ptr.h"
henrika8324b522015-03-27 10:56:23 +010017#include "webrtc/base/thread_checker.h"
18#include "webrtc/modules/audio_device/android/audio_common.h"
henrikaba35d052015-07-14 17:04:08 +020019#include "webrtc/modules/audio_device/audio_device_config.h"
henrika8324b522015-03-27 10:56:23 +010020#include "webrtc/modules/audio_device/include/audio_device_defines.h"
21#include "webrtc/modules/audio_device/audio_device_generic.h"
22#include "webrtc/modules/utility/interface/helpers_android.h"
henrikab2619892015-05-18 16:49:16 +020023#include "webrtc/modules/utility/interface/jvm_android.h"
henrika8324b522015-03-27 10:56:23 +010024
25namespace webrtc {
26
henrika8324b522015-03-27 10:56:23 +010027// Implements support for functions in the WebRTC audio stack for Android that
28// relies on the AudioManager in android.media. It also populates an
29// AudioParameter structure with native audio parameters detected at
30// construction. This class does not make any audio-related modifications
31// unless Init() is called. Caching audio parameters makes no changes but only
32// reads data from the Java side.
henrika8324b522015-03-27 10:56:23 +010033class AudioManager {
34 public:
henrikab2619892015-05-18 16:49:16 +020035 // Wraps the Java specific parts of the AudioManager into one helper class.
36 // Stores method IDs for all supported methods at construction and then
37 // allows calls like JavaAudioManager::Close() while hiding the Java/JNI
38 // parts that are associated with this call.
39 class JavaAudioManager {
40 public:
41 JavaAudioManager(NativeRegistration* native_registration,
42 rtc::scoped_ptr<GlobalRef> audio_manager);
henrika796e1722015-05-28 14:18:33 +020043 ~JavaAudioManager();
henrikab2619892015-05-18 16:49:16 +020044
henrika796e1722015-05-28 14:18:33 +020045 bool Init();
46 void Close();
henrikafe55c382015-06-05 11:45:56 +020047 bool IsCommunicationModeEnabled();
henrika8a897182015-06-09 10:45:09 +020048 bool IsDeviceBlacklistedForOpenSLESUsage();
henrikab2619892015-05-18 16:49:16 +020049
henrika796e1722015-05-28 14:18:33 +020050 private:
51 rtc::scoped_ptr<GlobalRef> audio_manager_;
52 jmethodID init_;
53 jmethodID dispose_;
henrikafe55c382015-06-05 11:45:56 +020054 jmethodID is_communication_mode_enabled_;
henrika8a897182015-06-09 10:45:09 +020055 jmethodID is_device_blacklisted_for_open_sles_usage_;
henrikab2619892015-05-18 16:49:16 +020056 };
henrika8324b522015-03-27 10:56:23 +010057
58 AudioManager();
59 ~AudioManager();
60
henrikab2619892015-05-18 16:49:16 +020061 // Sets the currently active audio layer combination. Must be called before
62 // Init().
63 void SetActiveAudioLayer(AudioDeviceModule::AudioLayer audio_layer);
64
henrika09bf1a12015-04-10 11:46:55 +020065 // Initializes the audio manager and stores the current audio mode.
henrika8324b522015-03-27 10:56:23 +010066 bool Init();
67 // Revert any setting done by Init().
68 bool Close();
69
henrikafe55c382015-06-05 11:45:56 +020070 // Returns true if current audio mode is AudioManager.MODE_IN_COMMUNICATION.
71 bool IsCommunicationModeEnabled() const;
henrika09bf1a12015-04-10 11:46:55 +020072
henrika8324b522015-03-27 10:56:23 +010073 // Native audio parameters stored during construction.
henrikab2619892015-05-18 16:49:16 +020074 const AudioParameters& GetPlayoutAudioParameters();
75 const AudioParameters& GetRecordAudioParameters();
henrika8324b522015-03-27 10:56:23 +010076
henrikab2619892015-05-18 16:49:16 +020077 // Returns true if the device supports a built-in Acoustic Echo Canceler.
78 // Some devices can also be blacklisted for use in combination with an AEC
79 // and these devices will return false.
80 // Can currently only be used in combination with a Java based audio backend
81 // for the recoring side (i.e. using the android.media.AudioRecord API).
82 bool IsAcousticEchoCancelerSupported() const;
83
84 // Returns true if the device supports the low-latency audio paths in
85 // combination with OpenSL ES.
86 bool IsLowLatencyPlayoutSupported() const;
87
88 // Returns the estimated total delay of this device. Unit is in milliseconds.
89 // The vaule is set once at construction and never changes after that.
90 // Possible values are webrtc::kLowLatencyModeDelayEstimateInMilliseconds and
91 // webrtc::kHighLatencyModeDelayEstimateInMilliseconds.
92 int GetDelayEstimateInMilliseconds() const;
henrika8324b522015-03-27 10:56:23 +010093
94 private:
95 // Called from Java side so we can cache the native audio parameters.
96 // This method will be called by the WebRtcAudioManager constructor, i.e.
97 // on the same thread that this object is created on.
henrikab2619892015-05-18 16:49:16 +020098 static void JNICALL CacheAudioParameters(JNIEnv* env,
99 jobject obj,
100 jint sample_rate,
101 jint channels,
102 jboolean hardware_aec,
103 jboolean low_latency_output,
104 jint output_buffer_size,
105 jint input_buffer_size,
106 jlong native_audio_manager);
107 void OnCacheAudioParameters(JNIEnv* env,
108 jint sample_rate,
109 jint channels,
110 jboolean hardware_aec,
111 jboolean low_latency_output,
112 jint output_buffer_size,
113 jint input_buffer_size);
henrika8324b522015-03-27 10:56:23 +0100114
115 // Stores thread ID in the constructor.
116 // We can then use ThreadChecker::CalledOnValidThread() to ensure that
117 // other methods are called from the same thread.
118 rtc::ThreadChecker thread_checker_;
119
henrikab2619892015-05-18 16:49:16 +0200120 // Calls AttachCurrentThread() if this thread is not attached at construction.
121 // Also ensures that DetachCurrentThread() is called at destruction.
122 AttachCurrentThreadIfNeeded attach_thread_if_needed_;
123
henrikaee369e42015-05-25 10:11:27 +0200124 // Wraps the JNI interface pointer and methods associated with it.
henrikab2619892015-05-18 16:49:16 +0200125 rtc::scoped_ptr<JNIEnvironment> j_environment_;
126
henrikaee369e42015-05-25 10:11:27 +0200127 // Contains factory method for creating the Java object.
henrikab2619892015-05-18 16:49:16 +0200128 rtc::scoped_ptr<NativeRegistration> j_native_registration_;
129
henrikaee369e42015-05-25 10:11:27 +0200130 // Wraps the Java specific parts of the AudioManager.
henrikab2619892015-05-18 16:49:16 +0200131 rtc::scoped_ptr<AudioManager::JavaAudioManager> j_audio_manager_;
132
133 AudioDeviceModule::AudioLayer audio_layer_;
henrika8324b522015-03-27 10:56:23 +0100134
135 // Set to true by Init() and false by Close().
136 bool initialized_;
137
henrikab2619892015-05-18 16:49:16 +0200138 // True if device supports hardware (or built-in) AEC.
139 bool hardware_aec_;
140
141 // True if device supports the low-latency OpenSL ES audio path.
142 bool low_latency_playout_;
143
144 // The delay estimate can take one of two fixed values depending on if the
145 // device supports low-latency output or not.
146 int delay_estimate_in_milliseconds_;
147
henrika8324b522015-03-27 10:56:23 +0100148 // Contains native parameters (e.g. sample rate, channel configuration).
149 // Set at construction in OnCacheAudioParameters() which is called from
150 // Java on the same thread as this object is created on.
151 AudioParameters playout_parameters_;
152 AudioParameters record_parameters_;
153};
154
155} // namespace webrtc
156
157#endif // WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_MANAGER_H_