47
47
#import " GSAudioPlayer.h"
48
48
#import " GSAVUtils.h"
49
49
50
+ #define BUFFER_SIZE 2048
51
+
50
52
static NSNotificationCenter *nc = nil ;
51
53
52
54
// Ignore the warning this will produce as it is intentional.
@@ -143,12 +145,13 @@ - (instancetype) initWithFrame: (NSRect)frame
143
145
// Flags...
144
146
_running = NO ;
145
147
_started = NO ;
146
- _paused = NO ;
147
-
148
+
149
+ /*
148
150
[nc addObserver: self
149
151
selector: @selector(handleNotification:)
150
152
name: NSApplicationWillTerminateNotification
151
153
object: nil];
154
+ */
152
155
}
153
156
return self;
154
157
}
@@ -191,18 +194,6 @@ - (void)dealloc
191
194
[super dealloc ];
192
195
}
193
196
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
-
206
197
// Notification responses...
207
198
- (void ) handleNotification : (NSNotification *)notification
208
199
{
@@ -222,7 +213,6 @@ - (void) _startFeed
222
213
[self setRate: 1.0 / 30.0 ];
223
214
[self setVolume: 1.0 ];
224
215
225
- _savedPts = 0 ;
226
216
_feedThread = [[NSThread alloc ] initWithTarget: self selector: @selector (feedVideo ) object: nil ];
227
217
[_feedThread start ];
228
218
[_audioPlayer startAudio ];
@@ -233,7 +223,6 @@ - (void) _stopFeed
233
223
{
234
224
if (_running == YES )
235
225
{
236
- _savedPts = _lastPts;
237
226
[_feedThread cancel ];
238
227
[_audioPlayer stopAudio ];
239
228
}
@@ -247,12 +236,12 @@ - (BOOL) isPlaying
247
236
248
237
- (IBAction ) start : (id )sender
249
238
{
250
- [self setPaused: NO ];
239
+ [self startVideo ];
251
240
}
252
241
253
242
- (IBAction ) stop : (id )sender
254
243
{
255
- [self setPaused: YES ];
244
+ [self stopVideo ];
256
245
}
257
246
258
247
- (void ) setMuted : (BOOL )muted
@@ -317,33 +306,14 @@ - (IBAction)gotoBeginning: (id)sender
317
306
318
307
- (IBAction )gotoEnd : (id )sender
319
308
{
320
- //
321
309
}
322
310
323
311
- (IBAction ) stepForward : (id )sender
324
312
{
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
- }
341
313
}
342
314
343
315
- (IBAction ) stepBack : (id )sender
344
316
{
345
- [self stop: sender];
346
- // [self _startAtPts: _savedPts - 1000];
347
317
}
348
318
349
319
// Video playback methods...
@@ -454,16 +424,10 @@ - (void) loop
454
424
455
425
while (av_read_frame (_formatCtx, &packet) >= 0 )
456
426
{
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)
464
429
{
465
430
[self startVideo ];
466
- [_audioPlayer startAudio ];
467
431
}
468
432
469
433
if (packet.stream_index == _videoStreamIndex)
@@ -481,11 +445,10 @@ - (void) loop
481
445
}
482
446
483
447
// if we had a very short video... play it.
484
- if (i < 1000 )
448
+ if (i < BUFFER_SIZE )
485
449
{
486
450
NSLog (@" [GSMovieView] Starting short video... | Timestamp: %ld " , av_gettime ());
487
451
[self startVideo ];
488
- [_audioPlayer startAudio ];
489
452
}
490
453
}
491
454
}
@@ -565,6 +528,7 @@ - (void) startVideo
565
528
selector: @selector (videoThreadEntry )
566
529
object: nil ];
567
530
[_videoThread start ];
531
+ [_audioPlayer startAudio ];
568
532
}
569
533
}
570
534
@@ -577,16 +541,14 @@ - (void) stopVideo
577
541
_running = NO ;
578
542
579
543
[_videoThread cancel ];
544
+ [_audioPlayer stopAudio ];
580
545
while (![_videoThread isFinished ])
581
546
{
582
547
usleep (1000 );
583
548
}
584
549
585
550
DESTROY (_videoThread);
586
551
avformat_close_input (&_formatCtx);
587
-
588
- _savedPts = _lastPts;
589
- _lastPts = 0 ;
590
552
}
591
553
}
592
554
@@ -598,12 +560,6 @@ - (void) setPlaying: (BOOL)f
598
560
- (void )submitPacket : (AVPacket *)packet
599
561
{
600
562
NSDictionary *dict = NSDictionaryFromAVPacket(packet);
601
-
602
- if (_paused)
603
- {
604
- NSLog (@" Submitted packet..." );
605
- }
606
-
607
563
@synchronized (_videoPackets)
608
564
{
609
565
[_videoPackets addObject: dict];
@@ -614,13 +570,6 @@ - (void)videoThreadEntry
614
570
{
615
571
while (_running)
616
572
{
617
- // Stop reading packets while paused...
618
- if (_paused == YES )
619
- {
620
- usleep (10000 ); // 10ms pause
621
- continue ; // start the loop again...
622
- }
623
-
624
573
// Create pool...
625
574
CREATE_AUTORELEASE_POOL (pool);
626
575
{
@@ -652,38 +601,31 @@ - (void)videoThreadEntry
652
601
653
602
// If the current pts is at or after the saved,
654
603
// 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;
670
609
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];
682
616
}
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 )
684
622
{
685
- usleep (1000 );
623
+ usleep (( useconds_t )delay );
686
624
}
625
+
626
+ // Decode the packet, display it and play the sound...
627
+ [self decodePacket: &packet];
628
+ RELEASE (dict);
687
629
}
688
630
}
689
631
RELEASE (pool);
0 commit comments