@@ -36,7 +36,8 @@ @interface FFTPlayer0x32 ()<FFTDecoderDelegate>
36
36
// 音频重采样
37
37
FFTAudioResample *_audioResample;
38
38
39
- FFTPacketQueue *_packetQueue;
39
+ FFTPacketQueue *_audioPacketQueue;
40
+ FFTPacketQueue *_videoPacketQueue;
40
41
41
42
FFTVideoFrameQueue *_videoFrameQueue;
42
43
// 视频尺寸
@@ -49,7 +50,9 @@ @interface FFTPlayer0x32 ()<FFTDecoderDelegate>
49
50
50
51
// 读包线程
51
52
@property (nonatomic , strong ) FFTThread *readThread;
52
- @property (nonatomic , strong ) FFTThread *decoderThread;
53
+ @property (nonatomic , strong ) FFTThread *audioDecoderThread;
54
+ @property (nonatomic , strong ) FFTThread *videoDecoderThread;
55
+
53
56
@property (nonatomic , strong ) FFTThread *videoThread;
54
57
55
58
@property (atomic , assign ) int abort_request;
@@ -72,7 +75,8 @@ static int decode_interrupt_cb(void *ctx)
72
75
- (void )_stop
73
76
{
74
77
self.abort_request = 1 ;
75
- [_packetQueue cancel ];
78
+ [_audioPacketQueue cancel ];
79
+ [_videoPacketQueue cancel ];
76
80
[_videoFrameQueue cancel ];
77
81
78
82
// 避免重复stop做无用功
@@ -81,9 +85,14 @@ - (void)_stop
81
85
[self .readThread join ];
82
86
}
83
87
84
- if (self.decoderThread ) {
85
- [self .decoderThread cancel ];
86
- [self .decoderThread join ];
88
+ if (self.audioDecoderThread ) {
89
+ [self .audioDecoderThread cancel ];
90
+ [self .audioDecoderThread join ];
91
+ }
92
+
93
+ if (self.videoDecoderThread ) {
94
+ [self .videoDecoderThread cancel ];
95
+ [self .videoDecoderThread join ];
87
96
}
88
97
89
98
if (self.videoThread ) {
@@ -115,13 +124,17 @@ - (void)prepareToPlay
115
124
NSAssert (NO , @" 不允许重复创建" );
116
125
}
117
126
118
- _packetQueue = [[FFTPacketQueue alloc ] init ];
127
+ _audioPacketQueue = [[FFTPacketQueue alloc ] init ];
128
+ _videoPacketQueue = [[FFTPacketQueue alloc ] init ];
119
129
120
130
self.readThread = [[FFTThread alloc ] initWithTarget: self selector: @selector (readPacketsFunc ) object: nil ];
121
131
self.readThread .name = @" mr-read" ;
122
132
123
- self.decoderThread = [[FFTThread alloc ] initWithTarget: self selector: @selector (decoderFunc ) object: nil ];
124
- self.decoderThread .name = @" mr-decoder" ;
133
+ self.audioDecoderThread = [[FFTThread alloc ] initWithTarget: self selector: @selector (audioDecoderFunc ) object: nil ];
134
+ self.audioDecoderThread .name = @" audio-decoder" ;
135
+
136
+ self.videoDecoderThread = [[FFTThread alloc ] initWithTarget: self selector: @selector (videoDecoderFunc ) object: nil ];
137
+ self.videoDecoderThread .name = @" video-decoder" ;
125
138
126
139
self.videoThread = [[FFTThread alloc ] initWithTarget: self selector: @selector (videoThreadFunc ) object: nil ];
127
140
self.videoThread .name = @" mr-v-display" ;
@@ -154,9 +167,9 @@ - (void)readPacketLoop:(AVFormatContext *)formatCtx
154
167
pkt->data = NULL ;
155
168
pkt->size = 0 ;
156
169
pkt->stream_index = _videoDecoder.streamIdx ;
157
- [_packetQueue enQueue: pkt];
170
+ [_videoPacketQueue enQueue: pkt];
158
171
pkt->stream_index = _audioDecoder.streamIdx ;
159
- [_packetQueue enQueue: pkt];
172
+ [_audioPacketQueue enQueue: pkt];
160
173
break ;
161
174
}
162
175
@@ -175,15 +188,15 @@ - (void)readPacketLoop:(AVFormatContext *)formatCtx
175
188
if (pkt->data != NULL ) {
176
189
self.videoPktCount ++;
177
190
}
178
- [_packetQueue enQueue: pkt];
191
+ [_videoPacketQueue enQueue: pkt];
179
192
}
180
193
break ;
181
194
case AVMEDIA_TYPE_AUDIO:
182
195
{
183
196
if (pkt->data != NULL ) {
184
197
self.audioPktCount ++;
185
198
}
186
- [_packetQueue enQueue: pkt];
199
+ [_audioPacketQueue enQueue: pkt];
187
200
}
188
201
break ;
189
202
default :
@@ -343,7 +356,8 @@ - (void)readPacketsFunc
343
356
});
344
357
345
358
_formatCtx = formatCtx;
346
- [self .decoderThread start ];
359
+ [self .audioDecoderThread start ];
360
+ [self .videoDecoderThread start ];
347
361
[self .videoThread start ];
348
362
// 循环读包
349
363
[self readPacketLoop: formatCtx];
@@ -460,11 +474,24 @@ - (void)decodePkt:(AVPacket *)pkt
460
474
}
461
475
}
462
476
463
- - (void )decoderFunc
477
+ - (void )audioDecoderFunc
478
+ {
479
+ while (!self.abort_request ) {
480
+ __weakSelf__
481
+ [_audioPacketQueue deQueue: ^(AVPacket * pkt) {
482
+ __strongSelf__
483
+ if (pkt) {
484
+ [self decodePkt: pkt];
485
+ }
486
+ }];
487
+ }
488
+ }
489
+
490
+ - (void )videoDecoderFunc
464
491
{
465
492
while (!self.abort_request ) {
466
493
__weakSelf__
467
- [_packetQueue deQueue: ^(AVPacket * pkt) {
494
+ [_videoPacketQueue deQueue: ^(AVPacket * pkt) {
468
495
__strongSelf__
469
496
if (pkt) {
470
497
[self decodePkt: pkt];
0 commit comments