diff --git a/library/src/main/java/com/sonymobile/android/media/internal/BufferedDataSource.java b/library/src/main/java/com/sonymobile/android/media/internal/BufferedDataSource.java index 84f350b..4458847 100644 --- a/library/src/main/java/com/sonymobile/android/media/internal/BufferedDataSource.java +++ b/library/src/main/java/com/sonymobile/android/media/internal/BufferedDataSource.java @@ -486,6 +486,11 @@ protected void doCloseSilently(Closeable c) { @Override public abstract int readAt(long offset, byte[] buffer, int size) throws IOException; + @Override + public void requestReadPosition(long offset) throws IOException { + // Empty implementation, interested subclasses should override. + } + class ReconnectHandler extends Handler { private int mPreviousAvailable = 0; diff --git a/library/src/main/java/com/sonymobile/android/media/internal/DataSource.java b/library/src/main/java/com/sonymobile/android/media/internal/DataSource.java index 2dfe904..7a92bf1 100644 --- a/library/src/main/java/com/sonymobile/android/media/internal/DataSource.java +++ b/library/src/main/java/com/sonymobile/android/media/internal/DataSource.java @@ -180,6 +180,10 @@ public DataAvailability hasDataAvailable(long offset, int size) { return DataAvailability.AVAILABLE; } + public void requestReadPosition(long offset) throws IOException { + // Empty implementation, interested subclasses should override. + } + public abstract void seek(long offset) throws IOException; protected long peekLong(byte[] src, int offset) { diff --git a/library/src/main/java/com/sonymobile/android/media/internal/HttpBufferedDataSource.java b/library/src/main/java/com/sonymobile/android/media/internal/HttpBufferedDataSource.java index e26887a..4eec559 100644 --- a/library/src/main/java/com/sonymobile/android/media/internal/HttpBufferedDataSource.java +++ b/library/src/main/java/com/sonymobile/android/media/internal/HttpBufferedDataSource.java @@ -202,6 +202,18 @@ public synchronized DataAvailability hasDataAvailable(long offset, int size) { return toReturn; } + @Override + public synchronized void requestReadPosition(long offset) throws IOException { + if (LOGS_ENABLED) + Log.d(TAG, "Request reconnect now at " + offset); + + mCurrentOffset = offset; + mOffset = offset; + + doCloseSync(); + openConnectionsAndStreams(); + } + public int getBuffering() { int percentage = 0; try { diff --git a/library/src/main/java/com/sonymobile/android/media/internal/ISOBMFFParser.java b/library/src/main/java/com/sonymobile/android/media/internal/ISOBMFFParser.java index 24c8c1a..53353b1 100644 --- a/library/src/main/java/com/sonymobile/android/media/internal/ISOBMFFParser.java +++ b/library/src/main/java/com/sonymobile/android/media/internal/ISOBMFFParser.java @@ -403,20 +403,45 @@ public boolean parse() { mMetaDataValues.put(MetaData.KEY_SEEK_AVAILABLE, 1); mMetaDataValues.put(MetaData.KEY_NUM_TRACKS, mTracks.size()); - updateAspectRatio(); + if (parseOK) { + updateAspectRatio(); - updateRotation(); + updateRotation(); - if (mCurrentAudioTrack != null) { - mCurrentAudioTrack.buildSampleTable(); - } + if (mCurrentAudioTrack != null) { + mCurrentAudioTrack.buildSampleTable(); + } - if (mCurrentVideoTrack != null) { - mCurrentVideoTrack.buildSampleTable(); - } + if (mCurrentVideoTrack != null) { + mCurrentVideoTrack.buildSampleTable(); + } + + if (mCurrentSubtitleTrack != null) { + mCurrentSubtitleTrack.buildSampleTable(); + } - if (mCurrentSubtitleTrack != null) { - mCurrentSubtitleTrack.buildSampleTable(); + long firstOffset = 0; + if (mIsFragmented) { + firstOffset = mFirstMoofOffset; + } else { + if (mCurrentVideoTrack != null) { + firstOffset = mCurrentVideoTrack.getSampleTable().getOffset(0); + } + + if (mCurrentAudioTrack != null) { + long audioOffset = mCurrentAudioTrack.getSampleTable().getOffset(0); + if (firstOffset == 0 || audioOffset < firstOffset) { + firstOffset = audioOffset; + } + } + } + + if (mDataSource.hasDataAvailable(firstOffset, 1) == DataAvailability.NOT_AVAILABLE) { + try { + mDataSource.requestReadPosition(firstOffset); + } catch (IOException e) { + } + } } return parseOK;