Make NetworkLock non-static and less specific.
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=126682871
This commit is contained in:
parent
dd031691b0
commit
660b26e3ad
@ -16,39 +16,51 @@
|
|||||||
package com.google.android.exoplayer.upstream;
|
package com.google.android.exoplayer.upstream;
|
||||||
|
|
||||||
import com.google.android.exoplayer.util.Assertions;
|
import com.google.android.exoplayer.util.Assertions;
|
||||||
|
import com.google.android.exoplayer.util.PriorityTaskManager;
|
||||||
|
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allows {@link #open(DataSpec)} and {@link #read(byte[], int, int)} calls only if the specified
|
* A {@link DataSource} that can be used as part of a task registered with a
|
||||||
* priority is the highest priority of any task. {@link NetworkLock.PriorityTooLowException} is
|
* {@link PriorityTaskManager}.
|
||||||
* thrown when this condition does not hold.
|
* <p>
|
||||||
|
* 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.
|
||||||
|
* <p>
|
||||||
|
* 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 <em>not</em> register as tasks
|
||||||
|
* themselves.
|
||||||
*/
|
*/
|
||||||
public final class PriorityDataSource implements DataSource {
|
public final class PriorityDataSource implements DataSource {
|
||||||
|
|
||||||
private final DataSource upstream;
|
private final DataSource upstream;
|
||||||
|
private final PriorityTaskManager priorityTaskManager;
|
||||||
private final int priority;
|
private final int priority;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param priority The priority of the source.
|
|
||||||
* @param upstream The upstream {@link DataSource}.
|
* @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) {
|
public PriorityDataSource(DataSource upstream, PriorityTaskManager priorityTaskManager,
|
||||||
this.priority = priority;
|
int priority) {
|
||||||
this.upstream = Assertions.checkNotNull(upstream);
|
this.upstream = Assertions.checkNotNull(upstream);
|
||||||
|
this.priorityTaskManager = Assertions.checkNotNull(priorityTaskManager);
|
||||||
|
this.priority = priority;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long open(DataSpec dataSpec) throws IOException {
|
public long open(DataSpec dataSpec) throws IOException {
|
||||||
NetworkLock.instance.proceedOrThrow(priority);
|
priorityTaskManager.proceedOrThrow(priority);
|
||||||
return upstream.open(dataSpec);
|
return upstream.open(dataSpec);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read(byte[] buffer, int offset, int max) throws IOException {
|
public int read(byte[] buffer, int offset, int max) throws IOException {
|
||||||
NetworkLock.instance.proceedOrThrow(priority);
|
priorityTaskManager.proceedOrThrow(priority);
|
||||||
return upstream.read(buffer, offset, max);
|
return upstream.read(buffer, offset, max);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,26 +13,24 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package com.google.android.exoplayer.upstream;
|
package com.google.android.exoplayer.util;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.PriorityQueue;
|
import java.util.PriorityQueue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A network task prioritization mechanism.
|
* Allows tasks with associated priorities to control how they proceed relative to one another.
|
||||||
* <p>
|
* <p>
|
||||||
* Manages different priority network tasks. A network task that wishes to have its priority
|
* A task should call {@link #add(int)} to register with the manager and {@link #remove(int)} to
|
||||||
* respected, and respect the priority of other tasks, should register itself with the lock prior
|
* unregister. A registered task will prevent tasks of lower priority from proceeding, and should
|
||||||
* to making network requests. It should then call one of the lock's proceed methods frequently
|
* call {@link #proceed(int)}, {@link #proceedNonBlocking(int)} or {@link #proceedOrThrow(int)} each
|
||||||
* during execution, so as to ensure that it continues only if it is the highest (or equally
|
* time it wishes to check whether it is itself allowed to proceed.
|
||||||
* highest) priority task.
|
|
||||||
* <p>
|
|
||||||
* Note that lower integer values correspond to higher priorities.
|
|
||||||
*/
|
*/
|
||||||
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 {
|
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();
|
private final Object lock = new Object();
|
||||||
|
|
||||||
/** Guarded by {@link #lock}. */
|
// Guarded by lock.
|
||||||
private final PriorityQueue<Integer> queue;
|
private final PriorityQueue<Integer> queue;
|
||||||
|
|
||||||
/** Guarded by {@link #lock}. */
|
|
||||||
private int highestPriority;
|
private int highestPriority;
|
||||||
|
|
||||||
private NetworkLock() {
|
private PriorityTaskManager() {
|
||||||
queue = new PriorityQueue<>();
|
queue = new PriorityQueue<>(10, Collections.reverseOrder());
|
||||||
highestPriority = Integer.MAX_VALUE;
|
highestPriority = Integer.MIN_VALUE;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Blocks until the passed priority is the lowest one (i.e. highest priority).
|
|
||||||
*
|
|
||||||
* @param priority The priority of the task that would like to proceed.
|
|
||||||
*/
|
|
||||||
public void proceed(int priority) throws InterruptedException {
|
|
||||||
synchronized (lock) {
|
|
||||||
while (highestPriority < priority) {
|
|
||||||
lock.wait();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A non-blocking variant of {@link #proceed(int)}.
|
|
||||||
*
|
|
||||||
* @param priority The priority of the task that would like to proceed.
|
|
||||||
* @return Whether the passed priority is allowed to proceed.
|
|
||||||
*/
|
|
||||||
public boolean proceedNonBlocking(int priority) {
|
|
||||||
synchronized (lock) {
|
|
||||||
return highestPriority >= priority;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A throwing variant of {@link #proceed(int)}.
|
|
||||||
*
|
|
||||||
* @param priority The priority of the task that would like to proceed.
|
|
||||||
* @throws PriorityTooLowException If the passed priority is not high enough to proceed.
|
|
||||||
*/
|
|
||||||
public void proceedOrThrow(int priority) throws PriorityTooLowException {
|
|
||||||
synchronized (lock) {
|
|
||||||
if (highestPriority < priority) {
|
|
||||||
throw new PriorityTooLowException(priority, highestPriority);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -115,7 +61,47 @@ public final class NetworkLock {
|
|||||||
public void add(int priority) {
|
public void add(int priority) {
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
queue.add(priority);
|
queue.add(priority);
|
||||||
highestPriority = Math.min(highestPriority, priority);
|
highestPriority = Math.max(highestPriority, priority);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Blocks until the task is allowed to proceed.
|
||||||
|
*
|
||||||
|
* @param priority The priority of the task.
|
||||||
|
* @throws InterruptedException If the thread is interrupted.
|
||||||
|
*/
|
||||||
|
public void proceed(int priority) throws InterruptedException {
|
||||||
|
synchronized (lock) {
|
||||||
|
while (highestPriority != priority) {
|
||||||
|
lock.wait();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A non-blocking variant of {@link #proceed(int)}.
|
||||||
|
*
|
||||||
|
* @param priority The priority of the task.
|
||||||
|
* @return Whether the task is allowed to proceed.
|
||||||
|
*/
|
||||||
|
public boolean proceedNonBlocking(int priority) {
|
||||||
|
synchronized (lock) {
|
||||||
|
return highestPriority == priority;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A throwing variant of {@link #proceed(int)}.
|
||||||
|
*
|
||||||
|
* @param priority The priority of the task.
|
||||||
|
* @throws PriorityTooLowException If the task is not allowed to proceed.
|
||||||
|
*/
|
||||||
|
public void proceedOrThrow(int priority) throws PriorityTooLowException {
|
||||||
|
synchronized (lock) {
|
||||||
|
if (highestPriority != priority) {
|
||||||
|
throw new PriorityTooLowException(priority, highestPriority);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,7 +113,7 @@ public final class NetworkLock {
|
|||||||
public void remove(int priority) {
|
public void remove(int priority) {
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
queue.remove(priority);
|
queue.remove(priority);
|
||||||
highestPriority = queue.isEmpty() ? Integer.MAX_VALUE : queue.peek();
|
highestPriority = queue.isEmpty() ? Integer.MIN_VALUE : queue.peek();
|
||||||
lock.notifyAll();
|
lock.notifyAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user