Skip to content

Commit a9a93c4

Browse files
committed
chore(core): codec: reusable extra for memory optimization
1 parent f4ffeaf commit a9a93c4

File tree

1 file changed

+16
-4
lines changed
  • core/src/main/java/io/github/thibaultbee/streampack/core/elements/encoders/mediacodec

1 file changed

+16
-4
lines changed

core/src/main/java/io/github/thibaultbee/streampack/core/elements/encoders/mediacodec/MediaCodecEncoder.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import kotlinx.coroutines.sync.Mutex
4343
import kotlinx.coroutines.sync.withLock
4444
import kotlinx.coroutines.withContext
4545
import java.io.Closeable
46+
import java.nio.ByteBuffer
4647
import kotlin.math.min
4748

4849
/**
@@ -63,6 +64,12 @@ internal constructor(
6364
private val mediaCodec: MediaCodec
6465
private val format: MediaFormat
6566
private var outputFormat: MediaFormat? = null
67+
set(value) {
68+
extra = value?.extra
69+
field = value
70+
}
71+
private var extra: List<ByteBuffer>? = null
72+
6673
private val frameFactory by lazy { FrameFactory(mediaCodec, isVideo) }
6774

6875
private val isVideo = encoderConfig.isVideo
@@ -353,7 +360,7 @@ internal constructor(
353360
info.isValid -> {
354361
try {
355362
val frame = frameFactory.frame(
356-
index, outputFormat!!, info, tag
363+
index, extra, outputFormat!!, info, tag
357364
)
358365
try {
359366
listener.outputChannel.send(frame)
@@ -601,15 +608,19 @@ internal constructor(
601608
* @return the created frame
602609
*/
603610
fun frame(
604-
index: Int, outputFormat: MediaFormat, info: BufferInfo, tag: String
611+
index: Int,
612+
extra: List<ByteBuffer>?,
613+
outputFormat: MediaFormat,
614+
info: BufferInfo,
615+
tag: String
605616
): Frame {
606617
var pts = info.presentationTimeUs
607618
if (pts <= previousPresentationTimeUs) {
608619
pts = previousPresentationTimeUs + 1
609620
Logger.w(tag, "Correcting timestamp: $pts <= $previousPresentationTimeUs")
610621
}
611622
previousPresentationTimeUs = pts
612-
return createFrame(codec, index, outputFormat, pts, info.isKeyFrame, tag)
623+
return createFrame(codec, index, extra, outputFormat, pts, info.isKeyFrame, tag)
613624
}
614625

615626
/**
@@ -622,14 +633,15 @@ internal constructor(
622633
private fun createFrame(
623634
codec: MediaCodec,
624635
index: Int,
636+
extra: List<ByteBuffer>?,
625637
outputFormat: MediaFormat,
626638
ptsInUs: Long,
627639
isKeyFrame: Boolean,
628640
tag: String
629641
): Frame {
630642
val buffer = requireNotNull(codec.getOutputBuffer(index))
631643
val extra = if (isKeyFrame || !isVideo) {
632-
outputFormat.extra
644+
extra!!.map { it.duplicate() }
633645
} else {
634646
null
635647
}

0 commit comments

Comments
 (0)