1 /*
2  * Copyright (C) 2018 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 package com.google.android.exoplayer2.source.chunk;
17 
18 import com.google.android.exoplayer2.upstream.DataSpec;
19 import java.util.NoSuchElementException;
20 
21 /**
22  * Iterator for media chunk sequences.
23  *
24  * <p>The iterator initially points in front of the first available element. The first call to
25  * {@link #next()} moves the iterator to the first element. Check the return value of {@link
26  * #next()} or {@link #isEnded()} to determine whether the iterator reached the end of the available
27  * data.
28  */
29 public interface MediaChunkIterator {
30 
31   /** An empty media chunk iterator without available data. */
32   MediaChunkIterator EMPTY =
33       new MediaChunkIterator() {
34         @Override
35         public boolean isEnded() {
36           return true;
37         }
38 
39         @Override
40         public boolean next() {
41           return false;
42         }
43 
44         @Override
45         public DataSpec getDataSpec() {
46           throw new NoSuchElementException();
47         }
48 
49         @Override
50         public long getChunkStartTimeUs() {
51           throw new NoSuchElementException();
52         }
53 
54         @Override
55         public long getChunkEndTimeUs() {
56           throw new NoSuchElementException();
57         }
58 
59         @Override
60         public void reset() {
61           // Do nothing.
62         }
63       };
64 
65   /** Returns whether the iteration has reached the end of the available data. */
isEnded()66   boolean isEnded();
67 
68   /**
69    * Moves the iterator to the next media chunk.
70    *
71    * <p>Check the return value or {@link #isEnded()} to determine whether the iterator reached the
72    * end of the available data.
73    *
74    * @return Whether the iterator points to a media chunk with available data.
75    */
next()76   boolean next();
77 
78   /**
79    * Returns the {@link DataSpec} used to load the media chunk.
80    *
81    * @throws java.util.NoSuchElementException If the method is called before the first call to
82    *     {@link #next()} or when {@link #isEnded()} is true.
83    */
getDataSpec()84   DataSpec getDataSpec();
85 
86   /**
87    * Returns the media start time of the chunk, in microseconds.
88    *
89    * @throws java.util.NoSuchElementException If the method is called before the first call to
90    *     {@link #next()} or when {@link #isEnded()} is true.
91    */
getChunkStartTimeUs()92   long getChunkStartTimeUs();
93 
94   /**
95    * Returns the media end time of the chunk, in microseconds.
96    *
97    * @throws java.util.NoSuchElementException If the method is called before the first call to
98    *     {@link #next()} or when {@link #isEnded()} is true.
99    */
getChunkEndTimeUs()100   long getChunkEndTimeUs();
101 
102   /** Resets the iterator to the initial position. */
reset()103   void reset();
104 }
105