-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): improve algorithm performance that sort audio and video fr…
…ames by timestamp for FLV/RTMP
- Loading branch information
1 parent
6d446ca
commit 55c7dd1
Showing
8 changed files
with
229 additions
and
37 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
core/src/main/java/io/github/thibaultbee/streampack/internal/muxers/AbstractSortedMuxer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright (C) 2023 Thibault B. | ||
* | ||
* 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. | ||
*/ | ||
package io.github.thibaultbee.streampack.internal.muxers | ||
|
||
import io.github.thibaultbee.streampack.internal.data.Packet | ||
import io.github.thibaultbee.streampack.internal.utils.SyncQueue | ||
import io.github.thibaultbee.streampack.logger.Logger | ||
|
||
/** | ||
* An abstract class that implements [IMuxer] and output frames in their natural order. | ||
* | ||
* Frames are not in order because audio and video frames are encoded in parallel and video encoding takes more time. | ||
* So some new audio frame could arrive sooner than older video frame. | ||
* | ||
* Some protocols (like RTMP) require frames to be in order. Use this class for this kind of protocols. | ||
* If the protocol doesn't need ordered frames, use [IMuxer] directly. | ||
* | ||
* Don't call [IMuxerListener.onOutputFrame] directly, use [queue] instead. | ||
* Don't forget to call [stopStream] at the end of [IMuxer.stopStream] implementation. | ||
*/ | ||
abstract class AbstractSortedMuxer : IMuxer { | ||
private val syncQueue = | ||
SyncQueue( | ||
{ packet1, packet2 -> packet1.ts.compareTo(packet2.ts) }, | ||
object : SyncQueue.Listener<Packet> { | ||
override fun onElement(element: Packet) { | ||
listener?.onOutputFrame(element) | ||
} | ||
}) | ||
|
||
fun queue(packet: Packet) { | ||
syncQueue.add(packet, packet.isVideo) | ||
} | ||
|
||
/** | ||
* To be called at the end of [stopStream] implementation. | ||
*/ | ||
override fun stopStream() { | ||
syncQueue.clear() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
core/src/main/java/io/github/thibaultbee/streampack/internal/utils/SyncQueue.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright (C) 2023 Thibault B. | ||
* | ||
* 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. | ||
*/ | ||
package io.github.thibaultbee.streampack.internal.utils | ||
|
||
import java.util.PriorityQueue | ||
import java.util.concurrent.Executors | ||
|
||
/** | ||
* A synchronized queue that allows to add elements in order. Elements are stored till a sync | ||
* element is added. When a sync element is added, all elements that are comparatively lower are | ||
* sent to the listener. | ||
* | ||
* The purpose of this class is to allow to put elements in order. | ||
* | ||
* @param E the type of elements held in this collection | ||
* @param comparator the comparator that will be used to order the elements | ||
* @param listener the listener that will be called when a sync element is added | ||
*/ | ||
class SyncQueue<E>( | ||
private val comparator: Comparator<in E>, | ||
private val listener: Listener<E> | ||
) { | ||
private val priorityQueue: PriorityQueue<E> = PriorityQueue(comparator) | ||
|
||
private val executor = Executors.newSingleThreadExecutor() | ||
|
||
val size: Int | ||
get() = priorityQueue.size | ||
|
||
fun add(element: E, isSync: Boolean = false) { | ||
if (isSync) { | ||
var polledElement: E? = pollIf(comparator, element) | ||
while (polledElement != null) { | ||
listener.onElement(polledElement) | ||
polledElement = pollIf(comparator, element) | ||
} | ||
// Send sync element | ||
listener.onElement(element) | ||
} else { | ||
synchronized(this) { | ||
priorityQueue.add(element) | ||
} | ||
} | ||
} | ||
|
||
private fun pollIf(comparator: Comparator<in E>, comparableElement: E): E? { | ||
return synchronized(this) { | ||
val element = priorityQueue.peek() | ||
if ((element != null) && comparator.compare(element, comparableElement) <= 0) { | ||
priorityQueue.poll() | ||
} | ||
null | ||
} | ||
} | ||
|
||
fun clear() { | ||
synchronized(this) { | ||
priorityQueue.clear() | ||
} | ||
} | ||
|
||
interface Listener<E> { | ||
/** | ||
* Called when element is polled. | ||
*/ | ||
fun onElement(element: E) | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
core/src/test/java/io/github/thibaultbee/streampack/internal/utils/SyncQueueTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright (C) 2023 Thibault B. | ||
* | ||
* 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. | ||
*/ | ||
package io.github.thibaultbee.streampack.internal.utils | ||
|
||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
|
||
class SyncQueueTest { | ||
@Test | ||
fun `test sync 1 element`() { | ||
val syncQueue = | ||
SyncQueue({ o1, o2 -> o1.compareTo(o2) }, object : SyncQueue.Listener<Int> { | ||
override fun onElement(element: Int) { | ||
assertEquals(1, element) | ||
} | ||
}) | ||
syncQueue.add(1, true) | ||
assertEquals(0, syncQueue.size) | ||
} | ||
|
||
@Test | ||
fun `test already sorted elements`() { | ||
var i = 1 | ||
val syncQueue = | ||
SyncQueue({ o1, o2 -> o1.compareTo(o2) }, object : SyncQueue.Listener<Int> { | ||
override fun onElement(element: Int) { | ||
assertEquals(i++, element) | ||
} | ||
}) | ||
syncQueue.add(1) | ||
syncQueue.add(2) | ||
syncQueue.add(3) | ||
syncQueue.add(4, isSync = true) | ||
|
||
assertEquals(5, i) | ||
assertEquals(0, syncQueue.size) | ||
} | ||
|
||
@Test | ||
fun `test equals elements`() { | ||
val syncQueue = | ||
SyncQueue({ o1, o2 -> o1.compareTo(o2) }, object : SyncQueue.Listener<Int> { | ||
override fun onElement(element: Int) { | ||
assertEquals(1, element) | ||
} | ||
}) | ||
syncQueue.add(1) | ||
syncQueue.add(1, isSync = true) | ||
|
||
assertEquals(0, syncQueue.size) | ||
} | ||
|
||
@Test | ||
fun `test not sorted elements`() { | ||
var i = 1 | ||
val syncQueue = | ||
SyncQueue({ o1, o2 -> o1.compareTo(o2) }, object : SyncQueue.Listener<Int> { | ||
override fun onElement(element: Int) { | ||
assertEquals(i++, element) | ||
} | ||
}) | ||
syncQueue.add(1) | ||
syncQueue.add(2) | ||
syncQueue.add(3) | ||
syncQueue.add(5) | ||
syncQueue.add(6) | ||
syncQueue.add(4, isSync = true) | ||
|
||
assertEquals(5, i) | ||
assertEquals(2, syncQueue.size) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters