Skip to content

Commit

Permalink
SGDecoder: timeRange.
Browse files Browse the repository at this point in the history
  • Loading branch information
libobjc committed Nov 2, 2019
1 parent ed46596 commit f11dc0b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
38 changes: 20 additions & 18 deletions SGPlayer/Classes/Core/SGDecoder/SGAudioDecoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ - (void)flush

- (NSArray<__kindof SGFrame *> *)clipFrames:(NSArray<__kindof SGFrame *> *)frames timeRange:(CMTimeRange)timeRange
{
if (!SGCMTimeIsValid(timeRange.start, NO) ||
!SGCMTimeIsValid(timeRange.duration, NO)) {
if (!SGCMTimeIsValid(timeRange.start, NO)) {
return frames;
}
NSMutableArray *ret = [NSMutableArray array];
Expand All @@ -187,7 +186,8 @@ - (void)flush
[obj unlock];
continue;
}
if (CMTimeCompare(obj.timeStamp, CMTimeRangeGetEnd(timeRange)) >= 0) {
if (SGCMTimeIsValid(timeRange.duration, NO) &&
CMTimeCompare(obj.timeStamp, CMTimeRangeGetEnd(timeRange)) >= 0) {
[obj unlock];
continue;
}
Expand All @@ -207,22 +207,24 @@ - (void)flush
[ret addObject:obj1];
}
}
CMTime start = obj.timeStamp;
CMTime duration = CMTimeSubtract(CMTimeRangeGetEnd(timeRange), obj.timeStamp);
int nb_samples = (int)CMTimeConvertScale(duration, ad.sampleRate, kCMTimeRoundingMethod_RoundTowardZero).value;
if (nb_samples < obj.numberOfSamples) {
duration = CMTimeMake(nb_samples, ad.sampleRate);
SGAudioFrame *obj1 = [SGAudioFrame audioFrameWithDescriptor:ad numberOfSamples:nb_samples];
for (int i = 0; i < ad.numberOfPlanes; i++) {
memcpy(obj1.core->data[i], obj.core->data[i], obj1.core->linesize[i]);
if (SGCMTimeIsValid(timeRange.duration, NO)) {
CMTime start = obj.timeStamp;
CMTime duration = CMTimeSubtract(CMTimeRangeGetEnd(timeRange), start);
int nb_samples = (int)CMTimeConvertScale(duration, ad.sampleRate, kCMTimeRoundingMethod_RoundTowardZero).value;
if (nb_samples < obj.numberOfSamples) {
duration = CMTimeMake(nb_samples, ad.sampleRate);
SGAudioFrame *obj1 = [SGAudioFrame audioFrameWithDescriptor:ad numberOfSamples:nb_samples];
for (int i = 0; i < ad.numberOfPlanes; i++) {
memcpy(obj1.core->data[i], obj.core->data[i], obj1.core->linesize[i]);
}
SGCodecDescriptor *cd = [[SGCodecDescriptor alloc] init];
cd.track = obj.track;
[obj1 setCodecDescriptor:cd];
[obj1 fillWithTimeStamp:start decodeTimeStamp:start duration:duration];
[ret addObject:obj1];
[obj unlock];
continue;
}
SGCodecDescriptor *cd = [[SGCodecDescriptor alloc] init];
cd.track = obj.track;
[obj1 setCodecDescriptor:cd];
[obj1 fillWithTimeStamp:start decodeTimeStamp:start duration:duration];
[ret addObject:obj1];
[obj unlock];
continue;
}
[ret addObject:obj];
}
Expand Down
24 changes: 13 additions & 11 deletions SGPlayer/Classes/Core/SGDecoder/SGVideoDecoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ - (void)flush

- (NSArray<__kindof SGFrame *> *)clipFrames:(NSArray<__kindof SGFrame *> *)frames timeRange:(CMTimeRange)timeRange
{
if (!SGCMTimeIsValid(timeRange.start, NO) ||
!SGCMTimeIsValid(timeRange.duration, NO)) {
if (!SGCMTimeIsValid(timeRange.start, NO)) {
return frames;
}
NSMutableArray *ret = [NSMutableArray array];
Expand All @@ -128,7 +127,8 @@ - (void)flush
[obj unlock];
continue;
}
if (CMTimeCompare(obj.timeStamp, CMTimeRangeGetEnd(timeRange)) >= 0) {
if (SGCMTimeIsValid(timeRange.duration, NO) &&
CMTimeCompare(obj.timeStamp, CMTimeRangeGetEnd(timeRange)) >= 0) {
[obj unlock];
continue;
}
Expand All @@ -144,14 +144,16 @@ - (void)flush
[obj fillWithTimeStamp:start decodeTimeStamp:start duration:duration];
}
}
CMTime start = obj.timeStamp;
CMTime duration = CMTimeSubtract(CMTimeRangeGetEnd(timeRange), obj.timeStamp);
if (CMTimeCompare(obj.duration, duration) > 0) {
SGCodecDescriptor *cd = [[SGCodecDescriptor alloc] init];
cd.track = obj.track;
cd.metadata = obj.codecDescriptor.metadata;
[obj setCodecDescriptor:cd];
[obj fillWithTimeStamp:start decodeTimeStamp:start duration:duration];
if (SGCMTimeIsValid(timeRange.duration, NO)) {
CMTime start = obj.timeStamp;
CMTime duration = CMTimeSubtract(CMTimeRangeGetEnd(timeRange), start);
if (CMTimeCompare(obj.duration, duration) > 0) {
SGCodecDescriptor *cd = [[SGCodecDescriptor alloc] init];
cd.track = obj.track;
cd.metadata = obj.codecDescriptor.metadata;
[obj setCodecDescriptor:cd];
[obj fillWithTimeStamp:start decodeTimeStamp:start duration:duration];
}
}
[ret addObject:obj];
}
Expand Down

0 comments on commit f11dc0b

Please sign in to comment.