Skip to content

Commit 721d587

Browse files
committed
optimize: capture log and code
1 parent 2df719c commit 721d587

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

StreamingCaptureBot.Core/Services/CaptureService.cs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,12 @@ public void Dispose()
126126
/// 解码下一关键帧(非线程安全)
127127
/// </summary>
128128
/// <returns></returns>
129-
public AvFrameWrapper DecodeNextFrameUnsafe()
129+
public void DecodeNextFrameUnsafe()
130130
{
131-
var frame = _frame;
132-
133131
var timeoutTokenSource = new CancellationTokenSource(_streamOption.Value.CodecTimeout);
134-
135-
IDisposable? scope = null;
136-
137132
do
138133
{
139-
// clear scope
140-
scope?.Dispose();
134+
IDisposable? scope = null;
141135

142136
try
143137
{
@@ -160,47 +154,49 @@ public AvFrameWrapper DecodeNextFrameUnsafe()
160154

161155
readResult.ThrowExceptionIfError();
162156

157+
scope = _logger.BeginScope(_packet.ToString());
158+
163159
// drop stream mis-matched packet
164160
if (_packet.StreamIndex != _streamOption.Value.StreamIndex)
165161
{
166-
_logger.LogInformation("Packet in stream[{id}] found, require stream[{req id}], igonre.",
162+
_logger.LogInformation("Received packet from stream[{id}], require stream[{req id}], drop.",
167163
_packet.StreamIndex, _streamOption.Value.StreamIndex);
168-
_packet.Reset();
164+
169165
continue;
170166
}
171167

172168
unsafe
173169
{
174170
_logger.LogInformation(
175-
"Packet in stream[{index}] with size:{size}, pts(display):{pts:c}, dts(decode):{dts:c}.",
171+
"Received packet from stream[{index}] with size:{size}, pts(display):{pts:c}, dts(decode):{dts:c}.",
176172
_packet.StreamIndex,
177173
string.Format(_formatter, "{0}", _packet.Size),
178174
_packet.GetPresentationTimeSpan(_decoder.Context.TimeBase),
179175
_packet.GetDecodingTimeSpan(_decoder.Context.TimeBase)
180176
);
181177
}
182178

183-
// drop packet with invalid size
184-
if (_packet.Size <= 0)
185-
{
186-
_logger.LogWarning("Packet with invalid size {size}, drop.",
187-
string.Format(_formatter, "{0}", _packet.Size));
188-
189-
continue;
190-
}
191-
192179
// drop packet without key frame
193180
unsafe
194181
{
195182
if ((_packet.UnmanagedPointer->flags & ffmpeg.AV_PKT_FLAG_KEY) == 0x00)
196183
{
197-
_logger.LogInformation("Packet flag {flag:x8} not contains KEY frame, drop.",
184+
_logger.LogInformation("Packet flag 0x{flag:x8} not contains KEY frame, drop.",
198185
_packet.UnmanagedPointer->flags);
199186

200187
continue;
201188
}
202189
}
203190

191+
// drop packet with invalid size
192+
if (_packet.Size <= 0)
193+
{
194+
_logger.LogWarning("Packet with invalid size {size}, drop.",
195+
string.Format(_formatter, "{0}", _packet.Size));
196+
197+
continue;
198+
}
199+
204200
// drop packet with invalid pts
205201
if (_packet.PresentationTimeStamp < 0)
206202
{
@@ -211,6 +207,7 @@ public AvFrameWrapper DecodeNextFrameUnsafe()
211207
}
212208

213209
// decode
210+
var frame = _frame;
214211
_decoder.Decode(_packet, ref frame);
215212

216213
scope?.Dispose();
@@ -254,8 +251,6 @@ public AvFrameWrapper DecodeNextFrameUnsafe()
254251
// ffmpeg.av_hwframe_transfer_data(frame, frame, 0).ThrowExceptionIfError();
255252
}
256253
}
257-
258-
return frame;
259254
}
260255

261256
/// <summary>
@@ -331,7 +326,9 @@ private void FlushDecoderBufferUnsafe(CancellationToken cancellationToken)
331326

332327
OpenInput();
333328

334-
var decodedFrame = DecodeNextFrameUnsafe();
329+
DecodeNextFrameUnsafe();
330+
var decodedFrame = _frame;
331+
335332
using (_logger.BeginScope(decodedFrame.ToString()))
336333
{
337334
var queue = _encoder.Encode(decodedFrame);
@@ -361,6 +358,7 @@ private void FlushDecoderBufferUnsafe(CancellationToken cancellationToken)
361358
finally
362359
{
363360
CloseInput();
361+
_frame.Reset();
364362
_semaphore.Release();
365363
}
366364
}, cancellationToken);

0 commit comments

Comments
 (0)