Skip to content

Commit 1c57c17

Browse files
committed
optmize: optimize codec logger scope
1 parent f9a850d commit 1c57c17

File tree

3 files changed

+172
-168
lines changed

3 files changed

+172
-168
lines changed

StreamingCaptureBot.Core/FfMpeg.Net/Codecs/CodecBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public unsafe Queue<byte[]> Encode(AvFrameWrapper rawFrame)
6464
#pragma warning disable CA2254
6565
logger.LogError(exception, message);
6666
#pragma warning restore CA2254
67+
scope?.Dispose();
6768
throw exception;
6869
}
6970

@@ -117,6 +118,7 @@ public unsafe Queue<byte[]> Encode(AvFrameWrapper rawFrame)
117118
logger.LogError(exception, message);
118119
#pragma warning restore CA2254
119120

121+
scope?.Dispose();
120122
throw exception;
121123
}
122124

StreamingCaptureBot.Core/FfMpeg.Net/Codecs/DecoderBase.cs

Lines changed: 88 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -48,95 +48,110 @@ public bool ConfigureAndOpen(Action<DecoderContext> config, IDictionary<string,
4848

4949
public void Decode(AvPacketWrapper packet, ref AvFrameWrapper frame)
5050
{
51-
int decodeResult;
52-
53-
// 尝试发送
54-
logger.LogDebug("Try send packet to decoder.");
55-
56-
var sendResult = ctx.TrySendPacket(packet);
57-
if (sendResult == ffmpeg.AVERROR(ffmpeg.EAGAIN))
58-
{
59-
// reference:
60-
// * tree/release/6.1/fftools/ffmpeg_dec.c:567
61-
// 理论上不会出现 EAGAIN
62-
63-
logger.LogWarning(
64-
"Receive {error} after sent, this could be cause by ffmpeg bug or some reason, ignored this message.",
65-
nameof(ffmpeg.EAGAIN));
66-
sendResult = 0;
67-
}
68-
69-
if (sendResult == 0 || sendResult == ffmpeg.AVERROR_EOF)
70-
{
71-
// 发送成功
72-
logger.LogDebug("PacketBuffer sent success, try get decoded frame.");
73-
// 获取解码结果
74-
decodeResult = ctx.TryReceivedFrame(ref frame);
75-
}
76-
else
77-
{
78-
var error = new ApplicationException(FfMpegExtension.av_strerror(sendResult));
79-
80-
// 无法处理的发送失败
81-
logger.LogError(error, "Send packet to decoder failed.\n");
82-
83-
throw error;
84-
}
85-
86-
if (decodeResult < 0)
51+
using (logger.BeginScope("{name}.{function}",
52+
Context.ToString(), nameof(Decode)))
8753
{
88-
// 错误处理
89-
ApplicationException error;
90-
var message = FfMpegExtension.av_strerror(decodeResult);
54+
int decodeResult;
55+
// 尝试发送
9156

92-
if (decodeResult == ffmpeg.AVERROR_EOF)
93-
{
94-
// reference:
95-
// * https://ffmpeg.org/doxygen/6.1/group__lavc__decoding.html#ga11e6542c4e66d3028668788a1a74217c
96-
// > the codec has been fully flushed, and there will be no more output frames
97-
// 理论上不会出现 EOF
98-
message =
99-
"the codec has been fully flushed, and there will be no more output frames.";
57+
var scope = logger.BeginScope(packet.ToString());
10058

101-
error = new(message);
59+
logger.LogDebug("Try send packet to decoder.");
10260

103-
logger.LogError(error, "Received EOF from decoder.\n");
104-
}
105-
else if (decodeResult == ffmpeg.AVERROR(ffmpeg.EAGAIN))
61+
var sendResult = ctx.TrySendPacket(packet);
62+
if (sendResult == ffmpeg.AVERROR(ffmpeg.EAGAIN))
10663
{
10764
// reference:
108-
// * tree/release/6.1/fftools/ffmpeg_dec.c:596
109-
// * https://ffmpeg.org/doxygen/6.1/group__lavc__decoding.html#ga11e6542c4e66d3028668788a1a74217c
110-
// > output is not available in this state - user must try to send new input
65+
// * tree/release/6.1/fftools/ffmpeg_dec.c:567
11166
// 理论上不会出现 EAGAIN
112-
message =
113-
"output is not available in this state - user must try to send new input";
11467

115-
//if (_streamOption.KeyFrameOnly)
116-
//{
117-
// // 抛出异常,仅关键帧模式中,该错误不可能通过发送更多需要的包来解决
118-
// error = new(message);
68+
logger.LogWarning(
69+
"Receive {error} after sent, this could be cause by ffmpeg bug or some reason, ignored this message.",
70+
nameof(ffmpeg.EAGAIN));
71+
sendResult = 0;
72+
}
11973

120-
// _logger.LogError(error, "Received EAGAIN from decoder.\n");
121-
// throw error;
122-
//}
74+
if (sendResult == 0 || sendResult == ffmpeg.AVERROR_EOF)
75+
{
76+
// 发送成功
77+
logger.LogDebug("PacketBuffer sent success, try get decoded frame.");
12378

124-
// 忽略错误,发送下一个包进行编码,可能足够的包进入解码器可以解决
125-
logger.LogWarning("Receive EAGAIN from decoder, retry.");
126-
// continue;
79+
scope?.Dispose();
80+
// 获取解码结果
81+
decodeResult = ctx.TryReceivedFrame(ref frame);
82+
scope = logger.BeginScope(frame.ToString());
12783
}
12884
else
12985
{
130-
error = new(message);
131-
logger.LogError(error, "Uncaught error occured during decoding.\n");
86+
var error = new ApplicationException(FfMpegExtension.av_strerror(sendResult));
87+
88+
// 无法处理的发送失败
89+
logger.LogError(error, "Send packet to decoder failed.\n");
90+
91+
scope?.Dispose();
13292
throw error;
13393
}
134-
}
13594

136-
// 解码正常
137-
logger.LogInformation("Decode frame success. type {type}, pts {pts}.",
138-
frame.PictureType.ToString(),
139-
frame.GetPresentationTimeSpan(ctx.TimeBase).ToString("c"));
95+
if (decodeResult < 0)
96+
{
97+
// 错误处理
98+
ApplicationException error;
99+
var message = FfMpegExtension.av_strerror(decodeResult);
100+
101+
if (decodeResult == ffmpeg.AVERROR_EOF)
102+
{
103+
// reference:
104+
// * https://ffmpeg.org/doxygen/6.1/group__lavc__decoding.html#ga11e6542c4e66d3028668788a1a74217c
105+
// > the codec has been fully flushed, and there will be no more output frames
106+
// 理论上不会出现 EOF
107+
message =
108+
"the codec has been fully flushed, and there will be no more output frames.";
109+
110+
error = new(message);
111+
112+
logger.LogError(error, "Received EOF from decoder.\n");
113+
}
114+
else if (decodeResult == ffmpeg.AVERROR(ffmpeg.EAGAIN))
115+
{
116+
// reference:
117+
// * tree/release/6.1/fftools/ffmpeg_dec.c:596
118+
// * https://ffmpeg.org/doxygen/6.1/group__lavc__decoding.html#ga11e6542c4e66d3028668788a1a74217c
119+
// > output is not available in this state - user must try to send new input
120+
// 理论上不会出现 EAGAIN
121+
message =
122+
"output is not available in this state - user must try to send new input";
123+
124+
//if (_streamOption.KeyFrameOnly)
125+
//{
126+
// // 抛出异常,仅关键帧模式中,该错误不可能通过发送更多需要的包来解决
127+
// error = new(message);
128+
129+
// _logger.LogError(error, "Received EAGAIN from decoder.\n");
130+
// throw error;
131+
//}
132+
133+
// 忽略错误,发送下一个包进行编码,可能足够的包进入解码器可以解决
134+
logger.LogWarning("Receive EAGAIN from decoder, retry.");
135+
// continue;
136+
}
137+
else
138+
{
139+
error = new(message);
140+
logger.LogError(error, "Uncaught error occured during decoding.\n");
141+
142+
scope?.Dispose();
143+
144+
throw error;
145+
}
146+
}
147+
148+
// 解码正常
149+
logger.LogInformation("Decode frame success. type {type}, pts {pts}.",
150+
frame.PictureType.ToString(),
151+
frame.GetPresentationTimeSpan(ctx.TimeBase).ToString("c"));
152+
153+
scope?.Dispose();
154+
}
140155
}
141156

142157
public void Dispose()

0 commit comments

Comments
 (0)