提高视频帧率
This commit is contained in:
@@ -286,13 +286,9 @@ public class MediaTransferFlvByJavacv extends MediaTransfer implements Runnable
|
||||
totalFrames++;
|
||||
long now = System.currentTimeMillis();
|
||||
|
||||
// 前几帧输出详细日志
|
||||
if (totalFrames <= 5 || totalFrames % 100 == 0) {
|
||||
log.info("📹 抓取帧 #{}: 类型={}, 有图像={}, 有音频={}",
|
||||
totalFrames,
|
||||
frame.image != null ? "视频帧" : (frame.samples != null ? "音频帧" : "未知"),
|
||||
frame.image != null,
|
||||
frame.samples != null);
|
||||
// 只在开始时和每1000帧输出一次日志
|
||||
if (totalFrames == 1 || totalFrames % 1000 == 0) {
|
||||
log.info("📹 已抓取 {} 帧", totalFrames);
|
||||
}
|
||||
|
||||
// 检查是否需要录制视频
|
||||
@@ -313,8 +309,8 @@ public class MediaTransferFlvByJavacv extends MediaTransfer implements Runnable
|
||||
}
|
||||
}
|
||||
|
||||
// 避免CPU占用过高
|
||||
LockSupport.parkNanos(1_000_000L); // 1毫秒
|
||||
// 优化:减少延迟,提高推流帧率
|
||||
// LockSupport.parkNanos(1_000_000L); // 移除 1ms 延迟,让帧处理更快
|
||||
}
|
||||
|
||||
log.warn("⚠️ 主处理循环退出: 总帧数={}, 丢帧数={}", totalFrames, droppedFrames);
|
||||
@@ -343,9 +339,6 @@ public class MediaTransferFlvByJavacv extends MediaTransfer implements Runnable
|
||||
Map<String, ChannelHandlerContext> contexts = MediaService.clientConnections.get(mediaKey);
|
||||
|
||||
if (contexts == null || contexts.isEmpty()) {
|
||||
if (totalFrames == 1 || totalFrames % 100 == 0) {
|
||||
log.debug("⚠️ 无客户端连接,跳过推流 (帧 #{})", totalFrames);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -357,7 +350,6 @@ public class MediaTransferFlvByJavacv extends MediaTransfer implements Runnable
|
||||
// 转换为JPEG字节
|
||||
BufferedImage bufferedImage = toImage.convert(frame);
|
||||
if (bufferedImage == null) {
|
||||
log.warn("❌ 帧转换失败: bufferedImage 为 null (帧 #{})", totalFrames);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -365,11 +357,10 @@ public class MediaTransferFlvByJavacv extends MediaTransfer implements Runnable
|
||||
ImageIO.write(bufferedImage, "jpg", outputStream);
|
||||
byte[] imageBytes = outputStream.toByteArray();
|
||||
|
||||
if (totalFrames <= 3 || totalFrames % 100 == 0) {
|
||||
log.info("📤 推送帧 #{}: 大小={} bytes, 分辨率={}x{}, 客户端数={}",
|
||||
totalFrames, imageBytes.length,
|
||||
bufferedImage.getWidth(), bufferedImage.getHeight(),
|
||||
contexts.size());
|
||||
// 每1000帧输出一次统计
|
||||
if (totalFrames % 1000 == 0) {
|
||||
log.info("📤 推送统计: 已推送 {} 帧, 平均大小={} KB, 客户端数={}",
|
||||
totalFrames, imageBytes.length / 1024, contexts.size());
|
||||
}
|
||||
|
||||
// 推送给所有客户端
|
||||
@@ -380,20 +371,14 @@ public class MediaTransferFlvByJavacv extends MediaTransfer implements Runnable
|
||||
BinaryWebSocketFrame binaryWebSocketFrame = new BinaryWebSocketFrame(
|
||||
Unpooled.wrappedBuffer(imageBytes));
|
||||
ctx.writeAndFlush(binaryWebSocketFrame);
|
||||
} else if (type == ClientType.HTTP) {
|
||||
// HTTP-FLV 推流 (JPEG 方式不适用,应该推送 FLV 数据)
|
||||
log.warn("⚠️ HTTP 客户端不支持 JPEG 推流,需要 FLV 格式");
|
||||
} else {
|
||||
log.warn("⚠️ 未知客户端类型: {}", type);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("❌ 推流失败 ({}): {}", clientId, e.getMessage());
|
||||
MediaService.clients.remove(clientId);
|
||||
contexts.remove(clientId);
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
log.error("❌ 帧转换失败: {}", e.getMessage(), e);
|
||||
// 静默处理错误
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user