Skip to content

Commit 4432edf

Browse files
committed
FFTPlayer0x35 use double packet queue, double decoder thread
1 parent cd79736 commit 4432edf

File tree

1 file changed

+46
-21
lines changed

1 file changed

+46
-21
lines changed

FFmpegTutorial/t35/FFTPlayer0x35.m

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ @interface FFTPlayer0x35 ()<FFTDecoderDelegate>
4747
//音频重采样
4848
FFTAudioResample *_audioResample;
4949

50-
FFTPacketQueue *_packetQueue;
50+
FFTPacketQueue *_audioPacketQueue;
51+
FFTPacketQueue *_videoPacketQueue;
5152

5253
FFTVideoFrameQueue *_videoFrameQueue;
5354
FFTAudioFrameQueue *_audioFrameQueue;
@@ -74,7 +75,8 @@ @interface FFTPlayer0x35 ()<FFTDecoderDelegate>
7475

7576
//读包线程
7677
@property (nonatomic, strong) FFTThread *readThread;
77-
@property (nonatomic, strong) FFTThread *decoderThread;
78+
@property (nonatomic, strong) FFTThread *audioDecoderThread;
79+
@property (nonatomic, strong) FFTThread *videoDecoderThread;
7880
@property (nonatomic, strong) FFTThread *videoThread;
7981

8082
@property (atomic, assign) int abort_request;
@@ -97,7 +99,8 @@ static int decode_interrupt_cb(void *ctx)
9799
- (void)_stop
98100
{
99101
self.abort_request = 1;
100-
[_packetQueue cancel];
102+
[_audioPacketQueue cancel];
103+
[_videoPacketQueue cancel];
101104
[_videoFrameQueue cancel];
102105
[_audioFrameQueue cancel];
103106

@@ -112,9 +115,14 @@ - (void)_stop
112115
[self.readThread join];
113116
}
114117

115-
if (self.decoderThread) {
116-
[self.decoderThread cancel];
117-
[self.decoderThread join];
118+
if (self.audioDecoderThread) {
119+
[self.audioDecoderThread cancel];
120+
[self.audioDecoderThread join];
121+
}
122+
123+
if (self.videoDecoderThread) {
124+
[self.videoDecoderThread cancel];
125+
[self.videoDecoderThread join];
118126
}
119127

120128
if (self.videoThread) {
@@ -144,13 +152,17 @@ - (void)prepareToPlay
144152
NSAssert(NO, @"不允许重复创建");
145153
}
146154

147-
_packetQueue = [[FFTPacketQueue alloc] init];
155+
_audioPacketQueue = [[FFTPacketQueue alloc] init];
156+
_videoPacketQueue = [[FFTPacketQueue alloc] init];
148157

149158
self.readThread = [[FFTThread alloc] initWithTarget:self selector:@selector(readPacketsFunc) object:nil];
150159
self.readThread.name = @"mr-read";
151160

152-
self.decoderThread = [[FFTThread alloc] initWithTarget:self selector:@selector(decoderFunc) object:nil];
153-
self.decoderThread.name = @"mr-decoder";
161+
self.audioDecoderThread = [[FFTThread alloc] initWithTarget:self selector:@selector(audioDecoderFunc) object:nil];
162+
self.audioDecoderThread.name = @"audio-decoder";
163+
164+
self.videoDecoderThread = [[FFTThread alloc] initWithTarget:self selector:@selector(videoDecoderFunc) object:nil];
165+
self.videoDecoderThread.name = @"video-decoder";
154166

155167
self.videoThread = [[FFTThread alloc] initWithTarget:self selector:@selector(videoThreadFunc) object:nil];
156168
self.videoThread.name = @"mr-v-display";
@@ -183,9 +195,9 @@ - (void)readPacketLoop:(AVFormatContext *)formatCtx
183195
pkt->data = NULL;
184196
pkt->size = 0;
185197
pkt->stream_index = _videoDecoder.streamIdx;
186-
[_packetQueue enQueue:pkt];
198+
[_videoPacketQueue enQueue:pkt];
187199
pkt->stream_index = _audioDecoder.streamIdx;
188-
[_packetQueue enQueue:pkt];
200+
[_audioPacketQueue enQueue:pkt];
189201
break;
190202
}
191203

