@@ -275,36 +275,35 @@ extension NextLevelSessionExporter {
275275 self . _reader? . startReading ( )
276276 self . _writer? . startSession ( atSourceTime: self . timeRange. start)
277277
278- let audioSemaphore = DispatchSemaphore ( value: 0 )
279- let videoSemaphore = DispatchSemaphore ( value: 0 )
278+ let dispatchGroup = DispatchGroup ( )
280279
281280 let videoTracks = asset. tracks ( withMediaType: AVMediaType . video)
282281 if let videoInput = self . _videoInput,
283282 let videoOutput = self . _videoOutput,
284283 videoTracks. count > 0 {
284+ dispatchGroup. enter ( )
285285 videoInput. requestMediaDataWhenReady ( on: self . _inputQueue, using: {
286286 if self . encode ( readySamplesFromReaderOutput: videoOutput, toWriterInput: videoInput) == false {
287- videoSemaphore . signal ( )
287+ dispatchGroup . leave ( )
288288 }
289289 } )
290290 } else {
291- videoSemaphore . signal ( )
291+ dispatchGroup . leave ( )
292292 }
293293
294294 if let audioInput = self . _audioInput,
295295 let audioOutput = self . _audioOutput {
296+ dispatchGroup. enter ( )
296297 audioInput. requestMediaDataWhenReady ( on: self . _inputQueue, using: {
297298 if self . encode ( readySamplesFromReaderOutput: audioOutput, toWriterInput: audioInput) == false {
298- audioSemaphore . signal ( )
299+ dispatchGroup . leave ( )
299300 }
300301 } )
301302 } else {
302- audioSemaphore . signal ( )
303+ dispatchGroup . leave ( )
303304 }
304305
305- DispatchQueue . global ( ) . async {
306- audioSemaphore. wait ( )
307- videoSemaphore. wait ( )
306+ dispatchGroup. notify ( queue: . global( ) ) {
308307 DispatchQueue . main. async {
309308 self . finish ( )
310309 }
0 commit comments