Android: Split out VolumeLogger class

The VolumeLogger class contains enough logic to deserve its own file.
Also, I want to potentially remove WebRtcAudioManager.java but keep
volume logging. One problem I see with the VolumeLogger is that it
spawns a new thread, and we should try to keep the number of threads
in WebRTC to a minimum. Right now we use excessively many threads.

Bug: webrtc:7452
Change-Id: I4dd8ffb4265903926f0b372715fc6b876fe5d393
Reviewed-on: https://webrtc-review.googlesource.com/65401
Commit-Queue: Magnus Jedvert <magjed@webrtc.org>
Reviewed-by: Paulina Hensman <phensman@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22676}
This commit is contained in:
Magnus Jedvert
2018-03-29 09:06:32 +02:00
committed by Commit Bot
parent 971bf03ee4
commit 27e41c52f5
3 changed files with 84 additions and 57 deletions

View File

@ -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;
}
}
}

View File

@ -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;