diff --git a/sdk/android/BUILD.gn b/sdk/android/BUILD.gn index cd17b9a952..a90ac2f298 100644 --- a/sdk/android/BUILD.gn +++ b/sdk/android/BUILD.gn @@ -793,6 +793,7 @@ rtc_android_library("base_java") { "src/java/org/webrtc/audio/BuildInfo.java", "src/java/org/webrtc/audio/WebRtcAudioEffects.java", "src/java/org/webrtc/audio/WebRtcAudioManager.java", + "src/java/org/webrtc/audio/VolumeLogger.java", "src/java/org/webrtc/audio/WebRtcAudioRecord.java", "src/java/org/webrtc/audio/WebRtcAudioTrack.java", "src/java/org/webrtc/audio/WebRtcAudioUtils.java", diff --git a/sdk/android/src/java/org/webrtc/audio/VolumeLogger.java b/sdk/android/src/java/org/webrtc/audio/VolumeLogger.java new file mode 100644 index 0000000000..859d7c1659 --- /dev/null +++ b/sdk/android/src/java/org/webrtc/audio/VolumeLogger.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +package org.webrtc.audio; + +import android.media.AudioManager; +import java.util.Timer; +import java.util.TimerTask; +import javax.annotation.Nullable; +import org.webrtc.Logging; + +// TODO(magjed): Do we really need to spawn a new thread just to log volume? Can we re-use the +// AudioTrackThread instead? +/** + * Private utility class that periodically checks and logs the volume level of the audio stream that + * is currently controlled by the volume control. A timer triggers logs once every 30 seconds and + * the timer's associated thread is named "WebRtcVolumeLevelLoggerThread". + */ +class VolumeLogger { + private static final String TAG = "VolumeLogger"; + private static final String THREAD_NAME = "WebRtcVolumeLevelLoggerThread"; + private static final int TIMER_PERIOD_IN_SECONDS = 30; + + private final AudioManager audioManager; + private @Nullable Timer timer; + + public VolumeLogger(AudioManager audioManager) { + this.audioManager = audioManager; + } + + public void start() { + Logging.d(TAG, "start" + WebRtcAudioUtils.getThreadInfo()); + if (timer != null) { + return; + } + Logging.d(TAG, "audio mode is: " + WebRtcAudioUtils.modeToString(audioManager.getMode())); + + timer = new Timer(THREAD_NAME); + timer.schedule(new LogVolumeTask(audioManager.getStreamMaxVolume(AudioManager.STREAM_RING), + audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL)), + 0, TIMER_PERIOD_IN_SECONDS * 1000); + } + + private class LogVolumeTask extends TimerTask { + private final int maxRingVolume; + private final int maxVoiceCallVolume; + + LogVolumeTask(int maxRingVolume, int maxVoiceCallVolume) { + this.maxRingVolume = maxRingVolume; + this.maxVoiceCallVolume = maxVoiceCallVolume; + } + + @Override + public void run() { + final int mode = audioManager.getMode(); + if (mode == AudioManager.MODE_RINGTONE) { + Logging.d(TAG, + "STREAM_RING stream volume: " + audioManager.getStreamVolume(AudioManager.STREAM_RING) + + " (max=" + maxRingVolume + ")"); + } else if (mode == AudioManager.MODE_IN_COMMUNICATION) { + Logging.d(TAG, + "VOICE_CALL stream volume: " + + audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL) + + " (max=" + maxVoiceCallVolume + ")"); + } + } + } + + public void stop() { + Logging.d(TAG, "stop" + WebRtcAudioUtils.getThreadInfo()); + if (timer != null) { + timer.cancel(); + timer = null; + } + } +} diff --git a/sdk/android/src/java/org/webrtc/audio/WebRtcAudioManager.java b/sdk/android/src/java/org/webrtc/audio/WebRtcAudioManager.java index e75b3ed50d..62b32abd0d 100644 --- a/sdk/android/src/java/org/webrtc/audio/WebRtcAudioManager.java +++ b/sdk/android/src/java/org/webrtc/audio/WebRtcAudioManager.java @@ -18,8 +18,6 @@ import android.media.AudioManager; import android.media.AudioRecord; import android.media.AudioTrack; import android.os.Build; -import java.util.Timer; -import java.util.TimerTask; import javax.annotation.Nullable; import org.webrtc.Logging; import org.webrtc.CalledByNative; @@ -78,61 +76,6 @@ class WebRtcAudioManager { private static final int DEFAULT_FRAME_PER_BUFFER = 256; - // Private utility class that periodically checks and logs the volume level - // of the audio stream that is currently controlled by the volume control. - // A timer triggers logs once every 30 seconds and the timer's associated - // thread is named "WebRtcVolumeLevelLoggerThread". - private static class VolumeLogger { - private static final String THREAD_NAME = "WebRtcVolumeLevelLoggerThread"; - private static final int TIMER_PERIOD_IN_SECONDS = 30; - - private final AudioManager audioManager; - private @Nullable Timer timer; - - public VolumeLogger(AudioManager audioManager) { - this.audioManager = audioManager; - } - - public void start() { - timer = new Timer(THREAD_NAME); - timer.schedule(new LogVolumeTask(audioManager.getStreamMaxVolume(AudioManager.STREAM_RING), - audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL)), - 0, TIMER_PERIOD_IN_SECONDS * 1000); - } - - private class LogVolumeTask extends TimerTask { - private final int maxRingVolume; - private final int maxVoiceCallVolume; - - LogVolumeTask(int maxRingVolume, int maxVoiceCallVolume) { - this.maxRingVolume = maxRingVolume; - this.maxVoiceCallVolume = maxVoiceCallVolume; - } - - @Override - public void run() { - final int mode = audioManager.getMode(); - if (mode == AudioManager.MODE_RINGTONE) { - Logging.d(TAG, - "STREAM_RING stream volume: " + audioManager.getStreamVolume(AudioManager.STREAM_RING) - + " (max=" + maxRingVolume + ")"); - } else if (mode == AudioManager.MODE_IN_COMMUNICATION) { - Logging.d(TAG, - "VOICE_CALL stream volume: " - + audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL) - + " (max=" + maxVoiceCallVolume + ")"); - } - } - } - - private void stop() { - if (timer != null) { - timer.cancel(); - timer = null; - } - } - } - private final AudioManager audioManager; private final int sampleRate; private final int outputBufferSize;