|
| 1 | +/* |
| 2 | + * Copyright 2020 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.android.exoplayer2.extractor; |
| 18 | + |
| 19 | +import static com.google.android.exoplayer2.util.Assertions.checkArgument; |
| 20 | + |
| 21 | +import com.google.android.exoplayer2.C; |
| 22 | +import com.google.android.exoplayer2.util.Util; |
| 23 | + |
| 24 | +/** |
| 25 | + * A {@link SeekMap} implementation based on a mapping between times and positions in the input |
| 26 | + * stream. |
| 27 | + */ |
| 28 | +public final class IndexSeekMap implements SeekMap { |
| 29 | + |
| 30 | + private final long[] positions; |
| 31 | + private final long[] timesUs; |
| 32 | + private final long durationUs; |
| 33 | + private final boolean isSeekable; |
| 34 | + |
| 35 | + /** |
| 36 | + * Creates an instance. |
| 37 | + * |
| 38 | + * @param positions The positions in the stream corresponding to {@code timesUs}, in bytes. |
| 39 | + * @param timesUs The times corresponding to {@code positions}, in microseconds. |
| 40 | + * @param durationUs The duration of the input stream, or {@link C#TIME_UNSET} if it is unknown. |
| 41 | + */ |
| 42 | + public IndexSeekMap(long[] positions, long[] timesUs, long durationUs) { |
| 43 | + checkArgument(positions.length == timesUs.length); |
| 44 | + int length = timesUs.length; |
| 45 | + isSeekable = length > 0; |
| 46 | + if (isSeekable && timesUs[0] > 0) { |
| 47 | + // Add (position = 0, timeUs = 0) as first entry. |
| 48 | + this.positions = new long[length + 1]; |
| 49 | + this.timesUs = new long[length + 1]; |
| 50 | + System.arraycopy(positions, 0, this.positions, 1, length); |
| 51 | + System.arraycopy(timesUs, 0, this.timesUs, 1, length); |
| 52 | + } else { |
| 53 | + this.positions = positions; |
| 54 | + this.timesUs = timesUs; |
| 55 | + } |
| 56 | + this.durationUs = durationUs; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public boolean isSeekable() { |
| 61 | + return isSeekable; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public long getDurationUs() { |
| 66 | + return durationUs; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public SeekMap.SeekPoints getSeekPoints(long timeUs) { |
| 71 | + if (!isSeekable) { |
| 72 | + return new SeekMap.SeekPoints(SeekPoint.START); |
| 73 | + } |
| 74 | + int targetIndex = |
| 75 | + Util.binarySearchFloor(timesUs, timeUs, /* inclusive= */ true, /* stayInBounds= */ true); |
| 76 | + SeekPoint leftSeekPoint = new SeekPoint(timesUs[targetIndex], positions[targetIndex]); |
| 77 | + if (leftSeekPoint.timeUs >= timeUs || targetIndex == timesUs.length - 1) { |
| 78 | + return new SeekMap.SeekPoints(leftSeekPoint); |
| 79 | + } else { |
| 80 | + SeekPoint rightSeekPoint = |
| 81 | + new SeekPoint(timesUs[targetIndex + 1], positions[targetIndex + 1]); |
| 82 | + return new SeekMap.SeekPoints(leftSeekPoint, rightSeekPoint); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments