summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/sdk/android/src/java/org/webrtc/audio/VolumeLogger.java
blob: 06d5cd3a8e53f2de96c509fa3038ae92de4bbb0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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 androidx.annotation.Nullable;
import java.util.Timer;
import java.util.TimerTask;
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;
    }
  }
}