# FakeSampleStream

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/FakeSampleStream.java+class:androidx.media3.test.utils.FakeSampleStream)

*** ** * ** ***


```
@UnstableApi
public class FakeSampleStream implements SampleStream
```

<br />

*** ** * ** ***

Fake `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleStream` that outputs a given `https://developer.android.com/reference/androidx/media3/common/Format` and any amount of `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream.FakeSampleStreamItem`.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream.FakeSampleStreamItem` Item to customize a return value of `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleStream#readData(androidx.media3.exoplayer.FormatHolder,androidx.media3.decoder.DecoderInputBuffer,int)`. |

| ### Public fields |
|---|---|
| `int` | `@https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleStream.Flags https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream#flags()` |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream#FakeSampleStream(androidx.media3.exoplayer.upstream.Allocator,androidx.media3.exoplayer.source.MediaSourceEventListener.EventDispatcher,androidx.media3.exoplayer.drm.DrmSessionManager,androidx.media3.exoplayer.drm.DrmSessionEventListener.EventDispatcher,androidx.media3.common.Format,java.util.List<androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem>)( https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocator allocator, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSourceEventListener.EventDispatcher mediaSourceEventDispatcher, https://developer.android.com/reference/androidx/media3/exoplayer/drm/DrmSessionManager drmSessionManager, https://developer.android.com/reference/androidx/media3/exoplayer/drm/DrmSessionEventListener.EventDispatcher drmEventDispatcher, https://developer.android.com/reference/androidx/media3/common/Format initialFormat, https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream.FakeSampleStreamItem> fakeSampleStreamItems )` Creates a fake sample stream which outputs the given `https://developer.android.com/reference/androidx/media3/common/Format` followed by the provided `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream.FakeSampleStreamItem`. |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream#append(java.util.List<androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem>)(https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream.FakeSampleStreamItem> items)` Appends `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream.FakeSampleStreamItem` to the list of items that should be written to the queue. |
| `void` | `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream#discardTo(long,boolean)(long positionUs, boolean toKeyframe)` Discards data from the queue. |
| `int` | `@https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleStream.Flags https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream#getFlags()()` Returns the `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleStream.Flags` set on this stream. |
| `long` | `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream#getLargestQueuedTimestampUs()()` Returns the timestamp of the largest queued sample in the queue, or `https://developer.android.com/reference/java/lang/Long.html#MIN_VALUE` if no samples are queued. |
| `boolean` | `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream#isLoadingFinished()()` Returns whether data has been written to the sample queue until the end of stream signal. |
| `boolean` | `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream#isReady()()` Returns whether data is available to be read. |
| `void` | `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream#maybeThrowError()()` Throws an error that's preventing data from being read. |
| `int` | `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream#readData(androidx.media3.exoplayer.FormatHolder,androidx.media3.decoder.DecoderInputBuffer,int)( https://developer.android.com/reference/androidx/media3/exoplayer/FormatHolder formatHolder, https://developer.android.com/reference/androidx/media3/decoder/DecoderInputBuffer buffer, @https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleStream.ReadFlags int readFlags )` Attempts to read from the stream. |
| `void` | `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream#release()()` Release the stream and its underlying sample queue. |
| `void` | `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream#reset()()` Resets the sample queue. |
| `boolean` | `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream#seekToUs(long,boolean)(long positionUs, boolean allowTimeBeyondBuffer)` Seeks the stream to a new position using already available data in the queue. |
| `void` | `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream#setFlags(int)(@https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleStream.Flags int flags)` Sets the flags to be returned by `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream#getFlags()`. |
| `int` | `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream#skipData(long)(long positionUs)` Attempts to skip to the keyframe before the specified position, or to the end of the stream if `positionUs` is beyond it. |
| `void` | `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream#writeData(long)(long startPositionUs)` Writes all not yet written `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream.FakeSampleStreamItem` to the sample queue starting at the given position. |

| ### Inherited Constants |
|---|
| From [androidx.media3.exoplayer.source.SampleStream](https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleStream) |---|---| | `static final int` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleStream#FLAG_HAS_PREROLL() = 4` Specifies that the stream contains a preroll of decode-only samples. | | `static final int` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleStream#FLAG_MAYBE_HAS_PREROLL() = 2` Specifies that the stream may contain preroll samples. | | `static final int` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleStream#FLAG_OMIT_SAMPLE_DATA() = 4` Specifies that `https://developer.android.com/reference/androidx/media3/decoder/DecoderInputBuffer#data()`, `https://developer.android.com/reference/androidx/media3/decoder/DecoderInputBuffer#supplementalData()` and `https://developer.android.com/reference/androidx/media3/decoder/DecoderInputBuffer#cryptoInfo()` should not be populated when reading a sample buffer. | | `static final int` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleStream#FLAG_PEEK() = 1` Specifies that the read position should not be advanced if a sample buffer is read. | | `static final int` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleStream#FLAG_REQUIRE_FORMAT() = 2` Specifies that if a sample buffer would normally be read next, the format of the stream should be read instead. | | `static final int` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleStream#FLAG_STRICT_DURATION() = 1` Specifies that playout of the stream should be stopped once the duration is reached. | |

## Public fields

### flags

```
@SampleStream.Flags
public int flags
```

## Public constructors

### FakeSampleStream

```
public FakeSampleStream(
    Allocator allocator,
    @Nullable MediaSourceEventListener.EventDispatcher mediaSourceEventDispatcher,
    DrmSessionManager drmSessionManager,
    DrmSessionEventListener.EventDispatcher drmEventDispatcher,
    Format initialFormat,
    List<FakeSampleStream.FakeSampleStreamItem> fakeSampleStreamItems
)
```

Creates a fake sample stream which outputs the given `https://developer.android.com/reference/androidx/media3/common/Format` followed by the provided `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream.FakeSampleStreamItem`.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocator allocator` | An `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocator`. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSourceEventListener.EventDispatcher mediaSourceEventDispatcher` | A `https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSourceEventListener.EventDispatcher` to notify of media events. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/drm/DrmSessionManager drmSessionManager` | A `https://developer.android.com/reference/androidx/media3/exoplayer/drm/DrmSessionManager` for DRM interactions. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/drm/DrmSessionEventListener.EventDispatcher drmEventDispatcher` | A `https://developer.android.com/reference/androidx/media3/exoplayer/drm/DrmSessionEventListener.EventDispatcher` to notify of DRM events. |
| `https://developer.android.com/reference/androidx/media3/common/Format initialFormat` | The first `https://developer.android.com/reference/androidx/media3/common/Format` to output. |
| `https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream.FakeSampleStreamItem> fakeSampleStreamItems` | The `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream.FakeSampleStreamItem` to output. |

## Public methods

### append

```
public void append(List<FakeSampleStream.FakeSampleStreamItem> items)
```

Appends `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream.FakeSampleStreamItem` to the list of items that should be written to the queue.

Note that this data is only written to the queue once `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream#writeData(long)` is called.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream.FakeSampleStreamItem> items` | The items to append. |

### discardTo

```
public void discardTo(long positionUs, boolean toKeyframe)
```

Discards data from the queue.

| Parameters |
|---|---|
| `long positionUs` | The position to discard to, in microseconds. |
| `boolean toKeyframe` | Whether to discard to keyframes only. |

### getFlags

```
@SampleStream.Flags
public int getFlags()
```

Returns the `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleStream.Flags` set on this stream.

The value returned by this method is allowed to change during playback. Callers should check the flags when needed.

### getLargestQueuedTimestampUs

```
public long getLargestQueuedTimestampUs()
```

Returns the timestamp of the largest queued sample in the queue, or `https://developer.android.com/reference/java/lang/Long.html#MIN_VALUE` if no samples are queued.

### isLoadingFinished

```
public boolean isLoadingFinished()
```

Returns whether data has been written to the sample queue until the end of stream signal.

### isReady

