You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 45b41bc
Browse filesBrowse the repository at this point in the historyBrowse files
fix: make media player seeking, position and queue memory reliable (#537)
* fix: count media positions from the start of the source
A seek went to the container's clock as it was, while the decoders gave
out its times unchanged, so a source that does not start at zero sought
to the wrong place and could not be looped or played again. Positions now
count from the first frame played, video or audio, and a seek before the
video starts keeps the audio that comes first.
* fix: let a seek through a queue the old position keeps full
The seek emptied the queues to free the decode thread, but a decoder with
more on its way than a queue takes filled them again, and with playback
paused nothing made room, so the thread never got to the seek. Each seek
now starts a generation of the pacer, which turns away what is pushed for
the one before.
* fix: limit the memory a player's video queue takes
Sixty pictures take over 700 MiB in 4K. The queue is now held to 256 MiB
as well, which leaves 1080p its sixty pictures, and a player reports what
its queue takes and has taken, so that this can be measured across players
running side by side.
* fix: keep the position from running backwards
Video set the position outright, so a frame delivered late took it back
behind audio already played. Delivery now only moves it forward. Items
also carry the loop offset of their pass, as the player's own is ahead of
playback by the whole queue: looping held the position at zero for most
of a short source, and now restarts it as the next pass reaches delivery.
* fix: keep video ahead of audio from going out all at once
The clock mapping came from whatever reached the pacer first, which on a
decoder with many threads was audio starting after the video: the first
pictures were overdue by then, and WebRTC dropped all but the last. The
mapping now starts at the position when audio gets there first.
* fix: report how long a Matroska file that starts late runs
Matroska gives where a file ends on its clock, which libavformat passes
on as the duration. With positions counting from the first frame, that
was too long for a slider and left a gap of the start time at each seam
of a loop.
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.
147
147
148
+
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.
149
+
148
150
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.
149
151
150
152
`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.
@@ -183,6 +185,17 @@ A listener must still return promptly: the calls behind it wait, and so does a `
183
185
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.
184
186
:::
185
187
188
+
## Memory
189
+
190
+
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.
191
+
192
+
To see what an application really needs, ask the players that run:
193
+
194
+
```java
195
+
source.getQueuedVideoBytes(); // what the queued pictures take right now
196
+
source.getPeakQueuedVideoBytes(); // the most they have taken so far
197
+
```
198
+
186
199
## Feeding Your Own Media Sources
187
200
188
201
`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:
0 commit comments