Skip to content

Commit

Permalink
Fixes a potential issue for videos that don't have an actual audio / …
Browse files Browse the repository at this point in the history
…image stream.

Signed-off-by: Ralph Gasser <[email protected]>
  • Loading branch information
ppanopticon committed Jun 12, 2024
1 parent 7ff2253 commit 8f5d577
Showing 1 changed file with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,23 +132,27 @@ class VideoDecoder : DecoderFactory {
val audioBuffer = LinkedList<Pair<ShortBuffer, Long>>()

/* Flags indicating that video / audio is ready to be emitted. */
var videoReady = !this@Instance.video
var audioReady = !this@Instance.audio
var videoReady = !(grabber.hasVideo() && this@Instance.video)
var audioReady = !(grabber.hasAudio() && this@Instance.audio)

do {
val frame = grabber.grabFrame(this@Instance.audio, this@Instance.video, true, this@Instance.keyFrames, true) ?: break
when (frame.type) {
Frame.Type.VIDEO -> {
imageBuffer.add(Java2DFrameConverter().use { it.convert(frame) to frame.timestamp })
if (frame.timestamp > windowEnd) videoReady = true
if (frame.timestamp > windowEnd) {
videoReady = true
}
}

Frame.Type.AUDIO -> {
val samples = frame.samples.firstOrNull() as? ShortBuffer
if (samples != null) {
audioBuffer.add(ShortBuffer.allocate(samples.limit()).put(samples) to frame.timestamp)
}
if (frame.timestamp > windowEnd) audioReady = true
if (frame.timestamp > windowEnd) {
audioReady = true
}
}

else -> { /* No op. */
Expand All @@ -160,8 +164,8 @@ class VideoDecoder : DecoderFactory {
emit(imageBuffer, audioBuffer, grabber, windowEnd, sourceRetrievable, channel)

/* Reset counters and flags. */
videoReady = !this@Instance.video
audioReady = !this@Instance.audio
videoReady = !(grabber.hasVideo() && this@Instance.video)
audioReady = !(grabber.hasAudio() && this@Instance.audio)

/* Update window end. */
windowEnd += TimeUnit.MILLISECONDS.toMicros(this@Instance.timeWindowMs)
Expand Down

0 comments on commit 8f5d577

Please sign in to comment.