```
public boolean isReady()
```

Returns whether data is available to be read.

Note: If the stream has ended then a buffer with the end of stream flag can always be read from `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleStream#readData(androidx.media3.exoplayer.FormatHolder,androidx.media3.decoder.DecoderInputBuffer,int)`. Hence an ended stream is always ready.

| Returns |
|---|---|
| `boolean` | Whether data is available to be read. |

### maybeThrowError

```
public void maybeThrowError()
```

Throws an error that's preventing data from being read. Does nothing if no such error exists.

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

### readData

```
public int readData(
    FormatHolder formatHolder,
    DecoderInputBuffer buffer,
    @SampleStream.ReadFlags int readFlags
)
```

Attempts to read from the stream.

If the stream has ended then `https://developer.android.com/reference/androidx/media3/common/C#BUFFER_FLAG_END_OF_STREAM()` flag is set on `
buffer` and `https://developer.android.com/reference/androidx/media3/common/C#RESULT_BUFFER_READ()` is returned. Else if no data is available then `https://developer.android.com/reference/androidx/media3/common/C#RESULT_NOTHING_READ()` is returned. Else if the format of the media is changing or if `
formatRequired` is set then `formatHolder` is populated and `https://developer.android.com/reference/androidx/media3/common/C#RESULT_FORMAT_READ()` is returned. Else `buffer` is populated and `https://developer.android.com/reference/androidx/media3/common/C#RESULT_BUFFER_READ()` is returned.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/FormatHolder formatHolder` | A `https://developer.android.com/reference/androidx/media3/exoplayer/FormatHolder` to populate in the case of reading a format. |
| `https://developer.android.com/reference/androidx/media3/decoder/DecoderInputBuffer buffer` | A `https://developer.android.com/reference/androidx/media3/decoder/DecoderInputBuffer` to populate in the case of reading a sample or the end of the stream. If the end of the stream has been reached, the `https://developer.android.com/reference/androidx/media3/common/C#BUFFER_FLAG_END_OF_STREAM()` flag will be set on the buffer. |
| `@https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleStream.ReadFlags int readFlags` | Flags controlling the behavior of this read operation. |

| Returns |
|---|---|
| `int` | The `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleStream.ReadDataResult` of the read operation. |

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/media3/decoder/DecoderInputBuffer.InsufficientCapacityException` | If the `buffer` has insufficient capacity to hold the data of a sample being read. The buffer `https://developer.android.com/reference/androidx/media3/decoder/DecoderInputBuffer#timeUs()` and flags are populated if this exception is thrown, but the read position is not advanced. |

### release

```
public void release()
```

Release the stream and its underlying sample queue.

### reset

```
public void reset()
```

Resets the sample queue.

A new call to `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream#writeData(long)` is required to fill the queue again.

### seekToUs

```
public boolean seekToUs(long positionUs, boolean allowTimeBeyondBuffer)
```

Seeks the stream to a new position using already available data in the queue.

| Parameters |
|---|---|
| `long positionUs` | The new position, in microseconds. |
| `boolean allowTimeBeyondBuffer` | Whether the operation can succeed if timeUs is beyond the end of the queue, by seeking to the last sample (or keyframe). |

| Returns |
|---|---|
| `boolean` | Whether seeking inside the available data was possible. |

### setFlags

```
public void setFlags(@SampleStream.Flags int flags)
```

Sets the flags to be returned by `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream#getFlags()`.

### skipData

```
public int skipData(long positionUs)
```

Attempts to skip to the keyframe before the specified position, or to the end of the stream if `positionUs` is beyond it.

| Parameters |
|---|---|
| `long positionUs` | The specified time. |

| Returns |
|---|---|
| `int` | The number of samples that were skipped. |

### writeData

```
public void writeData(long startPositionUs)
```

Writes all not yet written `https://developer.android.com/reference/androidx/media3/test/utils/FakeSampleStream.FakeSampleStreamItem` to the sample queue starting at the given position.

| Parameters |
|---|---|
| `long startPositionUs` | The start position, in microseconds. |