Skip to content

Commit

Permalink
SGDecoder: Add session finished.
Browse files Browse the repository at this point in the history
  • Loading branch information
libobjc committed Nov 28, 2019
1 parent b20dac5 commit a1373cb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
12 changes: 12 additions & 0 deletions SGPlayer/Classes/Core/SGDecoder/SGAudioDecoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ @interface SGAudioDecoder ()
struct {
BOOL needsAlignment;
BOOL needsResetSonic;
BOOL sessionFinished;
int64_t nextTimeStamp;
CMTime lastEndTimeStamp;
} _flags;
Expand Down Expand Up @@ -62,6 +63,7 @@ - (void)destroy
self->_flags.nextTimeStamp = 0;
self->_flags.needsAlignment = YES;
self->_flags.needsResetSonic = YES;
self->_flags.sessionFinished = NO;
self->_flags.lastEndTimeStamp = kCMTimeInvalid;
[self->_codecContext close];
self->_codecContext = nil;
Expand All @@ -76,6 +78,7 @@ - (void)flush
self->_flags.nextTimeStamp = 0;
self->_flags.needsAlignment = YES;
self->_flags.needsResetSonic = YES;
self->_flags.sessionFinished = NO;
self->_flags.lastEndTimeStamp = kCMTimeInvalid;
[self->_codecContext flush];
[self->_formatter flush];
Expand All @@ -101,6 +104,9 @@ - (void)flush
[self setup];
}
}
if (self->_flags.sessionFinished) {
return nil;
}
[cd fillToDescriptor:self->_codecDescriptor];
if (packet.flags & SGDataFlagPadding) {
SGAudioDescriptor *ad = self->_audioDescriptor;
Expand Down Expand Up @@ -135,6 +141,9 @@ - (void)flush

- (NSArray<__kindof SGFrame *> *)finish
{
if (self->_flags.sessionFinished) {
return nil;
}
NSArray<SGFrame *> *frames = [self processPacket:nil];
if (frames.count > 0) {
self->_flags.lastEndTimeStamp = CMTimeAdd(frames.lastObject.timeStamp, frames.lastObject.duration);
Expand Down Expand Up @@ -262,6 +271,7 @@ - (void)flush
CMTime duration = CMTimeSubtract(CMTimeRangeGetEnd(timeRange), start);
int nb_samples = (int)CMTimeConvertScale(duration, ad.sampleRate, kCMTimeRoundingMethod_RoundTowardZero).value;
if (nb_samples < obj.numberOfSamples) {
self->_flags.sessionFinished = YES;
duration = CMTimeMake(nb_samples, ad.sampleRate);
SGAudioFrame *obj1 = [SGAudioFrame frameWithDescriptor:ad numberOfSamples:nb_samples];
for (int i = 0; i < ad.numberOfPlanes; i++) {
Expand All @@ -275,6 +285,8 @@ - (void)flush
[ret addObject:obj1];
[obj unlock];
continue;
} else if (nb_samples == obj.numberOfSamples) {
self->_flags.sessionFinished = YES;
}
}
[ret addObject:obj];
Expand Down
3 changes: 3 additions & 0 deletions SGPlayer/Classes/Core/SGDecoder/SGVideoDecoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ - (void)flush

- (NSArray<__kindof SGFrame *> *)finish
{
if (self->_flags.sessionFinished) {
return nil;
}
NSArray<SGFrame *> *frames = [self processPacket:nil];
if (frames.count == 0 &&
self->_lastDecodeFrame &&
Expand Down

0 comments on commit a1373cb

Please sign in to comment.