@@ -607,18 +607,34 @@ void seekBeforeVideoStartsKeepsTheAudio() throws Exception {
607607 void positionCountsFromTheFirstFrame () throws Exception {
608608 try (Playback playback = new Playback (new MediaReader (
609609 TestMedia .shiftedMatroska (tempDir , asset (), 6000 , 5000 )))) {
610- playback .player .play ();
610+ AtomicLong firstDeliveredFramePositionUs = new AtomicLong (-1 );
611+ AudioTrackSink afterVideo = (data , bits , rate , ch , count ) -> {
612+ // Both sinks run on the pacer thread. An audio callback after
613+ // a video callback sees the completed video push, whereas the
614+ // video callback itself still sees the last accepted audio.
615+ if (playback .frames .get () > 0 ) {
616+ firstDeliveredFramePositionUs .compareAndSet (-1 , playback .player .getPositionUs ());
617+ }
618+ };
619+ playback .audioTrack .addSink (afterVideo );
611620
612- assertTrue (playback .ended .await (15 , TimeUnit .SECONDS ), "no end of stream" );
621+ try {
622+ playback .player .play ();
613623
614- // Video starts a second after the source does.
615- long first = playback .firstFramePositionUs .get ();
624+ assertTrue (playback .ended .await (15 , TimeUnit .SECONDS ), "no end of stream" );
616625
617- assertTrue (Math .abs (first - 1_000_000 ) < 20_000 , "first frame at " + first + " us" );
626+ // Video starts a second after the source does. Read its
627+ // position only once delivery has returned successfully.
628+ assertEquals (1_000_000 , firstDeliveredFramePositionUs .get (),
629+ "position after the first video delivery" );
618630
619- long end = playback .player .getPositionUs ();
631+ long end = playback .player .getPositionUs ();
620632
621- assertTrue (end > 3_900_000 && end < 4_100_000 , "ended at " + end + " us" );
633+ assertTrue (end > 3_900_000 && end < 4_100_000 , "ended at " + end + " us" );
634+ }
635+ finally {
636+ playback .audioTrack .removeSink (afterVideo );
637+ }
622638 }
623639 }
624640
@@ -1564,8 +1580,8 @@ private final class Playback implements AutoCloseable {
15641580 final List <Integer > silentChunks = Collections .synchronizedList (new ArrayList <>());
15651581
15661582 private final VideoTrackSink videoSink = frame -> {
1567- // The pacer delivers on its own thread, having just moved the
1568- // position to the frame's presentation time .
1583+ // This callback runs inside the push. The position still belongs
1584+ // to the last successful delivery until this push returns .
15691585 long positionUs = Playback .this .player .getPositionUs ();
15701586
15711587 if (firstFramePositionUs .get () < 0 ) {
0 commit comments