Add transferInitializing to BaseDataSource.

This allows implementations to notify when the transfer is about to be started.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=203102117
This commit is contained in:
tonihei 2018-07-03 04:35:45 -07:00 committed by Oliver Woodman
parent 985160a47d
commit 55ce085a0d
13 changed files with 25 additions and 3 deletions

View file

@ -301,6 +301,7 @@ public class CronetDataSource extends BaseDataSource implements HttpDataSource {
} }
currentUrlRequest.start(); currentUrlRequest.start();
transferInitializing(dataSpec);
try { try {
boolean connectionOpened = blockUntilConnectTimeout(); boolean connectionOpened = blockUntilConnectTimeout();
if (exception != null) { if (exception != null) {

View file

@ -165,6 +165,7 @@ public class OkHttpDataSource extends BaseDataSource implements HttpDataSource {
this.dataSpec = dataSpec; this.dataSpec = dataSpec;
this.bytesRead = 0; this.bytesRead = 0;
this.bytesSkipped = 0; this.bytesSkipped = 0;
transferInitializing(dataSpec);
Request request = makeRequest(dataSpec); Request request = makeRequest(dataSpec);
try { try {
response = callFactory.newCall(request).execute(); response = callFactory.newCall(request).execute();

View file

@ -51,6 +51,7 @@ public final class RtmpDataSource extends BaseDataSource {
@Override @Override
public long open(DataSpec dataSpec) throws RtmpIOException { public long open(DataSpec dataSpec) throws RtmpIOException {
transferInitializing(dataSpec);
rtmpClient = new RtmpClient(); rtmpClient = new RtmpClient();
rtmpClient.open(dataSpec.uri.toString(), false); rtmpClient.open(dataSpec.uri.toString(), false);

View file

@ -74,6 +74,7 @@ public final class AssetDataSource extends BaseDataSource {
} else if (path.startsWith("/")) { } else if (path.startsWith("/")) {
path = path.substring(1); path = path.substring(1);
} }
transferInitializing(dataSpec);
inputStream = assetManager.open(path, AssetManager.ACCESS_RANDOM); inputStream = assetManager.open(path, AssetManager.ACCESS_RANDOM);
long skipped = inputStream.skip(dataSpec.position); long skipped = inputStream.skip(dataSpec.position);
if (skipped < dataSpec.position) { if (skipped < dataSpec.position) {

View file

@ -20,8 +20,9 @@ import java.util.ArrayList;
/** /**
* Base {@link DataSource} implementation to keep a list of {@link TransferListener}s. * Base {@link DataSource} implementation to keep a list of {@link TransferListener}s.
* *
* <p>Subclasses must call {@link #transferStarted(DataSpec)}, {@link #bytesTransferred(int)}, and * <p>Subclasses must call {@link #transferInitializing(DataSpec)}, {@link
* {@link #transferEnded()} to inform listeners of data transfers. * #transferStarted(DataSpec)}, {@link #bytesTransferred(int)}, and {@link #transferEnded()} to
* inform listeners of data transfers.
*/ */
public abstract class BaseDataSource implements DataSource { public abstract class BaseDataSource implements DataSource {
@ -43,6 +44,15 @@ public abstract class BaseDataSource implements DataSource {
listeners.add(transferListener); listeners.add(transferListener);
} }
/**
* Notifies listeners that data transfer for the specified {@link DataSpec} is being initialized.
*
* @param dataSpec {@link DataSpec} describing the data for initializing transfer.
*/
protected final void transferInitializing(DataSpec dataSpec) {
// TODO: notify listeners.
}
/** /**
* Notifies listeners that data transfer for the specified {@link DataSpec} started. * Notifies listeners that data transfer for the specified {@link DataSpec} started.
* *

View file

@ -44,6 +44,7 @@ public final class ByteArrayDataSource extends BaseDataSource {
@Override @Override
public long open(DataSpec dataSpec) throws IOException { public long open(DataSpec dataSpec) throws IOException {
uri = dataSpec.uri; uri = dataSpec.uri;
transferInitializing(dataSpec);
readPosition = (int) dataSpec.position; readPosition = (int) dataSpec.position;
bytesRemaining = (int) ((dataSpec.length == C.LENGTH_UNSET) bytesRemaining = (int) ((dataSpec.length == C.LENGTH_UNSET)
? (data.length - dataSpec.position) : dataSpec.length); ? (data.length - dataSpec.position) : dataSpec.length);

View file

@ -73,6 +73,7 @@ public final class ContentDataSource extends BaseDataSource {
public long open(DataSpec dataSpec) throws ContentDataSourceException { public long open(DataSpec dataSpec) throws ContentDataSourceException {
try { try {
uri = dataSpec.uri; uri = dataSpec.uri;
transferInitializing(dataSpec);
assetFileDescriptor = resolver.openAssetFileDescriptor(uri, "r"); assetFileDescriptor = resolver.openAssetFileDescriptor(uri, "r");
if (assetFileDescriptor == null) { if (assetFileDescriptor == null) {
throw new FileNotFoundException("Could not open file descriptor for: " + uri); throw new FileNotFoundException("Could not open file descriptor for: " + uri);

View file

@ -39,6 +39,7 @@ public final class DataSchemeDataSource extends BaseDataSource {
@Override @Override
public long open(DataSpec dataSpec) throws IOException { public long open(DataSpec dataSpec) throws IOException {
transferInitializing(dataSpec);
this.dataSpec = dataSpec; this.dataSpec = dataSpec;
Uri uri = dataSpec.uri; Uri uri = dataSpec.uri;
String scheme = uri.getScheme(); String scheme = uri.getScheme();

View file

@ -201,6 +201,7 @@ public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSou
this.dataSpec = dataSpec; this.dataSpec = dataSpec;
this.bytesRead = 0; this.bytesRead = 0;
this.bytesSkipped = 0; this.bytesSkipped = 0;
transferInitializing(dataSpec);
try { try {
connection = makeConnection(dataSpec); connection = makeConnection(dataSpec);
} catch (IOException e) { } catch (IOException e) {

View file

@ -57,6 +57,7 @@ public final class FileDataSource extends BaseDataSource {
public long open(DataSpec dataSpec) throws FileDataSourceException { public long open(DataSpec dataSpec) throws FileDataSourceException {
try { try {
uri = dataSpec.uri; uri = dataSpec.uri;
transferInitializing(dataSpec);
file = new RandomAccessFile(dataSpec.uri.getPath(), "r"); file = new RandomAccessFile(dataSpec.uri.getPath(), "r");
file.seek(dataSpec.position); file.seek(dataSpec.position);
bytesRemaining = dataSpec.length == C.LENGTH_UNSET ? file.length() - dataSpec.position bytesRemaining = dataSpec.length == C.LENGTH_UNSET ? file.length() - dataSpec.position

View file

@ -105,6 +105,7 @@ public final class RawResourceDataSource extends BaseDataSource {
throw new RawResourceDataSourceException("Resource identifier must be an integer."); throw new RawResourceDataSourceException("Resource identifier must be an integer.");
} }
transferInitializing(dataSpec);
assetFileDescriptor = resources.openRawResourceFd(resourceId); assetFileDescriptor = resources.openRawResourceFd(resourceId);
inputStream = new FileInputStream(assetFileDescriptor.getFileDescriptor()); inputStream = new FileInputStream(assetFileDescriptor.getFileDescriptor());
inputStream.skip(assetFileDescriptor.getStartOffset()); inputStream.skip(assetFileDescriptor.getStartOffset());

View file

@ -100,7 +100,7 @@ public final class UdpDataSource extends BaseDataSource {
uri = dataSpec.uri; uri = dataSpec.uri;
String host = uri.getHost(); String host = uri.getHost();
int port = uri.getPort(); int port = uri.getPort();
transferInitializing(dataSpec);
try { try {
address = InetAddress.getByName(host); address = InetAddress.getByName(host);
socketAddress = new InetSocketAddress(address, port); socketAddress = new InetSocketAddress(address, port);

View file

@ -104,10 +104,12 @@ public class FakeDataSource extends BaseDataSource {
public final long open(DataSpec dataSpec) throws IOException { public final long open(DataSpec dataSpec) throws IOException {
Assertions.checkState(!openCalled); Assertions.checkState(!openCalled);
openCalled = true; openCalled = true;
// DataSpec requires a matching close call even if open fails. // DataSpec requires a matching close call even if open fails.
uri = dataSpec.uri; uri = dataSpec.uri;
openedDataSpecs.add(dataSpec); openedDataSpecs.add(dataSpec);
transferInitializing(dataSpec);
fakeData = fakeDataSet.getData(uri.toString()); fakeData = fakeDataSet.getData(uri.toString());
if (fakeData == null) { if (fakeData == null) {
throw new IOException("Data not found: " + dataSpec.uri); throw new IOException("Data not found: " + dataSpec.uri);