# FakeChunkSource

Artifact: [androidx.media3:media3-test-utils](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/test/utils/FakeChunkSource.java+class:androidx.media3.test.utils.FakeChunkSource)

*** ** * ** ***


```
@UnstableApi
public class FakeChunkSource implements ChunkSource
```

<br />

*** ** * ** ***

Fake `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/ChunkSource` with adaptive media chunks of a given duration.

## Summary

| ### Nested types |
|---|
| `public class https://developer.android.com/reference/androidx/media3/test/utils/FakeChunkSource.Factory` Factory for a `https://developer.android.com/reference/androidx/media3/test/utils/FakeChunkSource`. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/media3/test/utils/FakeChunkSource#FakeChunkSource(androidx.media3.exoplayer.trackselection.ExoTrackSelection,androidx.media3.datasource.DataSource,androidx.media3.test.utils.FakeAdaptiveDataSet)( https://developer.android.com/reference/androidx/media3/exoplayer/trackselection/ExoTrackSelection trackSelection, https://developer.android.com/reference/androidx/media3/datasource/DataSource dataSource, https://developer.android.com/reference/androidx/media3/test/utils/FakeAdaptiveDataSet dataSet )` |

| ### Public methods |
|---|---|
| `long` | `https://developer.android.com/reference/androidx/media3/test/utils/FakeChunkSource#getAdjustedSeekPositionUs(long,androidx.media3.exoplayer.SeekParameters)( long positionUs, https://developer.android.com/reference/androidx/media3/exoplayer/SeekParameters seekParameters )` Adjusts a seek position given the specified `https://developer.android.com/reference/androidx/media3/exoplayer/SeekParameters`. |
| `void` | `https://developer.android.com/reference/androidx/media3/test/utils/FakeChunkSource#getNextChunk(androidx.media3.exoplayer.LoadingInfo,long,java.util.List<? extends androidx.media3.exoplayer.source.chunk.MediaChunk>,androidx.media3.exoplayer.source.chunk.ChunkHolder)( https://developer.android.com/reference/androidx/media3/exoplayer/LoadingInfo loadingInfo, long loadPositionUs, https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/MediaChunk> queue, https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/ChunkHolder out )` Returns the next chunk to load. |
| `int` | `https://developer.android.com/reference/androidx/media3/test/utils/FakeChunkSource#getPreferredQueueSize(long,java.util.List<? extends androidx.media3.exoplayer.source.chunk.MediaChunk>)(long playbackPositionUs, https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/MediaChunk> queue)` Evaluates whether `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/MediaChunk`s should be removed from the back of the queue. |
| `void` | `https://developer.android.com/reference/androidx/media3/test/utils/FakeChunkSource#maybeThrowError()()` If the source is currently having difficulty providing chunks, then this method throws the underlying error. |
| `void` | `https://developer.android.com/reference/androidx/media3/test/utils/FakeChunkSource#onChunkLoadCompleted(androidx.media3.exoplayer.source.chunk.Chunk)(https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/Chunk chunk)` Called when the `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/ChunkSampleStream` has finished loading a chunk obtained from this source. |
| `boolean` | `https://developer.android.com/reference/androidx/media3/test/utils/FakeChunkSource#onChunkLoadError(androidx.media3.exoplayer.source.chunk.Chunk,boolean,androidx.media3.exoplayer.upstream.LoadErrorHandlingPolicy.LoadErrorInfo,androidx.media3.exoplayer.upstream.LoadErrorHandlingPolicy)( https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/Chunk chunk, boolean cancelable, https://developer.android.com/reference/androidx/media3/exoplayer/upstream/LoadErrorHandlingPolicy.LoadErrorInfo loadErrorInfo, https://developer.android.com/reference/androidx/media3/exoplayer/upstream/LoadErrorHandlingPolicy loadErrorHandlingPolicy )` Called when the `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/ChunkSampleStream` encounters an error loading a chunk obtained from this source. |
| `void` | `https://developer.android.com/reference/androidx/media3/test/utils/FakeChunkSource#release()()` Releases any held resources. |
| `boolean` | `https://developer.android.com/reference/androidx/media3/test/utils/FakeChunkSource#shouldCancelLoad(long,androidx.media3.exoplayer.source.chunk.Chunk,java.util.List<? extends androidx.media3.exoplayer.source.chunk.MediaChunk>)( long playbackPositionUs, https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/Chunk loadingChunk, https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/MediaChunk> queue )` Returns whether an ongoing load of a chunk should be canceled. |

| ### Protected methods |
|---|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/MediaChunk` | `https://developer.android.com/reference/androidx/media3/test/utils/FakeChunkSource#createMediaChunk(androidx.media3.datasource.DataSource,androidx.media3.datasource.DataSpec,androidx.media3.common.Format,int,java.lang.Object,long,long,long,int,androidx.media3.common.Format)( https://developer.android.com/reference/androidx/media3/datasource/DataSource dataSource, https://developer.android.com/reference/androidx/media3/datasource/DataSpec dataSpec, https://developer.android.com/reference/androidx/media3/common/Format trackFormat, @https://developer.android.com/reference/androidx/media3/common/C.SelectionReason int trackSelectionReason, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/Object.html trackSelectionData, long startTimeUs, long endTimeUs, long chunkIndex, @https://developer.android.com/reference/androidx/media3/common/C.TrackType int trackType, https://developer.android.com/reference/androidx/media3/common/Format sampleFormat )` Creates a `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/MediaChunk` for the given parameters. |

