diff options
Diffstat (limited to 'webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioTrack.java')
-rw-r--r-- | webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioTrack.java | 53 |
1 files changed, 30 insertions, 23 deletions
diff --git a/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioTrack.java b/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioTrack.java index ec0e109169..11eb51383d 100644 --- a/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioTrack.java +++ b/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioTrack.java @@ -13,6 +13,7 @@ package org.webrtc.voiceengine; import java.lang.Thread; import java.nio.ByteBuffer; +import android.annotation.TargetApi; import android.content.Context; import android.media.AudioFormat; import android.media.AudioManager; @@ -39,7 +40,6 @@ class WebRtcAudioTrack { private final Context context; private final long nativeAudioTrack; private final AudioManager audioManager; - private final int streamType; private ByteBuffer byteBuffer; @@ -91,13 +91,9 @@ class WebRtcAudioTrack { assertTrue(sizeInBytes <= byteBuffer.remaining()); int bytesWritten = 0; if (WebRtcAudioUtils.runningOnLollipopOrHigher()) { - bytesWritten = audioTrack.write(byteBuffer, - sizeInBytes, - AudioTrack.WRITE_BLOCKING); + bytesWritten = writeOnLollipop(audioTrack, byteBuffer, sizeInBytes); } else { - bytesWritten = audioTrack.write(byteBuffer.array(), - byteBuffer.arrayOffset(), - sizeInBytes); + bytesWritten = writePreLollipop(audioTrack, byteBuffer, sizeInBytes); } if (bytesWritten != sizeInBytes) { Logging.e(TAG, "AudioTrack.write failed: " + bytesWritten); @@ -124,6 +120,15 @@ class WebRtcAudioTrack { audioTrack.flush(); } + @TargetApi(21) + private int writeOnLollipop(AudioTrack audioTrack, ByteBuffer byteBuffer, int sizeInBytes) { + return audioTrack.write(byteBuffer, sizeInBytes, AudioTrack.WRITE_BLOCKING); + } + + private int writePreLollipop(AudioTrack audioTrack, ByteBuffer byteBuffer, int sizeInBytes) { + return audioTrack.write(byteBuffer.array(), byteBuffer.arrayOffset(), sizeInBytes); + } + public void joinThread() { keepAlive = false; while (isAlive()) { @@ -142,9 +147,6 @@ class WebRtcAudioTrack { this.nativeAudioTrack = nativeAudioTrack; audioManager = (AudioManager) context.getSystemService( Context.AUDIO_SERVICE); - this.streamType = - WebRtcAudioUtils.getOutputStreamTypeFromAudioMode( - audioManager.getMode()); if (DEBUG) { WebRtcAudioUtils.logDeviceInfo(TAG); } @@ -181,7 +183,7 @@ class WebRtcAudioTrack { // Create an AudioTrack object and initialize its associated audio buffer. // The size of this buffer determines how long an AudioTrack can play // before running out of data. - audioTrack = new AudioTrack(streamType, + audioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL, sampleRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, @@ -193,7 +195,7 @@ class WebRtcAudioTrack { } assertTrue(audioTrack.getState() == AudioTrack.STATE_INITIALIZED); assertTrue(audioTrack.getPlayState() == AudioTrack.PLAYSTATE_STOPPED); - assertTrue(audioTrack.getStreamType() == streamType); + assertTrue(audioTrack.getStreamType() == AudioManager.STREAM_VOICE_CALL); } private boolean startPlayout() { @@ -217,32 +219,37 @@ class WebRtcAudioTrack { return true; } - /** Get max possible volume index given type of audio stream. */ + /** Get max possible volume index for a phone call audio stream. */ private int getStreamMaxVolume() { Logging.d(TAG, "getStreamMaxVolume"); assertTrue(audioManager != null); - return audioManager.getStreamMaxVolume(streamType); + return audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL); } - /** Set current volume level given type of audio stream. */ + /** Set current volume level for a phone call audio stream. */ private boolean setStreamVolume(int volume) { Logging.d(TAG, "setStreamVolume(" + volume + ")"); assertTrue(audioManager != null); - if (WebRtcAudioUtils.runningOnLollipopOrHigher()) { - if (audioManager.isVolumeFixed()) { - Logging.e(TAG, "The device implements a fixed volume policy."); - return false; - } + if (isVolumeFixed()) { + Logging.e(TAG, "The device implements a fixed volume policy."); + return false; } - audioManager.setStreamVolume(streamType, volume, 0); + audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, volume, 0); return true; } - /** Get current volume level given type of audio stream. */ + @TargetApi(21) + private boolean isVolumeFixed() { + if (!WebRtcAudioUtils.runningOnLollipopOrHigher()) + return false; + return audioManager.isVolumeFixed(); + } + + /** Get current volume level for a phone call audio stream. */ private int getStreamVolume() { Logging.d(TAG, "getStreamVolume"); assertTrue(audioManager != null); - return audioManager.getStreamVolume(streamType); + return audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL); } /** Helper method which throws an exception when an assertion has failed. */ |