Make audio renderer more robust to bad getTimestamp values.

This commit is contained in:
Oliver Woodman 2014-09-08 11:11:01 +01:00
parent a7b88cd6a9
commit 3b25928a13

View File

@ -92,6 +92,13 @@ public class MediaCodecAudioTrackRenderer extends MediaCodecTrackRenderer {
private static final long MICROS_PER_SECOND = 1000000L; private static final long MICROS_PER_SECOND = 1000000L;
/**
* AudioTrack timestamps are deemed spurious if they are offset from the system clock by more
* than this amount. This is a fail safe that should not be required on correctly functioning
* devices.
*/
private static final long MAX_AUDIO_TIMSTAMP_OFFSET_US = 10 * MICROS_PER_SECOND;
private static final int MAX_PLAYHEAD_OFFSET_COUNT = 10; private static final int MAX_PLAYHEAD_OFFSET_COUNT = 10;
private static final int MIN_PLAYHEAD_OFFSET_SAMPLE_INTERVAL_US = 30000; private static final int MIN_PLAYHEAD_OFFSET_SAMPLE_INTERVAL_US = 30000;
private static final int MIN_TIMESTAMP_SAMPLE_INTERVAL_US = 500000; private static final int MIN_TIMESTAMP_SAMPLE_INTERVAL_US = 500000;
@ -502,11 +509,18 @@ public class MediaCodecAudioTrackRenderer extends MediaCodecTrackRenderer {
if (systemClockUs - lastTimestampSampleTimeUs >= MIN_TIMESTAMP_SAMPLE_INTERVAL_US) { if (systemClockUs - lastTimestampSampleTimeUs >= MIN_TIMESTAMP_SAMPLE_INTERVAL_US) {
audioTimestampSet = audioTimestampCompat.initTimestamp(audioTrack); audioTimestampSet = audioTimestampCompat.initTimestamp(audioTrack);
if (audioTimestampSet if (audioTimestampSet) {
&& (audioTimestampCompat.getNanoTime() / 1000) < audioTrackResumeSystemTimeUs) { // Perform sanity checks on the timestamp.
// The timestamp was set, but it corresponds to a time before the track was most recently long audioTimestampUs = audioTimestampCompat.getNanoTime() / 1000;
// resumed. if (audioTimestampUs < audioTrackResumeSystemTimeUs) {
audioTimestampSet = false; // The timestamp corresponds to a time before the track was most recently resumed.
audioTimestampSet = false;
} else if (Math.abs(audioTimestampUs - systemClockUs) > MAX_AUDIO_TIMSTAMP_OFFSET_US) {
// The timestamp time base is probably wrong.
audioTimestampSet = false;
Log.w(TAG, "Spurious audio timestamp: " + audioTimestampCompat.getFramePosition() + ", "
+ audioTimestampUs + ", " + systemClockUs);
}
} }
if (audioTrackGetLatencyMethod != null) { if (audioTrackGetLatencyMethod != null) {
try { try {