## Public constructors

### FakeChunkSource

```
public FakeChunkSource(
    ExoTrackSelection trackSelection,
    DataSource dataSource,
    FakeAdaptiveDataSet dataSet
)
```

## Public methods

### getAdjustedSeekPositionUs

```
public long getAdjustedSeekPositionUs(
    long positionUs,
    SeekParameters seekParameters
)
```

Adjusts a seek position given the specified `https://developer.android.com/reference/androidx/media3/exoplayer/SeekParameters`. Chunk boundaries are used as sync points.

| Parameters |
|---|---|
| `long positionUs` | The seek position in microseconds. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/SeekParameters seekParameters` | Parameters that control how the seek is performed. |

| Returns |
|---|---|
| `long` | The adjusted seek position, in microseconds. |

### getNextChunk

```
public void getNextChunk(
    LoadingInfo loadingInfo,
    long loadPositionUs,
    List<MediaChunk> queue,
    ChunkHolder out
)
```

Returns the next chunk to load.

If a chunk is available then `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/ChunkHolder#chunk()` is set. If the end of the stream has been reached then `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/ChunkHolder#endOfStream()` is set. If a chunk is not available but the end of the stream has not been reached, the `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/ChunkHolder` is not modified.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/LoadingInfo loadingInfo` | The `https://developer.android.com/reference/androidx/media3/exoplayer/LoadingInfo` when loading request is made. |
| `long loadPositionUs` | The current load position in microseconds. If `queue` is empty, this is the starting position from which chunks should be provided. Else it's equal to `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/Chunk#endTimeUs()` of the last chunk in the `queue`. |
| `https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/MediaChunk> queue` | The queue of buffered `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/MediaChunk`s. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/ChunkHolder out` | A holder to populate. |

### getPreferredQueueSize

```
public int getPreferredQueueSize(long playbackPositionUs, List<MediaChunk> queue)
```

Evaluates whether `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/MediaChunk`s should be removed from the back of the queue.

Removing `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/MediaChunk`s from the back of the queue can be useful if they could be replaced with chunks of a significantly higher quality (e.g. because the available bandwidth has substantially increased).

Will only be called if no `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/MediaChunk` in the queue is currently loading.

| Parameters |
|---|---|
| `long playbackPositionUs` | The current playback position, in microseconds. |
| `https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/MediaChunk> queue` | The queue of buffered `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/MediaChunk`s. |

| Returns |
|---|---|
| `int` | The preferred queue size. |

### maybeThrowError

```
public void maybeThrowError()
```

If the source is currently having difficulty providing chunks, then this method throws the underlying error. Otherwise does nothing.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/io/IOException.html` | The underlying error. |

### onChunkLoadCompleted

```
public void onChunkLoadCompleted(Chunk chunk)
```

Called when the `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/ChunkSampleStream` has finished loading a chunk obtained from this source.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/Chunk chunk` | The chunk whose load has been completed. |

### onChunkLoadError

```
public boolean onChunkLoadError(
    Chunk chunk,
    boolean cancelable,
    LoadErrorHandlingPolicy.LoadErrorInfo loadErrorInfo,
    LoadErrorHandlingPolicy loadErrorHandlingPolicy
)
```

Called when the `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/ChunkSampleStream` encounters an error loading a chunk obtained from this source.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/Chunk chunk` | The chunk whose load encountered the error. |
| `boolean cancelable` | Whether the load can be canceled. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/LoadErrorHandlingPolicy.LoadErrorInfo loadErrorInfo` | The load error info. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/LoadErrorHandlingPolicy loadErrorHandlingPolicy` | The load error handling policy to customize the behaviour of handling the load error. |

| Returns |
|---|---|
| `boolean` | Whether the load should be canceled so that a replacement chunk can be loaded instead. Must be `false` if `cancelable` is `false`. If `true`, `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/ChunkSource#getNextChunk(androidx.media3.exoplayer.LoadingInfo,long,java.util.List<? extends androidx.media3.exoplayer.source.chunk.MediaChunk>,androidx.media3.exoplayer.source.chunk.ChunkHolder)` will be called to obtain the replacement chunk. |

### release

```
public void release()
```

Releases any held resources.

### shouldCancelLoad

```
public boolean shouldCancelLoad(
    long playbackPositionUs,
    Chunk loadingChunk,
    List<MediaChunk> queue
)
```

Returns whether an ongoing load of a chunk should be canceled.

| Parameters |
|---|---|
| `long playbackPositionUs` | The current playback position, in microseconds. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/Chunk loadingChunk` | The currently loading `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/Chunk`. |
| `https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/MediaChunk> queue` | The queue of buffered `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/MediaChunk`. |

| Returns |
|---|---|
| `boolean` | Whether the ongoing load of `loadingChunk` should be canceled. |

## Protected methods

### createMediaChunk

```
protected MediaChunk createMediaChunk(
    DataSource dataSource,
    DataSpec dataSpec,
    Format trackFormat,
    @C.SelectionReason int trackSelectionReason,
    @Nullable Object trackSelectionData,
    long startTimeUs,
    long endTimeUs,
    long chunkIndex,
    @C.TrackType int trackType,
    Format sampleFormat
)
```

Creates a `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/MediaChunk` for the given parameters.

Subclasses can override this method to return custom `https://developer.android.com/reference/androidx/media3/exoplayer/source/chunk/MediaChunk` implementations.