Commit 5da703a5 by andrewlewis Committed by Oliver Woodman

Fix some nullness warnings in audio classes

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=206021873
parent d810352f
...@@ -39,12 +39,9 @@ public final class AudioAttributes { ...@@ -39,12 +39,9 @@ public final class AudioAttributes {
*/ */
public static final class Builder { public static final class Builder {
@C.AudioContentType private @C.AudioContentType int contentType;
private int contentType; private @C.AudioFlags int flags;
@C.AudioFlags private @C.AudioUsage int usage;
private int flags;
@C.AudioUsage
private int usage;
/** /**
* Creates a new builder for {@link AudioAttributes}. * Creates a new builder for {@link AudioAttributes}.
...@@ -91,14 +88,11 @@ public final class AudioAttributes { ...@@ -91,14 +88,11 @@ public final class AudioAttributes {
} }
@C.AudioContentType public final @C.AudioContentType int contentType;
public final int contentType; public final @C.AudioFlags int flags;
@C.AudioFlags public final @C.AudioUsage int usage;
public final int flags;
@C.AudioUsage
public final int usage;
private android.media.AudioAttributes audioAttributesV21; private @Nullable android.media.AudioAttributes audioAttributesV21;
private AudioAttributes(@C.AudioContentType int contentType, @C.AudioFlags int flags, private AudioAttributes(@C.AudioContentType int contentType, @C.AudioFlags int flags,
@C.AudioUsage int usage) { @C.AudioUsage int usage) {
......
...@@ -50,7 +50,7 @@ public final class AudioCapabilities { ...@@ -50,7 +50,7 @@ public final class AudioCapabilities {
} }
@SuppressLint("InlinedApi") @SuppressLint("InlinedApi")
/* package */ static AudioCapabilities getCapabilities(Intent intent) { /* package */ static AudioCapabilities getCapabilities(@Nullable Intent intent) {
if (intent == null || intent.getIntExtra(AudioManager.EXTRA_AUDIO_PLUG_STATE, 0) == 0) { if (intent == null || intent.getIntExtra(AudioManager.EXTRA_AUDIO_PLUG_STATE, 0) == 0) {
return DEFAULT_AUDIO_CAPABILITIES; return DEFAULT_AUDIO_CAPABILITIES;
} }
......
...@@ -20,6 +20,7 @@ import android.content.Context; ...@@ -20,6 +20,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.media.AudioManager; import android.media.AudioManager;
import android.support.annotation.Nullable;
import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
...@@ -45,9 +46,9 @@ public final class AudioCapabilitiesReceiver { ...@@ -45,9 +46,9 @@ public final class AudioCapabilitiesReceiver {
private final Context context; private final Context context;
private final Listener listener; private final Listener listener;
private final BroadcastReceiver receiver; private final @Nullable BroadcastReceiver receiver;
/* package */ AudioCapabilities audioCapabilities; /* package */ @Nullable AudioCapabilities audioCapabilities;
/** /**
* @param context A context for registering the receiver. * @param context A context for registering the receiver.
......
...@@ -15,10 +15,13 @@ ...@@ -15,10 +15,13 @@
*/ */
package com.google.android.exoplayer2.audio; package com.google.android.exoplayer2.audio;
import static org.checkerframework.checker.nullness.NullnessUtil.castNonNull;
import android.media.AudioTimestamp; import android.media.AudioTimestamp;
import android.media.AudioTrack; import android.media.AudioTrack;
import android.os.SystemClock; import android.os.SystemClock;
import android.support.annotation.IntDef; import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
...@@ -128,10 +131,10 @@ import java.lang.reflect.Method; ...@@ -128,10 +131,10 @@ import java.lang.reflect.Method;
private final Listener listener; private final Listener listener;
private final long[] playheadOffsets; private final long[] playheadOffsets;
private AudioTrack audioTrack; private @Nullable AudioTrack audioTrack;
private int outputPcmFrameSize; private int outputPcmFrameSize;
private int bufferSize; private int bufferSize;
private AudioTimestampPoller audioTimestampPoller; private @Nullable AudioTimestampPoller audioTimestampPoller;
private int outputSampleRate; private int outputSampleRate;
private boolean needsPassthroughWorkarounds; private boolean needsPassthroughWorkarounds;
private long bufferSizeUs; private long bufferSizeUs;
...@@ -139,7 +142,7 @@ import java.lang.reflect.Method; ...@@ -139,7 +142,7 @@ import java.lang.reflect.Method;
private long smoothedPlayheadOffsetUs; private long smoothedPlayheadOffsetUs;
private long lastPlayheadSampleTimeUs; private long lastPlayheadSampleTimeUs;
private Method getLatencyMethod; private @Nullable Method getLatencyMethod;
private long latencyUs; private long latencyUs;
private boolean hasData; private boolean hasData;
...@@ -205,13 +208,14 @@ import java.lang.reflect.Method; ...@@ -205,13 +208,14 @@ import java.lang.reflect.Method;
} }
public long getCurrentPositionUs(boolean sourceEnded) { public long getCurrentPositionUs(boolean sourceEnded) {
if (audioTrack.getPlayState() == PLAYSTATE_PLAYING) { if (Assertions.checkNotNull(this.audioTrack).getPlayState() == PLAYSTATE_PLAYING) {
maybeSampleSyncParams(); maybeSampleSyncParams();
} }
// If the device supports it, use the playback timestamp from AudioTrack.getTimestamp. // If the device supports it, use the playback timestamp from AudioTrack.getTimestamp.
// Otherwise, derive a smoothed position by sampling the track's frame position. // Otherwise, derive a smoothed position by sampling the track's frame position.
long systemTimeUs = System.nanoTime() / 1000; long systemTimeUs = System.nanoTime() / 1000;
AudioTimestampPoller audioTimestampPoller = Assertions.checkNotNull(this.audioTimestampPoller);
if (audioTimestampPoller.hasTimestamp()) { if (audioTimestampPoller.hasTimestamp()) {
// Calculate the speed-adjusted position using the timestamp (which may be in the future). // Calculate the speed-adjusted position using the timestamp (which may be in the future).
long timestampPositionFrames = audioTimestampPoller.getTimestampPositionFrames(); long timestampPositionFrames = audioTimestampPoller.getTimestampPositionFrames();
...@@ -241,12 +245,12 @@ import java.lang.reflect.Method; ...@@ -241,12 +245,12 @@ import java.lang.reflect.Method;
/** Starts position tracking. Must be called immediately before {@link AudioTrack#play()}. */ /** Starts position tracking. Must be called immediately before {@link AudioTrack#play()}. */
public void start() { public void start() {
audioTimestampPoller.reset(); Assertions.checkNotNull(audioTimestampPoller).reset();
} }
/** Returns whether the audio track is in the playing state. */ /** Returns whether the audio track is in the playing state. */
public boolean isPlaying() { public boolean isPlaying() {
return audioTrack.getPlayState() == PLAYSTATE_PLAYING; return Assertions.checkNotNull(audioTrack).getPlayState() == PLAYSTATE_PLAYING;
} }
/** /**
...@@ -257,7 +261,7 @@ import java.lang.reflect.Method; ...@@ -257,7 +261,7 @@ import java.lang.reflect.Method;
* @return Whether the caller can write data to the track. * @return Whether the caller can write data to the track.
*/ */
public boolean mayHandleBuffer(long writtenFrames) { public boolean mayHandleBuffer(long writtenFrames) {
@PlayState int playState = audioTrack.getPlayState(); @PlayState int playState = Assertions.checkNotNull(audioTrack).getPlayState();
if (needsPassthroughWorkarounds) { if (needsPassthroughWorkarounds) {
// An AC-3 audio track continues to play data written while it is paused. Stop writing so its // An AC-3 audio track continues to play data written while it is paused. Stop writing so its
// buffer empties. See [Internal: b/18899620]. // buffer empties. See [Internal: b/18899620].
...@@ -339,7 +343,7 @@ import java.lang.reflect.Method; ...@@ -339,7 +343,7 @@ import java.lang.reflect.Method;
if (stopTimestampUs == C.TIME_UNSET) { if (stopTimestampUs == C.TIME_UNSET) {
// The audio track is going to be paused, so reset the timestamp poller to ensure it doesn't // The audio track is going to be paused, so reset the timestamp poller to ensure it doesn't
// supply an advancing position. // supply an advancing position.
audioTimestampPoller.reset(); Assertions.checkNotNull(audioTimestampPoller).reset();
return true; return true;
} }
// We've handled the end of the stream already, so there's no need to pause the track. // We've handled the end of the stream already, so there's no need to pause the track.
...@@ -388,6 +392,7 @@ import java.lang.reflect.Method; ...@@ -388,6 +392,7 @@ import java.lang.reflect.Method;
} }
private void maybePollAndCheckTimestamp(long systemTimeUs, long playbackPositionUs) { private void maybePollAndCheckTimestamp(long systemTimeUs, long playbackPositionUs) {
AudioTimestampPoller audioTimestampPoller = Assertions.checkNotNull(this.audioTimestampPoller);
if (!audioTimestampPoller.maybePollTimestamp(systemTimeUs)) { if (!audioTimestampPoller.maybePollTimestamp(systemTimeUs)) {
return; return;
} }
...@@ -423,7 +428,9 @@ import java.lang.reflect.Method; ...@@ -423,7 +428,9 @@ import java.lang.reflect.Method;
// Compute the audio track latency, excluding the latency due to the buffer (leaving // Compute the audio track latency, excluding the latency due to the buffer (leaving
// latency due to the mixer and audio hardware driver). // latency due to the mixer and audio hardware driver).
latencyUs = latencyUs =
(Integer) getLatencyMethod.invoke(audioTrack, (Object[]) null) * 1000L - bufferSizeUs; castNonNull((Integer) getLatencyMethod.invoke(Assertions.checkNotNull(audioTrack)))
* 1000L
- bufferSizeUs;
// Sanity check that the latency is non-negative. // Sanity check that the latency is non-negative.
latencyUs = Math.max(latencyUs, 0); latencyUs = Math.max(latencyUs, 0);
// Sanity check that the latency isn't too large. // Sanity check that the latency isn't too large.
...@@ -457,7 +464,7 @@ import java.lang.reflect.Method; ...@@ -457,7 +464,7 @@ import java.lang.reflect.Method;
*/ */
private boolean forceHasPendingData() { private boolean forceHasPendingData() {
return needsPassthroughWorkarounds return needsPassthroughWorkarounds
&& audioTrack.getPlayState() == AudioTrack.PLAYSTATE_PAUSED && Assertions.checkNotNull(audioTrack).getPlayState() == AudioTrack.PLAYSTATE_PAUSED
&& getPlaybackHeadPosition() == 0; && getPlaybackHeadPosition() == 0;
} }
...@@ -483,6 +490,7 @@ import java.lang.reflect.Method; ...@@ -483,6 +490,7 @@ import java.lang.reflect.Method;
* @return The playback head position, in frames. * @return The playback head position, in frames.
*/ */
private long getPlaybackHeadPosition() { private long getPlaybackHeadPosition() {
AudioTrack audioTrack = Assertions.checkNotNull(this.audioTrack);
if (stopTimestampUs != C.TIME_UNSET) { if (stopTimestampUs != C.TIME_UNSET) {
// Simulate the playback head position up to the total number of frames submitted. // Simulate the playback head position up to the total number of frames submitted.
long elapsedTimeSinceStopUs = (SystemClock.elapsedRealtime() * 1000) - stopTimestampUs; long elapsedTimeSinceStopUs = (SystemClock.elapsedRealtime() * 1000) - stopTimestampUs;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment