Delays changing to COMMUNICATION mode until streaming starts.
Restores stored audio mode when all streaming stops. TBR=glaznev BUG=NONE TEST=AppRTCDemo Review URL: https://webrtc-codereview.appspot.com/46869005 Cr-Commit-Position: refs/heads/master@{#8970}
This commit is contained in:
parent
0184057d54
commit
09bf1a169b
@ -118,6 +118,7 @@ class AudioDeviceTemplate : public AudioDeviceGeneric {
|
||||
}
|
||||
|
||||
int32_t InitPlayout() override {
|
||||
audio_manager_.SetCommunicationMode(true);
|
||||
return output_.InitPlayout();
|
||||
}
|
||||
|
||||
@ -131,6 +132,7 @@ class AudioDeviceTemplate : public AudioDeviceGeneric {
|
||||
}
|
||||
|
||||
int32_t InitRecording() override {
|
||||
audio_manager_.SetCommunicationMode(true);
|
||||
return input_.InitRecording();
|
||||
}
|
||||
|
||||
@ -143,7 +145,13 @@ class AudioDeviceTemplate : public AudioDeviceGeneric {
|
||||
}
|
||||
|
||||
int32_t StopPlayout() override {
|
||||
return output_.StopPlayout();
|
||||
int32_t err = output_.StopPlayout();
|
||||
if (!Recording()) {
|
||||
// Restore initial audio mode since all audio streaming is disabled.
|
||||
// The default mode was stored in Init().
|
||||
audio_manager_.SetCommunicationMode(false);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
bool Playing() const override {
|
||||
@ -155,7 +163,13 @@ class AudioDeviceTemplate : public AudioDeviceGeneric {
|
||||
}
|
||||
|
||||
int32_t StopRecording() override {
|
||||
return input_.StopRecording();
|
||||
int32_t err = input_.StopRecording();
|
||||
if (!Playing()) {
|
||||
// Restore initial audio mode since all audio streaming is disabled.
|
||||
// The default mode was is stored in Init().
|
||||
audio_manager_.SetCommunicationMode(false);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
bool Recording() const override {
|
||||
|
@ -121,6 +121,18 @@ bool AudioManager::Close() {
|
||||
return true;
|
||||
}
|
||||
|
||||
void AudioManager::SetCommunicationMode(bool enable) {
|
||||
ALOGD("SetCommunicationMode(%d)%s", enable, GetThreadInfo().c_str());
|
||||
DCHECK(thread_checker_.CalledOnValidThread());
|
||||
DCHECK(initialized_);
|
||||
AttachThreadScoped ats(g_jvm);
|
||||
JNIEnv* jni = ats.env();
|
||||
jmethodID setcommID = GetMethodID(
|
||||
jni, g_audio_manager_class, "setCommunicationMode", "(Z)V");
|
||||
jni->CallVoidMethod(j_audio_manager_, setcommID, enable);
|
||||
CHECK_EXCEPTION(jni);
|
||||
}
|
||||
|
||||
void JNICALL AudioManager::CacheAudioParameters(JNIEnv* env, jobject obj,
|
||||
jint sample_rate, jint channels, jlong nativeAudioManager) {
|
||||
webrtc::AudioManager* this_object =
|
||||
|
@ -81,14 +81,15 @@ class AudioManager {
|
||||
AudioManager();
|
||||
~AudioManager();
|
||||
|
||||
// Initializes the audio manager (changes mode to MODE_IN_COMMUNICATION,
|
||||
// request audio focus etc.).
|
||||
// It is possible to use this class without calling Init() if the calling
|
||||
// application prefers to set up the audio environment on its own instead.
|
||||
// Initializes the audio manager and stores the current audio mode.
|
||||
bool Init();
|
||||
// Revert any setting done by Init().
|
||||
bool Close();
|
||||
|
||||
// Sets audio mode to AudioManager.MODE_IN_COMMUNICATION if |enable| is true.
|
||||
// Restores audio mode that was stored in Init() if |enable| is false.
|
||||
void SetCommunicationMode(bool enable);
|
||||
|
||||
// Native audio parameters stored during construction.
|
||||
AudioParameters GetPlayoutAudioParameters() const;
|
||||
AudioParameters GetRecordAudioParameters() const;
|
||||
|
@ -37,6 +37,14 @@ class WebRtcAudioManager {
|
||||
// TODO(henrika): add stereo support for playout.
|
||||
private static final int CHANNELS = 1;
|
||||
|
||||
// List of possible audio modes.
|
||||
private static final String[] AUDIO_MODES = new String[] {
|
||||
"MODE_NORMAL",
|
||||
"MODE_RINGTONE",
|
||||
"MODE_IN_CALL",
|
||||
"MODE_IN_COMMUNICATION",
|
||||
};
|
||||
|
||||
private final long nativeAudioManager;
|
||||
private final Context context;
|
||||
private final AudioManager audioManager;
|
||||
@ -68,13 +76,11 @@ class WebRtcAudioManager {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Store current audio state so we can restore it when close() is called.
|
||||
// Store current audio state so we can restore it when close() or
|
||||
// setCommunicationMode(false) is called.
|
||||
savedAudioMode = audioManager.getMode();
|
||||
savedIsSpeakerPhoneOn = audioManager.isSpeakerphoneOn();
|
||||
|
||||
// Switch to COMMUNICATION mode for best possible VoIP performance.
|
||||
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
|
||||
|
||||
if (DEBUG) {
|
||||
Logd("savedAudioMode: " + savedAudioMode);
|
||||
Logd("savedIsSpeakerPhoneOn: " + savedIsSpeakerPhoneOn);
|
||||
@ -95,6 +101,24 @@ class WebRtcAudioManager {
|
||||
audioManager.setMode(savedAudioMode);
|
||||
}
|
||||
|
||||
private void setCommunicationMode(boolean enable) {
|
||||
Logd("setCommunicationMode(" + enable + ")"
|
||||
+ WebRtcAudioUtils.getThreadInfo());
|
||||
assertTrue(initialized);
|
||||
if (enable) {
|
||||
// Avoid switching mode if MODE_IN_COMMUNICATION is already in use.
|
||||
if (audioManager.getMode() == AudioManager.MODE_IN_COMMUNICATION) {
|
||||
return;
|
||||
}
|
||||
// Switch to COMMUNICATION mode for best possible VoIP performance.
|
||||
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
|
||||
} else {
|
||||
// Restore audio mode that was stored in init().
|
||||
audioManager.setMode(savedAudioMode);
|
||||
}
|
||||
Logd("changing audio mode to: " + AUDIO_MODES[audioManager.getMode()]);
|
||||
}
|
||||
|
||||
private void storeAudioParameters() {
|
||||
// Only mono is supported currently (in both directions).
|
||||
// TODO(henrika): add support for stereo playout.
|
||||
|
Loading…
x
Reference in New Issue
Block a user