Skip to content

Commit 0af894a

Browse files
committed
Simplify some code
1 parent c5c673a commit 0af894a

File tree

4 files changed

+33
-122
lines changed

4 files changed

+33
-122
lines changed

Source/GSAudioPlayer.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
BOOL _running;
6969
BOOL _started;
7070
BOOL _muted;
71-
BOOL _paused;
7271
}
7372

7473
- (void) prepareWithFormatContext: (AVFormatContext *)formatCtx
@@ -87,8 +86,6 @@
8786

8887
- (void) setMuted: (BOOL)muted;
8988
- (BOOL) isMuted;
90-
- (void) setPaused: (BOOL)f;
91-
- (BOOL) isPaused;
9289
- (void) setPlaying: (BOOL)f;
9390
- (BOOL) isPlaying;
9491

Source/GSAudioPlayer.m

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ - (instancetype) init
4444
_running = NO;
4545
_volume = 1.0;
4646
_started = NO;
47-
_paused = NO;
4847
_loopMode = NSQTMovieNormalPlayback;
4948
}
5049
return self;
@@ -80,16 +79,6 @@ - (void)dealloc
8079
[super dealloc];
8180
}
8281

83-
- (void) setPaused: (BOOL)f;
84-
{
85-
_paused = f;
86-
}
87-
88-
- (BOOL) isPaused
89-
{
90-
return _paused;
91-
}
92-
9382
- (void) setPlaying: (BOOL)f
9483
{
9584
_running = f;
@@ -169,13 +158,6 @@ - (void)audioThreadEntry
169158
{
170159
while (_running)
171160
{
172-
// Stop reading packets while paused...
173-
if (_paused == YES)
174-
{
175-
usleep(10000); // 10ms pause
176-
continue; // start the loop again...
177-
}
178-
179161
// create pool...
180162
CREATE_AUTORELEASE_POOL(pool);
181163
{
@@ -272,12 +254,6 @@ - (void)decodePacket: (AVPacket *)packet
272254
- (void) submitPacket: (AVPacket *)packet
273255
{
274256
NSDictionary *dict = NSDictionaryFromAVPacket(packet);
275-
276-
if (_paused)
277-
{
278-
NSLog(@"Submitted audio packet...");
279-
}
280-
281257
@synchronized (_audioPackets)
282258
{
283259
[_audioPackets addObject: dict];

Source/GSMovieView.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,12 @@ APPKIT_EXPORT_CLASS
6666
struct SwsContext *_swsCtx;
6767
AVRational _timeBase;
6868

69-
BOOL _paused; // is the stream paused...
7069
BOOL _running; // is the loop currently running...
7170
BOOL _started; // has the video started...
7271
int64_t _videoClock;
7372
int _videoStreamIndex;
7473
int _audioStreamIndex;
7574
int64_t _lastPts;
76-
int64_t _savedPts;
7775
CGFloat _fps;
7876
}
7977

@@ -88,8 +86,6 @@ APPKIT_EXPORT_CLASS
8886
// Start and stop...
8987
- (void) startVideo;
9088
- (void) stopVideo;
91-
- (void) setPaused: (BOOL)f;
92-
- (BOOL) isPaused;
9389

9490
// Main loop to process packets...
9591
- (void) renderFrame: (AVFrame *)videoFrame;

Source/GSMovieView.m

Lines changed: 33 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
#import "GSAudioPlayer.h"
4848
#import "GSAVUtils.h"
4949

50+
#define BUFFER_SIZE 2048
51+
5052
static NSNotificationCenter *nc = nil;
5153

5254
// Ignore the warning this will produce as it is intentional.
@@ -143,12 +145,13 @@ - (instancetype) initWithFrame: (NSRect)frame
143145
// Flags...
144146
_running = NO;
145147
_started = NO;
146-
_paused = NO;
147-
148+
149+
/*
148150
[nc addObserver: self
149151
selector: @selector(handleNotification:)
150152
name: NSApplicationWillTerminateNotification
151153
object: nil];
154+
*/
152155
}
153156
return self;
154157
}
@@ -191,18 +194,6 @@ - (void)dealloc
191194
[super dealloc];
192195
}
193196

194-
// New methods...
195-
- (void) setPaused: (BOOL)f
196-
{
197-
_paused = f;
198-
[_audioPlayer setPaused: f];
199-
}
200-
201-
- (BOOL) isPaused
202-
{
203-
return _paused;
204-
}
205-
206197
// Notification responses...
207198
- (void) handleNotification: (NSNotification *)notification
208199
{
@@ -222,7 +213,6 @@ - (void) _startFeed
222213
[self setRate: 1.0 / 30.0];
223214
[self setVolume: 1.0];
224215

225-
_savedPts = 0;
226216
_feedThread = [[NSThread alloc] initWithTarget:self selector:@selector(feedVideo) object:nil];
227217
[_feedThread start];
228218
[_audioPlayer startAudio];
@@ -233,7 +223,6 @@ - (void) _stopFeed
233223
{
234224
if (_running == YES)
235225
{
236-
_savedPts = _lastPts;
237226
[_feedThread cancel];
238227
[_audioPlayer stopAudio];
239228
}
@@ -247,12 +236,12 @@ - (BOOL) isPlaying
247236

248237
- (IBAction) start: (id)sender
249238
{
250-
[self setPaused: NO];
239+
[self startVideo];
251240
}
252241

253242
- (IBAction) stop: (id)sender
254243
{
255-
[self setPaused: YES];
244+
[self stopVideo];
256245
}
257246

258247
- (void) setMuted: (BOOL)muted
@@ -317,33 +306,14 @@ - (IBAction)gotoBeginning: (id)sender
317306

318307
- (IBAction)gotoEnd: (id)sender
319308
{
320-
//
321309
}
322310

323311
- (IBAction) stepForward: (id)sender
324312
{
325-
int64_t seconds = 2;
326-
int64_t skip = av_rescale_q(seconds, (AVRational){1,1}, _stream->time_base);
327-
int64_t newPts = _lastPts + skip;
328-
329-
if (_paused == NO)
330-
{
331-
_paused = YES;
332-
av_seek_frame(_formatCtx, _videoStreamIndex, newPts, AVSEEK_FLAG_BACKWARD);
333-
avcodec_flush_buffers(_videoCodecCtx);
334-
_paused = NO;
335-
}
336-
else
337-
{
338-
av_seek_frame(_formatCtx, _videoStreamIndex, newPts, AVSEEK_FLAG_BACKWARD);
339-
avcodec_flush_buffers(_videoCodecCtx);
340-
}
341313
}
342314

343315
- (IBAction) stepBack: (id)sender
344316
{
345-
[self stop: sender];
346-
// [self _startAtPts: _savedPts - 1000];
347317
}
348318

349319
// Video playback methods...
@@ -454,16 +424,10 @@ - (void) loop
454424

455425
while (av_read_frame(_formatCtx, &packet) >= 0)
456426
{
457-
if (packet.pts <= _savedPts)
458-
{
459-
continue;
460-
}
461-
462-
// After 1000 frames, start the thread...
463-
if (i == 1000)
427+
// After BUFFER_SIZE frames, start the thread...
428+
if (i == BUFFER_SIZE)
464429
{
465430
[self startVideo];
466-
[_audioPlayer startAudio];
467431
}
468432

469433
if (packet.stream_index == _videoStreamIndex)
@@ -481,11 +445,10 @@ - (void) loop
481445
}
482446

483447
// if we had a very short video... play it.
484-
if (i < 1000)
448+
if (i < BUFFER_SIZE)
485449
{
486450
NSLog(@"[GSMovieView] Starting short video... | Timestamp: %ld", av_gettime());
487451
[self startVideo];
488-
[_audioPlayer startAudio];
489452
}
490453
}
491454
}
@@ -565,6 +528,7 @@ - (void) startVideo
565528
selector:@selector(videoThreadEntry)
566529
object:nil];
567530
[_videoThread start];
531+
[_audioPlayer startAudio];
568532
}
569533
}
570534

@@ -577,16 +541,14 @@ - (void) stopVideo
577541
_running = NO;
578542

579543
[_videoThread cancel];
544+
[_audioPlayer stopAudio];
580545
while (![_videoThread isFinished])
581546
{
582547
usleep(1000);
583548
}
584549

585550
DESTROY(_videoThread);
586551
avformat_close_input(&_formatCtx);
587-
588-
_savedPts = _lastPts;
589-
_lastPts = 0;
590552
}
591553
}
592554

@@ -598,12 +560,6 @@ - (void) setPlaying: (BOOL)f
598560
- (void)submitPacket: (AVPacket *)packet
599561
{
600562
NSDictionary *dict = NSDictionaryFromAVPacket(packet);
601-
602-
if (_paused)
603-
{
604-
NSLog(@"Submitted packet...");
605-
}
606-
607563
@synchronized (_videoPackets)
608564
{
609565
[_videoPackets addObject: dict];
@@ -614,13 +570,6 @@ - (void)videoThreadEntry
614570
{
615571
while (_running)
616572
{
617-
// Stop reading packets while paused...
618-
if (_paused == YES)
619-
{
620-
usleep(10000); // 10ms pause
621-
continue; // start the loop again...
622-
}
623-
624573
// Create pool...
625574
CREATE_AUTORELEASE_POOL(pool);
626575
{
@@ -652,38 +601,31 @@ - (void)videoThreadEntry
652601

653602
// If the current pts is at or after the saved,
654603
// then alow it to be decoded and displayed...
655-
if (packet.pts >= _savedPts)
656-
{
657-
int64_t packetTime = av_rescale_q(packet.pts,
658-
_timeBase,
659-
(AVRational){1, 1000000});
660-
int64_t now = av_gettime() - _videoClock;
661-
int64_t delay = packetTime - now;
662-
663-
if (_statusField != nil)
664-
{
665-
// Show the information...
666-
_statusString = [NSString stringWithFormat: @"Rendering video frame PTS: %ld | Delay: %ld us | %@",
667-
packet.pts, delay, _running ? @"Running" : @"Stopped"];
668-
[_statusField setStringValue: _statusString];
669-
}
604+
int64_t packetTime = av_rescale_q(packet.pts,
605+
_timeBase,
606+
(AVRational){1, 1000000});
607+
int64_t now = av_gettime() - _videoClock;
608+
int64_t delay = packetTime - now;
670609

671-
// Show status on the command line...
672-
fprintf(stderr, "[GSMovieView] Rendering video frame PTS: %ld | Delay: %ld us\r",
673-
packet.pts, delay);
674-
if (delay > 0)
675-
{
676-
usleep((useconds_t)delay);
677-
}
678-
679-
// Decode the packet, display it and play the sound...
680-
[self decodePacket: &packet];
681-
RELEASE(dict);
610+
if (_statusField != nil)
611+
{
612+
// Show the information...
613+
_statusString = [NSString stringWithFormat: @"Rendering video frame PTS: %ld | Delay: %ld us | %@",
614+
packet.pts, delay, _running ? @"Running" : @"Stopped"];
615+
[_statusField setStringValue: _statusString];
682616
}
683-
else
617+
618+
// Show status on the command line...
619+
fprintf(stderr, "[GSMovieView] Rendering video frame PTS: %ld | Delay: %ld us\r",
620+
packet.pts, delay);
621+
if (delay > 0)
684622
{
685-
usleep(1000);
623+
usleep((useconds_t)delay);
686624
}
625+
626+
// Decode the packet, display it and play the sound...
627+
[self decodePacket: &packet];
628+
RELEASE(dict);
687629
}
688630
}
689631
RELEASE(pool);

0 commit comments

Comments
 (0)