Skip to content

Commit 30d902a

Browse files
committed
FFTPlayer0x32 use double packet queue, double decoder thread
1 parent 226f067 commit 30d902a

File tree

4 files changed

+46
-19
lines changed

4 files changed

+46
-19
lines changed

Example/Common/4.Movie Player/VideoFrameQueue/MRVideoFrameQueueViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ - (void)parseURL:(NSString *)url
208208
[self.indicatorView stopAnimation:nil];
209209
self.audioFrameQueue = [[FFTAudioFrameQueue alloc] init];
210210
[self setupAudioRender:player.sampleFormat sampleRate:player.sampleRate];
211-
[self playAudio];
212211

213212
[self prepareRendererView];
214213
self.videoFrameQueue = [[FFTVideoFrameQueue alloc] init];
@@ -242,6 +241,7 @@ - (void)parseURL:(NSString *)url
242241
//audio
243242
else if (type == 2) {
244243
[self enQueueAudioFrame:frame];
244+
[self playAudio];
245245
}
246246
};
247247
[player prepareToPlay];

Example/iOS/iOSExample.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,8 @@
359359
C94AF2172B3040EE006DC380 /* 4.Movie Player */ = {
360360
isa = PBXGroup;
361361
children = (
362-
C94AF2182B3040EE006DC380 /* PacketQueue */,
363362
C94AF2352B3040EE006DC380 /* VideoFrameQueue */,
363+
C94AF2182B3040EE006DC380 /* PacketQueue */,
364364
C94AF2602B305659006DC380 /* VideoEmbed */,
365365
DDE075192B31DBB4004B7042 /* AudioEmbed */,
366366
C9F3DFD82B33CF4B003602E0 /* PlayProgress */,

Example/iOS/iOSExample/RootViewController/MRTableViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ - (void)viewDidLoad {
118118
},
119119
@{
120120
@"title":@"PacketQueue",
121-
@"detail":@"增加 AVPacket 缓存队列,创建解码线程",
121+
@"detail":@"增加 AVPacket 缓存队列,分别创建解码线程",
122122
@"class":@"MRPacketQueueViewController",
123123
},
124124
@{

FFmpegTutorial/t32/FFTPlayer0x32.m

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ @interface FFTPlayer0x32 ()<FFTDecoderDelegate>
3636
//音频重采样
3737
FFTAudioResample *_audioResample;
3838

39-
FFTPacketQueue *_packetQueue;
39+
FFTPacketQueue *_audioPacketQueue;
40+
FFTPacketQueue *_videoPacketQueue;
4041

4142
FFTVideoFrameQueue *_videoFrameQueue;
4243
//视频尺寸
@@ -49,7 +50,9 @@ @interface FFTPlayer0x32 ()<FFTDecoderDelegate>
4950

5051
//读包线程
5152
@property (nonatomic, strong) FFTThread *readThread;
52-
@property (nonatomic, strong) FFTThread *decoderThread;
53+
@property (nonatomic, strong) FFTThread *audioDecoderThread;
54+
@property (nonatomic, strong) FFTThread *videoDecoderThread;
55+
5356
@property (nonatomic, strong) FFTThread *videoThread;
5457

5558
@property (atomic, assign) int abort_request;
@@ -72,7 +75,8 @@ static int decode_interrupt_cb(void *ctx)
7275
- (void)_stop
7376
{
7477
self.abort_request = 1;
75-
[_packetQueue cancel];
78+
[_audioPacketQueue cancel];
79+
[_videoPacketQueue cancel];
7680
[_videoFrameQueue cancel];
7781

7882
//避免重复stop做无用功
@@ -81,9 +85,14 @@ - (void)_stop
8185
[self.readThread join];
8286
}
8387

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];
8796
}
8897

8998
if (self.videoThread) {
@@ -115,13 +124,17 @@ - (void)prepareToPlay
115124
NSAssert(NO, @"不允许重复创建");
116125
}
117126

118-
_packetQueue = [[FFTPacketQueue alloc] init];
127+
_audioPacketQueue = [[FFTPacketQueue alloc] init];
128+
_videoPacketQueue = [[FFTPacketQueue alloc] init];
119129

120130
self.readThread = [[FFTThread alloc] initWithTarget:self selector:@selector(readPacketsFunc) object:nil];
121131
self.readThread.name = @"mr-read";
122132

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";
125138

126139
self.videoThread = [[FFTThread alloc] initWithTarget:self selector:@selector(videoThreadFunc) object:nil];
127140
self.videoThread.name = @"mr-v-display";
@@ -154,9 +167,9 @@ - (void)readPacketLoop:(AVFormatContext *)formatCtx
154167
pkt->data = NULL;
155168
pkt->size = 0;
156169
pkt->stream_index = _videoDecoder.streamIdx;
157-
[_packetQueue enQueue:pkt];
170+
[_videoPacketQueue enQueue:pkt];
158171
pkt->stream_index = _audioDecoder.streamIdx;
159-
[_packetQueue enQueue:pkt];
172+
[_audioPacketQueue enQueue:pkt];
160173
break;
161174
}
162175

@@ -175,15 +188,15 @@ - (void)readPacketLoop:(AVFormatContext *)formatCtx
175188
if (pkt->data != NULL) {
176189
self.videoPktCount++;
177190
}
178-
[_packetQueue enQueue:pkt];
191+
[_videoPacketQueue enQueue:pkt];
179192
}
180193
break;
181194
case AVMEDIA_TYPE_AUDIO:
182195
{
183196
if (pkt->data != NULL) {
184197
self.audioPktCount++;
185198
}
186-
[_packetQueue enQueue:pkt];
199+
[_audioPacketQueue enQueue:pkt];
187200
}
188201
break;
189202
default:
@@ -343,7 +356,8 @@ - (void)readPacketsFunc
343356
});
344357

345358
_formatCtx = formatCtx;
346-
[self.decoderThread start];
359+
[self.audioDecoderThread start];
360+
[self.videoDecoderThread start];
347361
[self.videoThread start];
348362
//循环读包
349363
[self readPacketLoop:formatCtx];
@@ -460,11 +474,24 @@ - (void)decodePkt:(AVPacket *)pkt
460474
}
461475
}
462476

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
464491
{
465492
while (!self.abort_request) {
466493
__weakSelf__
467-
[_packetQueue deQueue:^(AVPacket * pkt) {
494+
[_videoPacketQueue deQueue:^(AVPacket * pkt) {
468495
__strongSelf__
469496
if (pkt) {
470497
[self decodePkt:pkt];

0 commit comments

Comments
 (0)