Keep shared microphone PCM continuous across device changes
This commit is contained in:
1 parent
ad0505bd31
commit
b72e51accb
3 files changed
+145
-5
No files matched your search
+71
@@ -0,0 +1,71 @@
|
||||
package com.utun.chat.data
|
||||
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
|
||||
class MediaCaptureTimelineTest {
|
||||
private val origin = 1_000_000_000L
|
||||
private fun frame(time: Long, value: Int, epoch: Long = 1) =
|
||||
MicrophoneCapture.Frame(ShortArray(1920) { value.toShort() }, time, 0, generation = epoch)
|
||||
private fun pull(timeline: MediaCaptureTimeline) = timeline.pull(timeline.deadlineNs)
|
||||
|
||||
@Test fun switchingKeepsTimeAndSequenceAndRejectsOldDevice() {
|
||||
val timeline = MediaCaptureTimeline(origin)
|
||||
timeline.reset(1)
|
||||
assertTrue(timeline.offer(frame(origin, 11)))
|
||||
val first = pull(timeline)
|
||||
timeline.reset(2)
|
||||
assertFalse(timeline.offer(frame(origin + 20_000_000, 99)))
|
||||
val silence = pull(timeline)
|
||||
assertTrue(silence.pcm.all { it == 0.toShort() })
|
||||
assertTrue(timeline.offer(frame(origin + 40_000_000, 22, 2)))
|
||||
val next = pull(timeline)
|
||||
assertEquals(first.timeNs + 40_000_000, next.timeNs)
|
||||
assertEquals(1920L, next.sequence)
|
||||
assertTrue(next.pcm.all { it == 22.toShort() })
|
||||
}
|
||||
|
||||
@Test fun differentlyAlignedMicrophoneFramesFillTheirActualTime() {
|
||||
val timeline = MediaCaptureTimeline(origin)
|
||||
timeline.reset(1)
|
||||
assertTrue(timeline.offer(frame(origin + 10_000_000, 1)))
|
||||
assertTrue(timeline.offer(frame(origin + 30_000_000, 2)))
|
||||
val first = pull(timeline).pcm
|
||||
val second = pull(timeline).pcm
|
||||
assertTrue(first.take(960).all { it == 0.toShort() })
|
||||
assertTrue(first.drop(960).all { it == 1.toShort() })
|
||||
assertTrue(second.take(960).all { it == 1.toShort() })
|
||||
assertTrue(second.drop(960).all { it == 2.toShort() })
|
||||
}
|
||||
|
||||
@Test fun lateSoundIsNeverRetimestampedOverPreviouslySentSilence() {
|
||||
val timeline = MediaCaptureTimeline(origin)
|
||||
timeline.reset(1)
|
||||
repeat(20) { pull(timeline) }
|
||||
assertFalse(timeline.offer(frame(origin, 123)))
|
||||
assertEquals(1L, timeline.lateFrames)
|
||||
val result = pull(timeline)
|
||||
assertEquals(origin + 400_000_000, result.timeNs)
|
||||
assertTrue(result.pcm.all { it == 0.toShort() })
|
||||
}
|
||||
|
||||
@Test fun deviceOpeningDelayProducesContinuousTwentyMillisecondPackets() {
|
||||
val timeline = MediaCaptureTimeline(origin)
|
||||
timeline.reset(1)
|
||||
repeat(1500) { index ->
|
||||
if (index % 50 == 0) timeline.reset(index.toLong() + 1)
|
||||
val result = pull(timeline)
|
||||
assertEquals(origin + index * 20_000_000L, result.timeNs)
|
||||
assertEquals(index * 960L, result.sequence)
|
||||
assertEquals(1920, result.pcm.size)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun stalePacketsCannotGrowTheCaptureQueueWithoutBound() {
|
||||
val timeline = MediaCaptureTimeline(origin)
|
||||
timeline.reset(1)
|
||||
repeat(24) { assertTrue(timeline.offer(frame(origin + it * 20_000_000L, 1))) }
|
||||
assertFalse(timeline.offer(frame(origin + 480_000_000L, 2)))
|
||||
assertEquals(1L, timeline.lateFrames)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user