diff --git a/library/src/main/java/com/google/android/exoplayer/upstream/PriorityDataSource.java b/library/src/main/java/com/google/android/exoplayer/upstream/PriorityDataSource.java index 310a054296..10f0f588a3 100644 --- a/library/src/main/java/com/google/android/exoplayer/upstream/PriorityDataSource.java +++ b/library/src/main/java/com/google/android/exoplayer/upstream/PriorityDataSource.java @@ -16,39 +16,51 @@ package com.google.android.exoplayer.upstream; import com.google.android.exoplayer.util.Assertions; +import com.google.android.exoplayer.util.PriorityTaskManager; import android.net.Uri; import java.io.IOException; /** - * Allows {@link #open(DataSpec)} and {@link #read(byte[], int, int)} calls only if the specified - * priority is the highest priority of any task. {@link NetworkLock.PriorityTooLowException} is - * thrown when this condition does not hold. + * A {@link DataSource} that can be used as part of a task registered with a + * {@link PriorityTaskManager}. + *
+ * Calls to {@link #open(DataSpec)} and {@link #read(byte[], int, int)} are allowed to proceed only + * if there are no higher priority tasks registered to the {@link PriorityTaskManager}. If there + * exists a higher priority task then {@link PriorityTaskManager.PriorityTooLowException} is thrown. + *
+ * Instances of this class are intended to be used as parts of (possibly larger) tasks that are + * registered with the {@link PriorityTaskManager}, and hence do not register as tasks + * themselves. */ public final class PriorityDataSource implements DataSource { private final DataSource upstream; + private final PriorityTaskManager priorityTaskManager; private final int priority; /** - * @param priority The priority of the source. * @param upstream The upstream {@link DataSource}. + * @param priorityTaskManager The priority manager to which the task is registered. + * @param priority The priority of the task. */ - public PriorityDataSource(int priority, DataSource upstream) { - this.priority = priority; + public PriorityDataSource(DataSource upstream, PriorityTaskManager priorityTaskManager, + int priority) { this.upstream = Assertions.checkNotNull(upstream); + this.priorityTaskManager = Assertions.checkNotNull(priorityTaskManager); + this.priority = priority; } @Override public long open(DataSpec dataSpec) throws IOException { - NetworkLock.instance.proceedOrThrow(priority); + priorityTaskManager.proceedOrThrow(priority); return upstream.open(dataSpec); } @Override public int read(byte[] buffer, int offset, int max) throws IOException { - NetworkLock.instance.proceedOrThrow(priority); + priorityTaskManager.proceedOrThrow(priority); return upstream.read(buffer, offset, max); } diff --git a/library/src/main/java/com/google/android/exoplayer/upstream/NetworkLock.java b/library/src/main/java/com/google/android/exoplayer/util/PriorityTaskManager.java similarity index 52% rename from library/src/main/java/com/google/android/exoplayer/upstream/NetworkLock.java rename to library/src/main/java/com/google/android/exoplayer/util/PriorityTaskManager.java index b8c6aa1f65..fd85d8fe02 100644 --- a/library/src/main/java/com/google/android/exoplayer/upstream/NetworkLock.java +++ b/library/src/main/java/com/google/android/exoplayer/util/PriorityTaskManager.java @@ -13,26 +13,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.google.android.exoplayer.upstream; +package com.google.android.exoplayer.util; import java.io.IOException; +import java.util.Collections; import java.util.PriorityQueue; /** - * A network task prioritization mechanism. + * Allows tasks with associated priorities to control how they proceed relative to one another. *
- * Manages different priority network tasks. A network task that wishes to have its priority - * respected, and respect the priority of other tasks, should register itself with the lock prior - * to making network requests. It should then call one of the lock's proceed methods frequently - * during execution, so as to ensure that it continues only if it is the highest (or equally - * highest) priority task. - *
- * Note that lower integer values correspond to higher priorities.
+ * A task should call {@link #add(int)} to register with the manager and {@link #remove(int)} to
+ * unregister. A registered task will prevent tasks of lower priority from proceeding, and should
+ * call {@link #proceed(int)}, {@link #proceedNonBlocking(int)} or {@link #proceedOrThrow(int)} each
+ * time it wishes to check whether it is itself allowed to proceed.
*/
-public final class NetworkLock {
+public final class PriorityTaskManager {
/**
- * Thrown when a task is attempts to proceed when it does not have the highest priority.
+ * Thrown when task attempts to proceed when another registered task has a higher priority.
*/
public static class PriorityTooLowException extends IOException {
@@ -42,67 +40,15 @@ public final class NetworkLock {
}
- public static final NetworkLock instance = new NetworkLock();
-
- /**
- * Priority for network tasks associated with media streaming.
- */
- public static final int STREAMING_PRIORITY = 0;
- /**
- * Priority for network tasks associated with background downloads.
- */
- public static final int DOWNLOAD_PRIORITY = 10;
-
private final Object lock = new Object();
- /** Guarded by {@link #lock}. */
+ // Guarded by lock.
private final PriorityQueue