1 /* 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 package org.webrtc.audio; 12 13 import android.media.AudioManager; 14 import androidx.annotation.Nullable; 15 import java.util.Timer; 16 import java.util.TimerTask; 17 import org.webrtc.Logging; 18 19 // TODO(magjed): Do we really need to spawn a new thread just to log volume? Can we re-use the 20 // AudioTrackThread instead? 21 /** 22 * Private utility class that periodically checks and logs the volume level of the audio stream that 23 * is currently controlled by the volume control. A timer triggers logs once every 30 seconds and 24 * the timer's associated thread is named "WebRtcVolumeLevelLoggerThread". 25 */ 26 class VolumeLogger { 27 private static final String TAG = "VolumeLogger"; 28 private static final String THREAD_NAME = "WebRtcVolumeLevelLoggerThread"; 29 private static final int TIMER_PERIOD_IN_SECONDS = 30; 30 31 private final AudioManager audioManager; 32 private @Nullable Timer timer; 33 VolumeLogger(AudioManager audioManager)34 public VolumeLogger(AudioManager audioManager) { 35 this.audioManager = audioManager; 36 } 37 start()38 public void start() { 39 Logging.d(TAG, "start" + WebRtcAudioUtils.getThreadInfo()); 40 if (timer != null) { 41 return; 42 } 43 Logging.d(TAG, "audio mode is: " + WebRtcAudioUtils.modeToString(audioManager.getMode())); 44 45 timer = new Timer(THREAD_NAME); 46 timer.schedule(new LogVolumeTask(audioManager.getStreamMaxVolume(AudioManager.STREAM_RING), 47 audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL)), 48 0, TIMER_PERIOD_IN_SECONDS * 1000); 49 } 50 51 private class LogVolumeTask extends TimerTask { 52 private final int maxRingVolume; 53 private final int maxVoiceCallVolume; 54 LogVolumeTask(int maxRingVolume, int maxVoiceCallVolume)55 LogVolumeTask(int maxRingVolume, int maxVoiceCallVolume) { 56 this.maxRingVolume = maxRingVolume; 57 this.maxVoiceCallVolume = maxVoiceCallVolume; 58 } 59 60 @Override run()61 public void run() { 62 final int mode = audioManager.getMode(); 63 if (mode == AudioManager.MODE_RINGTONE) { 64 Logging.d(TAG, 65 "STREAM_RING stream volume: " + audioManager.getStreamVolume(AudioManager.STREAM_RING) 66 + " (max=" + maxRingVolume + ")"); 67 } else if (mode == AudioManager.MODE_IN_COMMUNICATION) { 68 Logging.d(TAG, 69 "VOICE_CALL stream volume: " 70 + audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL) 71 + " (max=" + maxVoiceCallVolume + ")"); 72 } 73 } 74 } 75 stop()76 public void stop() { 77 Logging.d(TAG, "stop" + WebRtcAudioUtils.getThreadInfo()); 78 if (timer != null) { 79 timer.cancel(); 80 timer = null; 81 } 82 } 83 } 84