Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed some bugs in PKVideoDecoder #17

Merged
merged 1 commit into from
Jul 4, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 39 additions & 18 deletions PKShortVideo/PKShortVideoPlayer/PKVideoDecoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,21 @@ @interface PKVideoDecoder () {
const GLfloat *_preferredConversion;

int imageBufferWidth, imageBufferHeight;

NSLock *readerLock;
NSMutableArray *readers;
}

@property (nonatomic, assign) CGSize size;

@property (nonatomic, strong, readwrite) AVAssetReader *reader;

@property (atomic, assign) BOOL foreground;

@end

@implementation PKVideoDecoder

@synthesize foreground = _foreground;


#pragma mark - Initialization
Expand All @@ -47,13 +52,21 @@ - (instancetype)initWithVideoPath:(NSString *)videoPath size:(CGSize)size {
_size = size;
_asset = nil;
_keepLooping = YES;
readerLock = [[NSLock alloc] init];
readers = [NSMutableArray array];

[self yuvConversionSetup];
}
return self;
}

- (void)dealloc {
[_reader cancelReading];
[readerLock lock];
for (AVAssetReader *reader in readers) {
[reader cancelReading];
}
[readers removeAllObjects];
[readerLock unlock];
}

- (void)yuvConversionSetup {
Expand Down Expand Up @@ -107,41 +120,47 @@ - (AVAssetReader*)createAssetReader {
NSDictionary *outputSettings = @{
(id)kCVPixelBufferWidthKey:@(outputSize.width),
(id)kCVPixelBufferHeightKey:@(outputSize.height),
};
};

AVAssetReaderTrackOutput *readerVideoTrackOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:assetTrack outputSettings:outputSettings];
readerVideoTrackOutput.alwaysCopiesSampleData = NO;
[assetReader addOutput:readerVideoTrackOutput];

[readerLock lock];
self.reader = assetReader;
[readers addObject:assetReader];
[readerLock unlock];

return assetReader;
}

- (void)processAsset {
self.reader = [self createAssetReader];
AVAssetReader *reader = [self createAssetReader];

AVAssetReaderOutput *readerVideoTrackOutput = nil;

for( AVAssetReaderOutput *output in self.reader.outputs ) {
for( AVAssetReaderOutput *output in reader.outputs ) {
if( [output.mediaType isEqualToString:AVMediaTypeVideo] ) {
readerVideoTrackOutput = output;
}
}

if ([self.reader startReading] == NO) {
if (reader.status == AVAssetReaderStatusUnknown && [reader startReading] == NO && self.foreground) {
NSLog(@"Error reading from file at Path: %@", self.videoPath);
return;
}

__weak typeof(self)weakSelf = self;

while (self.reader.status == AVAssetReaderStatusReading) {
[weakSelf readNextVideoFrameFromOutput:readerVideoTrackOutput];
while (reader.status == AVAssetReaderStatusReading && self.foreground) {
[weakSelf readNextVideoFrameFromOutput:readerVideoTrackOutput reader:reader];
}

if (self.reader.status == AVAssetReaderStatusCompleted) {

[self.reader cancelReading];

[reader cancelReading];
[readerLock lock];
[readers removeObject:reader];
[readerLock unlock];
if (reader.status == AVAssetReaderStatusCompleted && self.foreground) {
if (self.keepLooping) {
self.reader = nil;
dispatch_async(dispatch_get_main_queue(), ^{
Expand All @@ -150,7 +169,6 @@ - (void)processAsset {
} else {
[weakSelf endProcessing];
}

}
}

Expand All @@ -159,6 +177,7 @@ - (void)processAsset {
#pragma mark - Public

- (void)startProcessing {
self.foreground = true;
previousFrameTime = kCMTimeZero;
previousActualFrameTime = CFAbsoluteTimeGetCurrent();

Expand Down Expand Up @@ -198,9 +217,7 @@ - (void)endProcessing {
}

- (void)cancelProcessing {
if (self.reader) {
[self.reader cancelReading];
}
self.foreground = false;
[self endProcessing];
}

Expand Down Expand Up @@ -246,9 +263,11 @@ - (void)convertYUVToRGBOutput {

#pragma mark - Pravite

- (BOOL)readNextVideoFrameFromOutput:(AVAssetReaderOutput *)readerVideoTrackOutput {
if (self.reader.status == AVAssetReaderStatusReading) {
- (BOOL)readNextVideoFrameFromOutput:(AVAssetReaderOutput *)readerVideoTrackOutput reader:(AVAssetReader*)reader {
[readerLock lock];
if (reader.status == AVAssetReaderStatusReading) {
CMSampleBufferRef sampleBufferRef = [readerVideoTrackOutput copyNextSampleBuffer];
[readerLock unlock];
if (sampleBufferRef) {
// Do this outside of the video processing queue to not slow that down while waiting
CMTime currentSampleTime = CMSampleBufferGetOutputPresentationTimeStamp(sampleBufferRef);
Expand Down Expand Up @@ -279,6 +298,8 @@ - (BOOL)readNextVideoFrameFromOutput:(AVAssetReaderOutput *)readerVideoTrackOutp
[self endProcessing];
}
}
} else {
[readerLock unlock];
}
return NO;
}
Expand Down