@@ -204,15 +216,15 @@ - (void)readPacketLoop:(AVFormatContext *)formatCtx
204216
if (pkt->data != NULL) {
205217
self.videoPktCount++;
206218
}
207-
[_packetQueue enQueue:pkt];
219+
[_videoPacketQueue enQueue:pkt];
208220
}
209221
break;
210222
case AVMEDIA_TYPE_AUDIO:
211223
{
212224
if (pkt->data != NULL) {
213225
self.audioPktCount++;
214226
}
215-
[_packetQueue enQueue:pkt];
227+
[_audioPacketQueue enQueue:pkt];
216228
}
217229
break;
218230
default:
@@ -383,8 +395,9 @@ - (void)readPacketsFunc
383395
});
384396

385397
_formatCtx = formatCtx;
386-
[self.decoderThread start];
387-
//[self.videoThread start];
398+
[self.audioDecoderThread start];
399+
[self.videoDecoderThread start];
400+
388401
//循环读包
389402
[self readPacketLoop:formatCtx];
390403
}
@@ -467,7 +480,6 @@ - (FFTAudioResample *)createAudioResampleIfNeed
467480

468481
#pragma mark - 解码
469482

470-
471483
- (FFTDecoder *)openStreamComponent:(AVFormatContext *)ic streamIdx:(int)idx
472484
{
473485
FFTDecoder *decoder = [FFTDecoder new];
@@ -500,11 +512,11 @@ - (void)decodePkt:(AVPacket *)pkt
500512
}
501513
}
502514

503-
- (void)decoderFunc
515+
- (void)audioDecoderFunc
504516
{
505517
while (!self.abort_request) {
506518
__weakSelf__
507-
[_packetQueue deQueue:^(AVPacket * pkt) {
519+
[_audioPacketQueue deQueue:^(AVPacket * pkt) {
508520
__strongSelf__
509521
if (pkt) {
510522
[self decodePkt:pkt];
@@ -513,6 +525,20 @@ - (void)decoderFunc
513525
}
514526
}
515527

528+
- (void)videoDecoderFunc
529+
{
530+
while (!self.abort_request) {
531+
__weakSelf__
532+
[_videoPacketQueue deQueue:^(AVPacket * pkt) {
533+
__strongSelf__
534+
if (pkt) {
535+
[self decodePkt:pkt];
536+
}
537+
}];
538+
}
539+
}
540+
541+
516542
#pragma mark - FFTDecoderDelegate
517543

518544
- (void)decoder:(FFTDecoder *)decoder reveivedAFrame:(AVFrame *)aFrame
@@ -759,7 +785,6 @@ - (void)enQueueAudioFrame:(AVFrame *)frame
759785
{
760786
const char *fmt_str = av_sample_fmt_to_string(frame->format);
761787
self.audioSamplelInfo = [NSString stringWithFormat:@"(%s)%d",fmt_str,frame->sample_rate];
762-
763788
[_audioFrameQueue enQueue:frame];
764789
}
765790

@@ -819,10 +844,10 @@ - (NSString *)audioRenderName
819844
return [_audioRender name];
820845
}
821846

822-
- (UIView *)videoRender
847+
- (UIView<IJKVideoRenderingProtocol> *)videoRender
823848
{
824849
if (!_videoRender) {
825-
id videoRender = [[IJKMetalView alloc] init];
850+
IJKMetalView *videoRender = [[IJKMetalView alloc] init];
826851
_videoRender = videoRender;
827852
}
828853
return _videoRender;

0 commit comments

Comments
 (0)