@@ -275,36 +275,35 @@ extension NextLevelSessionExporter {
275
275
self . _reader? . startReading ( )
276
276
self . _writer? . startSession ( atSourceTime: self . timeRange. start)
277
277
278
- let audioSemaphore = DispatchSemaphore ( value: 0 )
279
- let videoSemaphore = DispatchSemaphore ( value: 0 )
278
+ let dispatchGroup = DispatchGroup ( )
280
279
281
280
let videoTracks = asset. tracks ( withMediaType: AVMediaType . video)
282
281
if let videoInput = self . _videoInput,
283
282
let videoOutput = self . _videoOutput,
284
283
videoTracks. count > 0 {
284
+ dispatchGroup. enter ( )
285
285
videoInput. requestMediaDataWhenReady ( on: self . _inputQueue, using: {
286
286
if self . encode ( readySamplesFromReaderOutput: videoOutput, toWriterInput: videoInput) == false {
287
- videoSemaphore . signal ( )
287
+ dispatchGroup . leave ( )
288
288
}
289
289
} )
290
290
} else {
291
- videoSemaphore . signal ( )
291
+ dispatchGroup . leave ( )
292
292
}
293
293
294
294
if let audioInput = self . _audioInput,
295
295
let audioOutput = self . _audioOutput {
296
+ dispatchGroup. enter ( )
296
297
audioInput. requestMediaDataWhenReady ( on: self . _inputQueue, using: {
297
298
if self . encode ( readySamplesFromReaderOutput: audioOutput, toWriterInput: audioInput) == false {
298
- audioSemaphore . signal ( )
299
+ dispatchGroup . leave ( )
299
300
}
300
301
} )
301
302
} else {
302
- audioSemaphore . signal ( )
303
+ dispatchGroup . leave ( )
303
304
}
304
305
305
- DispatchQueue . global ( ) . async {
306
- audioSemaphore. wait ( )
307
- videoSemaphore. wait ( )
306
+ dispatchGroup. notify ( queue: . global( ) ) {
308
307
DispatchQueue . main. async {
309
308
self . finish ( )
310
309
}
0 commit comments