Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
SDK
/
exoplayer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
c4e1c354
authored
Sep 19, 2014
by
Oliver Woodman
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Enhance Loader API.
parent
ce5eea72
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
23 deletions
library/src/main/java/com/google/android/exoplayer/chunk/ChunkSampleSource.java
library/src/main/java/com/google/android/exoplayer/upstream/Loader.java
library/src/main/java/com/google/android/exoplayer/chunk/ChunkSampleSource.java
View file @
c4e1c354
...
...
@@ -24,6 +24,7 @@ import com.google.android.exoplayer.SampleSource;
import
com.google.android.exoplayer.TrackInfo
;
import
com.google.android.exoplayer.TrackRenderer
;
import
com.google.android.exoplayer.upstream.Loader
;
import
com.google.android.exoplayer.upstream.Loader.Loadable
;
import
com.google.android.exoplayer.util.Assertions
;
import
android.os.Handler
;
...
...
@@ -39,7 +40,7 @@ import java.util.List;
* A {@link SampleSource} that loads media in {@link Chunk}s, which are themselves obtained from a
* {@link ChunkSource}.
*/
public
class
ChunkSampleSource
implements
SampleSource
,
Loader
.
Listener
{
public
class
ChunkSampleSource
implements
SampleSource
,
Loader
.
Callback
{
/**
* Interface definition for a callback to be notified of {@link ChunkSampleSource} events.
...
...
@@ -199,7 +200,7 @@ public class ChunkSampleSource implements SampleSource, Loader.Listener {
@Override
public
boolean
prepare
()
{
Assertions
.
checkState
(
state
==
STATE_UNPREPARED
);
loader
=
new
Loader
(
"Loader:"
+
chunkSource
.
getTrackInfo
().
mimeType
,
this
);
loader
=
new
Loader
(
"Loader:"
+
chunkSource
.
getTrackInfo
().
mimeType
);
state
=
STATE_PREPARED
;
return
true
;
}
...
...
@@ -413,7 +414,7 @@ public class ChunkSampleSource implements SampleSource, Loader.Listener {
}
@Override
public
void
onLoad
ed
(
)
{
public
void
onLoad
Completed
(
Loadable
loadable
)
{
Chunk
currentLoadable
=
currentLoadableHolder
.
chunk
;
notifyLoadCompleted
(
currentLoadable
.
bytesLoaded
());
try
{
...
...
@@ -436,7 +437,7 @@ public class ChunkSampleSource implements SampleSource, Loader.Listener {
}
@Override
public
void
on
Canceled
(
)
{
public
void
on
LoadCanceled
(
Loadable
loadable
)
{
Chunk
currentLoadable
=
currentLoadableHolder
.
chunk
;
notifyLoadCanceled
(
currentLoadable
.
bytesLoaded
());
if
(!
isMediaChunk
(
currentLoadable
))
{
...
...
@@ -452,7 +453,7 @@ public class ChunkSampleSource implements SampleSource, Loader.Listener {
}
@Override
public
void
on
Error
(
IOException
e
)
{
public
void
on
LoadError
(
Loadable
loadable
,
IOException
e
)
{
currentLoadableException
=
e
;
currentLoadableExceptionCount
++;
currentLoadableExceptionTimestamp
=
SystemClock
.
elapsedRealtime
();
...
...
@@ -553,7 +554,7 @@ public class ChunkSampleSource implements SampleSource, Loader.Listener {
discardUpstreamMediaChunks
(
currentLoadableHolder
.
queueSize
);
if
(
currentLoadableHolder
.
chunk
==
backedOffChunk
)
{
// Chunk was unchanged. Resume loading.
loader
.
startLoading
(
backedOffChunk
);
loader
.
startLoading
(
backedOffChunk
,
this
);
}
else
{
backedOffChunk
.
release
();
maybeStartLoading
();
...
...
@@ -564,7 +565,7 @@ public class ChunkSampleSource implements SampleSource, Loader.Listener {
if
(
backedOffChunk
==
mediaChunks
.
getFirst
())
{
// We're not able to clear the first media chunk, so we have no choice but to continue
// loading it.
loader
.
startLoading
(
backedOffChunk
);
loader
.
startLoading
(
backedOffChunk
,
this
);
return
;
}
...
...
@@ -579,7 +580,7 @@ public class ChunkSampleSource implements SampleSource, Loader.Listener {
if
(
currentLoadableHolder
.
chunk
==
backedOffChunk
)
{
// Chunk was unchanged. Resume loading.
loader
.
startLoading
(
backedOffChunk
);
loader
.
startLoading
(
backedOffChunk
,
this
);
}
else
{
// This call will remove and release at least one chunk from the end of mediaChunks. Since
// the current loadable is the last media chunk, it is guaranteed to be removed.
...
...
@@ -609,7 +610,7 @@ public class ChunkSampleSource implements SampleSource, Loader.Listener {
notifyLoadStarted
(
currentLoadable
.
format
.
id
,
currentLoadable
.
trigger
,
true
,
-
1
,
-
1
,
currentLoadable
.
getLength
());
}
loader
.
startLoading
(
currentLoadable
);
loader
.
startLoading
(
currentLoadable
,
this
);
}
/**
...
...
library/src/main/java/com/google/android/exoplayer/upstream/Loader.java
View file @
c4e1c354
...
...
@@ -20,6 +20,7 @@ import com.google.android.exoplayer.util.Util;
import
android.annotation.SuppressLint
;
import
android.os.Handler
;
import
android.os.Looper
;
import
android.os.Message
;
import
android.util.Log
;
...
...
@@ -72,22 +73,28 @@ public final class Loader {
/**
* Interface definition for a callback to be notified of {@link Loader} events.
*/
public
interface
Listener
{
public
interface
Callback
{
/**
* Invoked when loading has been canceled.
*
* @param loadable The loadable whose load has been canceled.
*/
void
on
Canceled
(
);
void
on
LoadCanceled
(
Loadable
loadable
);
/**
* Invoked when the data source has been fully loaded.
*
* @param loadable The loadable whose load has completed.
*/
void
onLoad
ed
(
);
void
onLoad
Completed
(
Loadable
loadable
);
/**
* Invoked when the data source is stopped due to an error.
*
* @param loadable The loadable whose load has failed.
*/
void
on
Error
(
IOException
exception
);
void
on
LoadError
(
Loadable
loadable
,
IOException
exception
);
}
...
...
@@ -95,18 +102,29 @@ public final class Loader {
private
static
final
int
MSG_ERROR
=
1
;
private
final
ExecutorService
downloadExecutorService
;
private
final
Listener
listener
;
private
LoadTask
currentTask
;
private
boolean
loading
;
/**
* @param threadName A name for the loader's thread.
* @param listener A listener to invoke when state changes occur.
*/
public
Loader
(
String
threadName
,
Listener
listener
)
{
public
Loader
(
String
threadName
)
{
this
.
downloadExecutorService
=
Util
.
newSingleThreadExecutor
(
threadName
);
this
.
listener
=
listener
;
}
/**
* Invokes {@link #startLoading(Looper, Loadable, Callback)}, using the {@link Looper}
* associated with the calling thread.
*
* @param loadable The {@link Loadable} to load.
* @param callback A callback to invoke when the load ends.
* @throws IllegalStateException If the calling thread does not have an associated {@link Looper}.
*/
public
void
startLoading
(
Loadable
loadable
,
Callback
callback
)
{
Looper
myLooper
=
Looper
.
myLooper
();
Assertions
.
checkState
(
myLooper
!=
null
);
startLoading
(
myLooper
,
loadable
,
callback
);
}
/**
...
...
@@ -115,12 +133,14 @@ public final class Loader {
* A {@link Loader} instance can only load one {@link Loadable} at a time, and so this method
* must not be called when another load is in progress.
*
* @param looper The looper of the thread on which the callback should be invoked.
* @param loadable The {@link Loadable} to load.
* @param callback A callback to invoke when the load ends.
*/
public
void
startLoading
(
Lo
adable
loadable
)
{
public
void
startLoading
(
Lo
oper
looper
,
Loadable
loadable
,
Callback
callback
)
{
Assertions
.
checkState
(!
loading
);
loading
=
true
;
currentTask
=
new
LoadTask
(
lo
adable
);
currentTask
=
new
LoadTask
(
lo
oper
,
loadable
,
callback
);
downloadExecutorService
.
submit
(
currentTask
);
}
...
...
@@ -161,11 +181,14 @@ public final class Loader {
private
static
final
String
TAG
=
"LoadTask"
;
private
final
Loadable
loadable
;
private
final
Loader
.
Callback
callback
;
private
volatile
Thread
executorThread
;
public
LoadTask
(
Loadable
loadable
)
{
public
LoadTask
(
Looper
looper
,
Loadable
loadable
,
Loader
.
Callback
callback
)
{
super
(
looper
);
this
.
loadable
=
loadable
;
this
.
callback
=
callback
;
}
public
void
quit
()
{
...
...
@@ -200,15 +223,15 @@ public final class Loader {
public
void
handleMessage
(
Message
msg
)
{
onFinished
();
if
(
loadable
.
isLoadCanceled
())
{
listener
.
onCanceled
(
);
callback
.
onLoadCanceled
(
loadable
);
return
;
}
switch
(
msg
.
what
)
{
case
MSG_END_OF_SOURCE:
listener
.
onLoaded
(
);
callback
.
onLoadCompleted
(
loadable
);
break
;
case
MSG_ERROR:
listener
.
onError
(
(
IOException
)
msg
.
obj
);
callback
.
onLoadError
(
loadable
,
(
IOException
)
msg
.
obj
);
break
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment