From d84ce7461f94c7b39eb801d58280084d02adf794 Mon Sep 17 00:00:00 2001 From: felixncheng Date: Wed, 20 Nov 2024 17:30:19 +0800 Subject: [PATCH] =?UTF-8?q?bug:=20=E4=BF=AE=E5=A4=8D=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E8=BD=AC=E5=BD=95=E8=BF=87=E7=A8=8B=E4=B8=AD=E8=BF=9B=E7=A8=8B?= =?UTF-8?q?=E6=8C=82=E4=BA=86=20#2775?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/tencent/bkrepo/media/stream/Mux.kt | 21 ++++++++++++------- .../media/stream/RemuxRecordingListener.kt | 8 +++++-- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/stream/Mux.kt b/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/stream/Mux.kt index 68a8055d35..126f4c00a3 100644 --- a/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/stream/Mux.kt +++ b/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/stream/Mux.kt @@ -36,10 +36,10 @@ class Mux { * @param inputStream 视频输入流 * @param output 封装后的文件 * */ - constructor(inputStream: InputStream, output: File) : this() { + constructor(inputStream: InputStream, output: File, name: String) : this() { this.inputStream = inputStream this.outputFile = output - this.fileName = inputStream.toString() + this.fileName = name } constructor() @@ -55,6 +55,9 @@ class Mux { private var stopFlag: AtomicBoolean = AtomicBoolean(false) private var writeHeader = false + @Volatile + var packetCount = 0 + fun start() { if (!running.compareAndSet(false, true)) { logger.info("Mux was already started") @@ -129,7 +132,6 @@ class Mux { } writeHeader = true var dts = 0L - var count = 0L while (!stopFlag.get()) { if (avformat.av_read_frame(ifmtCtx, pkt) < 0) { break @@ -151,9 +153,9 @@ class Mux { pkt!!.pos(-1) logPacket(ofmtCtx, pkt!!, "out") check(avformat.av_interleaved_write_frame(ofmtCtx, pkt) >= 0) { "write frame error" } - count++ + packetCount++ } - logger.info("Complete remux $fileName,size ${outputFile!!.length()} B,$count packet.") + logger.info("Complete remux $fileName,size ${outputFile!!.length()} B,$packetCount packet.") } catch (e: Exception) { logger.error("Remux error:", e) throw e @@ -161,6 +163,7 @@ class Mux { release() scope.close() running.set(false) + logger.info("Finish remux $fileName to ${outputFile!!.absolutePath}") } } @@ -200,9 +203,11 @@ class Mux { if (writeHeader) { avformat.av_write_trailer(ofmtCtx) } - logger.info("Finish remux $fileName to ${outputFile!!.absolutePath}") - if (ofmtCtx!!.oformat().flags() and avformat.AVFMT_NOFILE == 0) { - avformat.avio_closep(ofmtCtx!!.pb()) + val oformat = ofmtCtx!!.oformat() + if (oformat != null) { + if (oformat.flags() and avformat.AVFMT_NOFILE == 0 && !ofmtCtx!!.pb().isNull) { + avformat.avio_closep(ofmtCtx!!.pb()) + } } avformat.avformat_free_context(ofmtCtx) ofmtCtx = null diff --git a/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/stream/RemuxRecordingListener.kt b/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/stream/RemuxRecordingListener.kt index 26e9e72b9f..e68e9b4a71 100644 --- a/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/stream/RemuxRecordingListener.kt +++ b/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/stream/RemuxRecordingListener.kt @@ -68,7 +68,7 @@ class RemuxRecordingListener( fileName = "$name.$fileType" val tempFileName = StringPool.randomStringByLongValue(REMUX_PREFIX, ".$fileType") tempFilePath = Paths.get(path, tempFileName) - mux = Mux(pipeIn, tempFilePath!!.toFile()) + mux = Mux(pipeIn, tempFilePath!!.toFile(), name) val remuxFuture = threadPool.submit { try { mux!!.start() @@ -90,7 +90,11 @@ class RemuxRecordingListener( pipeOut.close() mux!!.stop() pipeIn.close() - fileConsumer.accept(tempFilePath!!.toFile(), fileName!!) + if (mux!!.packetCount > 0) { + fileConsumer.accept(tempFilePath!!.toFile(), fileName!!) + } else { + logger.warn("empty stream $fileName") + } } } finally { Files.deleteIfExists(tempFilePath)