Skip to content

Commit 1564cf9

Browse files
committed
SGDemuxable: Add shared demuxer.
1 parent f77fe27 commit 1564cf9

12 files changed

Lines changed: 139 additions & 31 deletions

SGPlayer/Classes/Core/SGAsset/SGPaddingSegment.m

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,19 @@ - (instancetype)initWithDuration:(CMTime)duration
2727
return self;
2828
}
2929

30-
- (id<SGDemuxable>)newDemuxable
30+
- (NSString *)sharedDemuxerKey
31+
{
32+
return nil;
33+
}
34+
35+
- (id<SGDemuxable>)newDemuxer
3136
{
3237
return [[SGPaddingDemuxer alloc] initWithDuration:self->_duration];
3338
}
3439

40+
- (id<SGDemuxable>)newDemuxerWithSharedDemuxer:(id<SGDemuxable>)demuxer
41+
{
42+
return [self newDemuxer];
43+
}
44+
3545
@end

SGPlayer/Classes/Core/SGAsset/SGSegment+Internal.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414
/**
1515
*
1616
*/
17-
- (id<SGDemuxable>)newDemuxable;
17+
- (NSString *)sharedDemuxerKey;
18+
19+
/**
20+
*
21+
*/
22+
- (id<SGDemuxable>)newDemuxer;
23+
24+
/**
25+
*
26+
*/
27+
- (id<SGDemuxable>)newDemuxerWithSharedDemuxer:(id<SGDemuxable>)demuxer;
1828

1929
@end

SGPlayer/Classes/Core/SGAsset/SGSegment.m

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,19 @@ - (id)copyWithZone:(NSZone *)zone
3434
return obj;
3535
}
3636

37-
- (id<SGDemuxable>)newDemuxable
37+
- (NSString *)sharedDemuxerKey
38+
{
39+
NSAssert(NO, @"Subclass only.");
40+
return nil;
41+
}
42+
43+
- (id<SGDemuxable>)newDemuxer
44+
{
45+
NSAssert(NO, @"Subclass only.");
46+
return nil;
47+
}
48+
49+
- (id<SGDemuxable>)newDemuxerWithSharedDemuxer:(id<SGDemuxable>)demuxer
3850
{
3951
NSAssert(NO, @"Subclass only.");
4052
return nil;

SGPlayer/Classes/Core/SGAsset/SGURLSegment.m

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,22 @@ - (instancetype)initWithURL:(NSURL *)URL index:(NSInteger)index timeRange:(CMTim
3838
return self;
3939
}
4040

41-
- (id<SGDemuxable>)newDemuxable
41+
- (NSString *)sharedDemuxerKey
4242
{
43-
SGURLDemuxer *demuxable = [[SGURLDemuxer alloc] initWithURL:self->_URL];
44-
SGExtractingDemuxer *obj = [[SGExtractingDemuxer alloc] initWithDemuxable:demuxable index:self->_index timeRange:self->_timeRange scale:self->_scale];
43+
return self->_URL.isFileURL ? self->_URL.path : self->_URL.absoluteString;
44+
}
45+
46+
- (id<SGDemuxable>)newDemuxer
47+
{
48+
return [self newDemuxerWithSharedDemuxer:nil];
49+
}
50+
51+
- (id<SGDemuxable>)newDemuxerWithSharedDemuxer:(id<SGDemuxable>)demuxer
52+
{
53+
if (!demuxer) {
54+
demuxer = [[SGURLDemuxer alloc] initWithURL:self->_URL];
55+
}
56+
SGExtractingDemuxer *obj = [[SGExtractingDemuxer alloc] initWithDemuxable:demuxer index:self->_index timeRange:self->_timeRange scale:self->_scale];
4557
return obj;
4658
}
4759

SGPlayer/Classes/Core/SGDecoder/SGAudioDecoder.m

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,20 @@ - (void)flush
8282
NSMutableArray<__kindof SGFrame *> *ret = [NSMutableArray array];
8383
SGCodecDescriptor *cd = packet.codecDescriptor;
8484
NSAssert(cd, @"Invalid codec descriptor.");
85-
if (![cd isEqualCodecContextToDescriptor:self->_codecDescriptor]) {
85+
BOOL isEqual = [cd isEqualToDescriptor:self->_codecDescriptor];
86+
BOOL isEqualCodec = [cd isEqualCodecContextToDescriptor:self->_codecDescriptor];
87+
if (!isEqual) {
8688
NSArray<SGFrame *> *objs = [self finish];
8789
for (SGFrame *obj in objs) {
8890
[ret addObject:obj];
8991
}
9092
self->_codecDescriptor = [cd copy];
91-
[self destroy];
92-
[self setup];
93+
if (isEqualCodec) {
94+
[self flush];
95+
} else {
96+
[self destroy];
97+
[self setup];
98+
}
9399
}
94100
[cd fillToDescriptor:self->_codecDescriptor];
95101
if (packet.flags & SGDataFlagPadding) {

SGPlayer/Classes/Core/SGDecoder/SGVideoDecoder.m

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,20 @@ - (void)flush
9393
NSMutableArray *ret = [NSMutableArray array];
9494
SGCodecDescriptor *cd = packet.codecDescriptor;
9595
NSAssert(cd, @"Invalid Codec Descriptor.");
96-
if (![cd isEqualCodecContextToDescriptor:self->_codecDescriptor]) {
96+
BOOL isEqual = [cd isEqualToDescriptor:self->_codecDescriptor];
97+
BOOL isEqualCodec = [cd isEqualCodecContextToDescriptor:self->_codecDescriptor];
98+
if (!isEqual) {
9799
NSArray<SGFrame *> *objs = [self finish];
98100
for (SGFrame *obj in objs) {
99101
[ret addObject:obj];
100102
}
101103
self->_codecDescriptor = [cd copy];
102-
[self destroy];
103-
[self setup];
104+
if (isEqualCodec) {
105+
[self flush];
106+
} else {
107+
[self destroy];
108+
[self setup];
109+
}
104110
}
105111
if (self->_flags.sessionFinished) {
106112
return nil;

SGPlayer/Classes/Core/SGDemuxer/SGDemuxable.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
*
1919
*/
20-
@property (nonatomic, strong) SGDemuxerOptions *options;
20+
@property (nonatomic, copy) SGDemuxerOptions *options;
2121

2222
/**
2323
*
@@ -39,6 +39,11 @@
3939
*/
4040
@property (nonatomic, readonly) CMTime duration;
4141

42+
/**
43+
*
44+
*/
45+
- (id<SGDemuxable>)sharedDemuxer;
46+
4247
/**
4348
*
4449
*/

SGPlayer/Classes/Core/SGDemuxer/SGExtractingDemuxer.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ - (instancetype)initWithDemuxable:(id<SGDemuxable>)demuxable index:(NSInteger)in
5959

6060
#pragma mark - Control
6161

62+
- (id<SGDemuxable>)sharedDemuxer
63+
{
64+
return [self->_demuxable sharedDemuxer];
65+
}
66+
6267
- (NSError *)open
6368
{
6469
NSError *error = [self->_demuxable open];

SGPlayer/Classes/Core/SGDemuxer/SGMutilDemuxer.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ - (SGDemuxerOptions *)options
6060

6161
#pragma mark - Control
6262

63+
- (id<SGDemuxable>)sharedDemuxer
64+
{
65+
return nil;
66+
}
67+
6368
- (NSError *)open
6469
{
6570
for (id<SGDemuxable> obj in self->_demuxers) {

SGPlayer/Classes/Core/SGDemuxer/SGPaddingDemuxer.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ - (instancetype)initWithDuration:(CMTime)duration
3535

3636
#pragma mark - Control
3737

38+
- (id<SGDemuxable>)sharedDemuxer
39+
{
40+
return nil;
41+
}
42+
3843
- (NSError *)open
3944
{
4045
return nil;

0 commit comments

Comments
 (0)