@@ -126,18 +126,12 @@ public void Dispose()
126
126
/// 解码下一关键帧(非线程安全)
127
127
/// </summary>
128
128
/// <returns></returns>
129
- public AvFrameWrapper DecodeNextFrameUnsafe ( )
129
+ public void DecodeNextFrameUnsafe ( )
130
130
{
131
- var frame = _frame ;
132
-
133
131
var timeoutTokenSource = new CancellationTokenSource ( _streamOption . Value . CodecTimeout ) ;
134
-
135
- IDisposable ? scope = null ;
136
-
137
132
do
138
133
{
139
- // clear scope
140
- scope ? . Dispose ( ) ;
134
+ IDisposable ? scope = null ;
141
135
142
136
try
143
137
{
@@ -160,47 +154,49 @@ public AvFrameWrapper DecodeNextFrameUnsafe()
160
154
161
155
readResult . ThrowExceptionIfError ( ) ;
162
156
157
+ scope = _logger . BeginScope ( _packet . ToString ( ) ) ;
158
+
163
159
// drop stream mis-matched packet
164
160
if ( _packet . StreamIndex != _streamOption . Value . StreamIndex )
165
161
{
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 ." ,
167
163
_packet . StreamIndex , _streamOption . Value . StreamIndex ) ;
168
- _packet . Reset ( ) ;
164
+
169
165
continue ;
170
166
}
171
167
172
168
unsafe
173
169
{
174
170
_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}." ,
176
172
_packet . StreamIndex ,
177
173
string . Format ( _formatter , "{0}" , _packet . Size ) ,
178
174
_packet . GetPresentationTimeSpan ( _decoder . Context . TimeBase ) ,
179
175
_packet . GetDecodingTimeSpan ( _decoder . Context . TimeBase )
180
176
) ;
181
177
}
182
178
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
-
192
179
// drop packet without key frame
193
180
unsafe
194
181
{
195
182
if ( ( _packet . UnmanagedPointer ->flags & ffmpeg . AV_PKT_FLAG_KEY ) == 0x00 )
196
183
{
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." ,
198
185
_packet . UnmanagedPointer ->flags ) ;
199
186
200
187
continue ;
201
188
}
202
189
}
203
190
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
+
204
200
// drop packet with invalid pts
205
201
if ( _packet . PresentationTimeStamp < 0 )
206
202
{
@@ -211,6 +207,7 @@ public AvFrameWrapper DecodeNextFrameUnsafe()
211
207
}
212
208
213
209
// decode
210
+ var frame = _frame ;
214
211
_decoder . Decode ( _packet , ref frame ) ;
215
212
216
213
scope ? . Dispose ( ) ;
@@ -254,8 +251,6 @@ public AvFrameWrapper DecodeNextFrameUnsafe()
254
251
// ffmpeg.av_hwframe_transfer_data(frame, frame, 0).ThrowExceptionIfError();
255
252
}
256
253
}
257
-
258
- return frame ;
259
254
}
260
255
261
256
/// <summary>
@@ -331,7 +326,9 @@ private void FlushDecoderBufferUnsafe(CancellationToken cancellationToken)
331
326
332
327
OpenInput ( ) ;
333
328
334
- var decodedFrame = DecodeNextFrameUnsafe ( ) ;
329
+ DecodeNextFrameUnsafe ( ) ;
330
+ var decodedFrame = _frame ;
331
+
335
332
using ( _logger . BeginScope ( decodedFrame . ToString ( ) ) )
336
333
{
337
334
var queue = _encoder . Encode ( decodedFrame ) ;
@@ -361,6 +358,7 @@ private void FlushDecoderBufferUnsafe(CancellationToken cancellationToken)
361
358
finally
362
359
{
363
360
CloseInput ( ) ;
361
+ _frame . Reset ( ) ;
364
362
_semaphore . Release ( ) ;
365
363
}
366
364
} , cancellationToken ) ;
0 commit comments