GPUImageMovieWriter原生BUG(1)时间戳问题

96
Gongjia
2016.11.26 16:10* 字数 80

GPUImageMovieWriter.m, 默认的 代码有个小bug,
就是取时间戳的时候,先会取音频的时间戳,因为音频比视频编码要快,后面就一直取视频时间戳,

这样就造成了第二帧可能比第一帧时间戳小
原声的代码

- (CMTime)duration {
    if( ! CMTIME_IS_VALID(startTime) )
      return kCMTimeZero;
    if( ! CMTIME_IS_NEGATIVE_INFINITY(previousFrameTime) )
      return CMTimeSubtract(previousFrameTime, startTime);

    if( ! CMTIME_IS_NEGATIVE_INFINITY(previousAudioTime) )
      return CMTimeSubtract(previousAudioTime, startTime);

      return kCMTimeZero;
}
- (CMTime)duration {
    if( ! CMTIME_IS_VALID(startTime) ){
        return kCMTimeZero;
    }
    
    CMTime  audioDuration = kCMTimeZero;
    CMTime  videoDuration = kCMTimeZero;
    
    if( ! CMTIME_IS_NEGATIVE_INFINITY(previousFrameTime) ){
        videoDuration = CMTimeSubtract(previousFrameTime, startTime);
        //校验视频时间合法性
        if (CMTIME_COMPARE_INLINE(videoDuration, <, kCMTimeZero)
            || (CMTIME_IS_INVALID(videoDuration))){
                videoDuration = kCMTimeZero;
            }
    }
    if( ! CMTIME_IS_NEGATIVE_INFINITY(previousAudioTime) ){
        audioDuration = CMTimeSubtract(previousAudioTime, startTime);
        //校验音频时间合法性
        if (CMTIME_COMPARE_INLINE(audioDuration, <, kCMTimeZero)
            || (CMTIME_IS_INVALID(audioDuration))){
            audioDuration = kCMTimeZero;
        }
    }
    
    //返回最大的那个
    return CMTimeMaximum(videoDuration, audioDuration);
}
GPUImage/Opengl学习
Gupao