This commit is contained in:
Karol Wrótniak 2017-06-19 12:46:09 +02:00
parent 8caad492d4
commit 50da6d870c
No known key found for this signature in database
GPG key ID: F8BE40B77582D123
5 changed files with 61 additions and 38 deletions

View file

@ -0,0 +1,8 @@
package com.google.android.exoplayer2.upstream;
final class AndroidDataSourceConstants {
static final long SAMPLE_MP4_BYTES = 101597;
static final String SAMPLE_MP4_PATH = "/mp4/sample.mp4";
private AndroidDataSourceConstants() {}
}

View file

@ -1,31 +0,0 @@
package com.google.android.exoplayer2.upstream;
import android.content.Context;
import android.net.Uri;
import android.test.InstrumentationTestCase;
public class AndroidDataSourceTest extends InstrumentationTestCase {
private static final long SAMPLE_MP4_BYTES = 101597;
private static final String SAMPLE_MP4_PATH = "/mp4/sample.mp4";
public void testAssetDataSource() throws Exception {
final Context context = getInstrumentation().getContext();
AssetDataSource dataSource = new AssetDataSource(context);
Uri assetUri = Uri.parse("file:///android_asset" + SAMPLE_MP4_PATH);
DataSpec dataSpec = new DataSpec(assetUri);
long sourceLengthBytes = dataSource.open(dataSpec);
assertEquals(SAMPLE_MP4_BYTES, sourceLengthBytes);
}
public void testContentDataSource() throws Exception {
Context context = getInstrumentation().getContext();
ContentDataSource dataSource = new ContentDataSource(context);
Uri contentUri = Uri.parse("content://exoplayer" + SAMPLE_MP4_PATH);
DataSpec dataSpec = new DataSpec(contentUri);
long sourceLengthBytes = dataSource.open(dataSpec);
assertEquals(SAMPLE_MP4_BYTES, sourceLengthBytes);
}
}

View file

@ -0,0 +1,21 @@
package com.google.android.exoplayer2.upstream;
import android.content.Context;
import android.net.Uri;
import android.test.InstrumentationTestCase;
import static com.google.android.exoplayer2.upstream.AndroidDataSourceConstants.SAMPLE_MP4_BYTES;
import static com.google.android.exoplayer2.upstream.AndroidDataSourceConstants.SAMPLE_MP4_PATH;
public class AssetDataSourceTest extends InstrumentationTestCase {
public void testAssetDataSource() throws Exception {
final Context context = getInstrumentation().getContext();
AssetDataSource dataSource = new AssetDataSource(context);
Uri assetUri = Uri.parse("file:///android_asset" + SAMPLE_MP4_PATH);
DataSpec dataSpec = new DataSpec(assetUri);
long sourceLengthBytes = dataSource.open(dataSpec);
assertEquals(SAMPLE_MP4_BYTES, sourceLengthBytes);
}
}

View file

@ -0,0 +1,21 @@
package com.google.android.exoplayer2.upstream;
import android.content.Context;
import android.net.Uri;
import android.test.InstrumentationTestCase;
import static com.google.android.exoplayer2.upstream.AndroidDataSourceConstants.SAMPLE_MP4_BYTES;
import static com.google.android.exoplayer2.upstream.AndroidDataSourceConstants.SAMPLE_MP4_PATH;
public class ContentDataSourceTest extends InstrumentationTestCase {
public void testContentDataSource() throws Exception {
Context context = getInstrumentation().getContext();
ContentDataSource dataSource = new ContentDataSource(context);
Uri contentUri = Uri.parse("content://exoplayer" + SAMPLE_MP4_PATH);
DataSpec dataSpec = new DataSpec(contentUri);
long sourceLengthBytes = dataSource.open(dataSpec);
assertEquals(SAMPLE_MP4_BYTES, sourceLengthBytes);
}
}

View file

@ -71,7 +71,7 @@ public final class ContentDataSource implements DataSource {
try {
uri = dataSpec.uri;
assetFileDescriptor = resolver.openAssetFileDescriptor(uri, "r");
inputStream = assetFileDescriptor.createInputStream();
inputStream = new FileInputStream(assetFileDescriptor.getFileDescriptor());
long skipped = inputStream.skip(dataSpec.position);
if (skipped < dataSpec.position) {
// We expect the skip to be satisfied in full. If it isn't then we're probably trying to
@ -81,12 +81,16 @@ public final class ContentDataSource implements DataSource {
if (dataSpec.length != C.LENGTH_UNSET) {
bytesRemaining = dataSpec.length;
} else {
bytesRemaining = inputStream.available();
if (bytesRemaining == 0) {
// FileInputStream.available() returns 0 if the remaining length cannot be determined, or
// if it's greater than Integer.MAX_VALUE. We don't know the true length in either case,
// so treat as unbounded.
bytesRemaining = C.LENGTH_UNSET;
bytesRemaining = assetFileDescriptor.getLength();
if (bytesRemaining == AssetFileDescriptor.UNKNOWN_LENGTH) {
// The asset must extend to the end of the file.
bytesRemaining = inputStream.available();
if (bytesRemaining == 0) {
// FileInputStream.available() returns 0 if the remaining length cannot be determined, or
// if it's greater than Integer.MAX_VALUE. We don't know the true length in either case,
// so treat as unbounded.
bytesRemaining = C.LENGTH_UNSET;
}
}
}
} catch (IOException e) {