Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/guide/media/media-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ source.getState(); // IDLE, PLAYING, PAUSED, ENDED or CLOSED

Looping keeps the timing across the seam, so a looping file works as a stand-in for a camera that never stops. A looping source never reports an end of stream.

Positions count from the start of the source, which is its first video or audio frame, whichever comes first. The times a container gives its frames do not have to begin at zero: a file cut out of a longer recording or an MPEG-TS capture may start at any time, and `seek(2_000_000)` still means two seconds into it. Video and audio share that start, so a stream that begins after the other one keeps that distance.

A seek starts playback at the position asked for. The decoder has to go back to the keyframe at or before it to produce a picture at all, so the frames from there up to the position are decoded and dropped, and the audio is cut at it. How long a seek takes therefore depends on how often the media was encoded with keyframes, not on how long the media is: MKV and MP4 have an index that takes the reader straight to that keyframe. MPEG-TS has none, and a search for the position by its timestamps can land past the keyframe before it. The player then searches again further back, a second before the position, then two, four and so on, so that the seek still starts at the position.

`fastSeek` moves to that keyframe and plays from there, without decoding up to the position, so it takes no longer however far the keyframes are apart. That suits a slider being dragged, as browsers' `fastSeek()` does: fast seeks while it moves, and a `seek` once it is let go. In a container without an index, such as MPEG-TS, the keyframe can be the one after the position.
Expand Down Expand Up @@ -183,6 +185,17 @@ A listener must still return promptly: the calls behind it wait, and so does a `
Closing the player, or the `MediaFileSource`, from a listener is fine: it takes effect at once, and the native player is released on another thread once the listener has returned. The same holds for a sink of a track made from the player's sources, which the player calls on threads of its own as well.
:::

## Memory

Decoding runs ahead of playback by up to sixty pictures, and at most by what `MediaPlayer.getMaxQueuedVideoBytes()` reports, which is 256 MiB. That keeps sixty pictures of 1080p, about twenty of 4K, and whatever the resolution, a player does not take more than that for pictures waiting to be delivered. Several players take that much each, on top of what the decoders use themselves.

To see what an application really needs, ask the players that run:

```java
source.getQueuedVideoBytes(); // what the queued pictures take right now
source.getPeakQueuedVideoBytes(); // the most they have taken so far
```

## Feeding Your Own Media Sources

`MediaFileSource` creates the media sources for you. An application that needs its own — to feed a source it already created, or to send only the video of a file — uses `MediaPlayer` directly:
Expand Down
24 changes: 24 additions & 0 deletions webrtc-media/src/main/cpp/include/JNI_MediaPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,30 @@ extern "C" {
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_player_MediaPlayer_dispose
(JNIEnv *, jclass, jlong);

/*
* Class: dev_onvoid_webrtc_media_player_MediaPlayer
* Method: queuedVideoBytes
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_media_player_MediaPlayer_queuedVideoBytes
(JNIEnv *, jclass, jlong);

/*
* Class: dev_onvoid_webrtc_media_player_MediaPlayer
* Method: peakQueuedVideoBytes
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_media_player_MediaPlayer_peakQueuedVideoBytes
(JNIEnv *, jclass, jlong);

/*
* Class: dev_onvoid_webrtc_media_player_MediaPlayer
* Method: maxQueuedVideoBytes
* Signature: ()J
*/
JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_media_player_MediaPlayer_maxQueuedVideoBytes
(JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
Expand Down
44 changes: 44 additions & 0 deletions webrtc-media/src/main/cpp/include/JNI_PlaybackProbe.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2026 Alex Andres
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <jni.h>
/* Header for class dev_onvoid_webrtc_media_player_PlaybackProbe */

#ifndef _Included_dev_onvoid_webrtc_media_player_PlaybackProbe
#define _Included_dev_onvoid_webrtc_media_player_PlaybackProbe
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: dev_onvoid_webrtc_media_player_PlaybackProbe
* Method: seeks
* Signature: ()J
*/
JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_media_player_PlaybackProbe_seeks
(JNIEnv *, jclass);

/*
* Class: dev_onvoid_webrtc_media_player_PlaybackProbe
* Method: waitingPushes
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_dev_onvoid_webrtc_media_player_PlaybackProbe_waitingPushes
(JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif
7 changes: 5 additions & 2 deletions webrtc-media/src/main/cpp/include/media/AudioDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ namespace ffmpeg

// Opens a decoder for the given stream, which must outlive this
// decoder. Source audio of more than two channels is downmixed to
// stereo. Returns 0 or a negative AVERROR.
int Open(const AVStream * stream);
// stereo. Timestamps are given out relative to the start time,
// which is where the source starts on the container's clock.
// Returns 0 or a negative AVERROR.
int Open(const AVStream * stream, int64_t start_time_us = 0);

void Close();

Expand Down Expand Up @@ -136,6 +138,7 @@ namespace ffmpeg
int in_format_ = -1;
int in_rate_ = 0;
AVRational time_base_ = { 0, 1 };
int64_t start_time_us_ = 0;
int channels_ = 0;

// Resampled samples not yet handed out, interleaved.
Expand Down
119 changes: 105 additions & 14 deletions webrtc-media/src/main/cpp/include/media/MediaPacer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ namespace ffmpeg
// is a time on the WebRTC clock. Every item carries that time with it, so
// the timeline that reaches the receiver is the source's own and not the
// pacing thread's, and audio stays lined up with video however much the
// thread is jostled.
// thread is jostled. The mapping starts at the position playback starts
// from, if audio gets here first, as video that belongs before it may
// still be in the decoder.
//
// Video and audio are paced independently. They go to different sources
// and need no ordering between them, and keeping them independent is also
Expand All @@ -58,6 +60,13 @@ namespace ffmpeg
static constexpr size_t kVideoCapacity = 60;
static constexpr size_t kAudioCapacity = 100;

// What the pictures in the video queue may take, on top of their
// number. Sixty pictures in 4K would take over 700 MiB, for each
// player. This leaves 1080p its sixty pictures, of about 3 MiB
// each, and 4K about twenty, which is still most of a second.
// A single picture larger than this goes through on its own.
static constexpr size_t kVideoByteCapacity = 256 * 1024 * 1024;

// How far audio may run past its capacity while video is short.
//
// One thread decodes both, in the order the container stores
Expand All @@ -68,10 +77,19 @@ namespace ffmpeg
// then leaves the video queue running dry, and its frames arrive
// late and all at once, which WebRTC's encoder answers by dropping
// all but the last of each burst. Letting audio grow while video
// is below half its capacity is what keeps video fed; this bounds
// how much, at ten seconds, for a source that has no video at all.
// is below half its capacity, in pictures and in bytes, is what
// keeps video fed; this bounds how much, at ten seconds, for a
// source that has no video at all.
static constexpr size_t kAudioOverflowCapacity = 1000;

// How far past the position playback starts from the first audio
// may lie for the mapping to start at that position regardless.
// It covers what a decoder holds back, which is up to one picture
// for each of its threads. Further than that the video would be
// here by now, and a source with timestamps that do not count
// from its start must not be held up for as long as they say.
static constexpr int64_t kMaxStartGapUs = 5000000;

// The sources may be 0, in which case media of that kind is
// dropped rather than delivered.
MediaPacer(const webrtc_java_api * api, void * video_source,
Expand All @@ -87,17 +105,24 @@ namespace ffmpeg
// call more than once.
void Stop();

// Hands over a decoded frame and its presentation time. Ownership
// of the frame passes to the pacer whether this succeeds or not.
// Hands over a decoded frame and its presentation time, which is
// its position in the source. A source played again is pushed
// with a loop offset, which goes on top of that to make the times
// of all its passes one timeline that only rises. Ownership of
// the frame passes to the pacer whether this succeeds or not.
//
// Blocks while the queue is full, which is the backpressure that
// keeps decoding from running ahead of playback. Returns false
// once the pacer has been stopped.
bool PushVideo(AVFrame * frame, int64_t timestamp_us);
// once the pacer has been stopped, or once the generation given
// is no longer the current one: what was decoded for it belongs
// to a position that is being left, so it is dropped, and a wait
// for room ends there, as nothing else may ever end it.
bool PushVideo(AVFrame * frame, int64_t timestamp_us, int64_t loop_offset_us,
uint64_t generation);

// Hands over one 10 ms chunk of interleaved 16-bit PCM.
bool PushAudio(std::vector<int16_t> && samples, int channels,
int64_t timestamp_us);
int64_t timestamp_us, int64_t loop_offset_us, uint64_t generation);

void Pause();
void Resume();
Expand All @@ -106,17 +131,47 @@ namespace ffmpeg
// the next item delivered starts a new one. This is what a seek
// needs: what is queued belongs to the position being left. The
// position is where playback is taken to be until an item has been
// delivered, in the timestamps the items are pushed with.
void Flush(int64_t position_us);
// delivered, in the source: the first item then sets it, wherever
// that landed, and starts a new timeline with no loop offset.
//
// Exact says that playback really starts at this position, which
// a seek to a keyframe does not: there, what is played comes as
// it lands, so the first item starts the mapping.
void Flush(int64_t position_us, bool exact);

// Flushes, and starts a new generation, which turns away what is
// still being pushed for the one before. This is for a seek that
// has been asked for, by a thread other than the one pushing,
// which may have more on its way than the queue can take. With
// playback paused nothing would ever make room for that, and the
// thread would never get round to the seek.
void StartGeneration(int64_t position_us);

// The generation pushes have to name to be taken. The thread
// pushing reads this when it starts on a seek, so that everything
// it pushes from there on was decoded for that seek.
uint64_t GetGeneration() const;

// True once everything handed over has been delivered: the
// queues are empty and no item is being handed to a source.
bool IsDrained() const;

// The presentation time of the item delivered last, which is where
// playback has got to.
// Where playback has got to in the source, in the pass that is
// being delivered: the furthest item delivered so far. It only
// moves forward, as video and audio are delivered independently
// and a frame that arrives late is behind what has been heard,
// other than when a flush moves it, or a pass of the source ends
// and the next one starts. That happens as the next pass reaches
// delivery, however far ahead the decoder has started it.
int64_t GetPositionUs() const;

// What the pictures in the video queue take right now, in bytes.
size_t GetQueuedVideoBytes() const;

// The most they have taken so far. Nothing delivered counts, nor
// the pictures the decoder holds on to.
size_t GetPeakQueuedVideoBytes() const;

// True on the pacing thread, which is the one that calls the sinks
// of the tracks fed by the sources.
bool IsOwnThread() const;
Expand All @@ -126,9 +181,12 @@ namespace ffmpeg
{
AVFrame * frame = nullptr;
int64_t timestamp_us = 0;
int64_t loop_offset_us = 0;
size_t bytes = 0;

VideoItem() = default;
VideoItem(AVFrame * f, int64_t ts) : frame(f), timestamp_us(ts) {}
VideoItem(AVFrame * f, int64_t ts, int64_t offset, size_t size)
: frame(f), timestamp_us(ts), loop_offset_us(offset), bytes(size) {}
~VideoItem();

VideoItem(VideoItem && other) noexcept;
Expand All @@ -143,10 +201,25 @@ namespace ffmpeg
std::vector<int16_t> samples;
int channels = 0;
int64_t timestamp_us = 0;
int64_t loop_offset_us = 0;
};

void Run();

void FlushLocked(int64_t position_us, bool exact);

// Takes in the time of an item about to be delivered, on the
// timeline of all passes, and the loop offset of its pass.
void AdvancePosition(int64_t timestamp_us, int64_t loop_offset_us);

// Whether a picture of the given size fits the video queue. An
// empty queue takes anything, as nothing else would ever let a
// picture larger than the queue through.
bool HasVideoRoom(size_t bytes) const;

// Whether video is below half of what its queue takes.
bool IsVideoShort() const;

// Both run without the lock held: the extension API delivers
// synchronously into WebRTC, which must not happen underneath a
// lock of ours. The time on the WebRTC clock is worked out by the
Expand All @@ -169,17 +242,35 @@ namespace ffmpeg
std::deque<VideoItem> video_queue_;
std::deque<AudioItem> audio_queue_;

// What the pictures in the video queue take, and the most they
// have taken.
size_t video_bytes_ = 0;
size_t peak_video_bytes_ = 0;

bool running_ = false;
bool paused_ = false;

// Items taken from a queue whose delivery has not returned yet.
int delivering_ = 0;

// Presentation time plus this is a time on the WebRTC clock.
// Counts the seeks asked for.
uint64_t generation_ = 0;

// Presentation time, on the timeline of all passes, plus this is
// a time on the WebRTC clock.
int64_t base_us_ = 0;
bool have_base_ = false;

// Whether the mapping to come starts at the position.
bool exact_start_ = true;
int64_t paused_at_us_ = 0;

// Where playback has got to on the timeline of all passes, the
// loop offset of the pass that is in, and whether an item has set
// them since the last flush.
int64_t position_us_ = 0;
int64_t position_offset_us_ = 0;
bool position_set_ = false;
};
}

Expand Down
Loading
Loading