GPUImage icon indicating copy to clipboard operation
GPUImage copied to clipboard

Changing/initializing filters after calling [GPUImageMovie startProcessing]

Open mattwiechec opened this issue 13 years ago • 7 comments

Since upgrading to the latest version of GPUImage today I'm having issues changing filters on the fly.

If I have already called [startProcessing] I cannot alloc/init a new filter (ie. filter = [[GPUImageSepiaFilter alloc] init]) or call [removeAllTargets]/[addTarget] when a loaded video is running. The changes only take effect after the video loops.

The current workaround is to call [endProcessing], make the changes, then call [startProcessing] again. The downside to this is that the video restarts from the beginning.

My previous version of GPUImage did not have this issue. Can anyone shed some light on this? What has changed?

Edit: Interestingly this is not an issue in the supplied example project SimpleVideoFileFilter, but when duplicating the code to a new project the issue shows up.

Edit 2: Perhaps SimpleVideoFileFilter is using an old static lib? Modifying FilterShowcase to run a video and trying to change filters on the fly produces the issue mentioned above.

mattwiechec avatar Sep 12 '13 00:09 mattwiechec

This also happened to me yesterday.

In addition to what is described above:

  1. Parameter changes are not happening in real-time during playback.
  2. Removing targets / [filter alloc] blocks the app (no touch events registered etc) until playback is finished.

Cheers!

kbola avatar Sep 12 '13 16:09 kbola

@kbola I'm facing the same issue if you manage to solve this please post here.

balabs avatar Sep 13 '13 08:09 balabs

Same issue here. Main thread is being blocked somewhere, but I could not find it using Instruments.

plenilune avatar Sep 20 '13 05:09 plenilune

My solution for blocked main thread and GUI was this: in file GPUIUmageMovie.m find the method

  • (void)processAsset

there is if condition

if (synchronizedMovieWriter != nil)
{

// HERE GOES CODE IF - TRUE } else { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

// HERE GOES ALL CODE FROM IF - FALSE });

}

Those are some of my changes that worked for me.

Here is the whole method from my code:

  • (void)processAsset { reader = [self createAssetReader];

    AVAssetReaderOutput *readerVideoTrackOutput = nil; AVAssetReaderOutput *readerAudioTrackOutput = nil;

    audioEncodingIsFinished = YES; for( AVAssetReaderOutput *output in reader.outputs ) { if( [output.mediaType isEqualToString:AVMediaTypeAudio] ) { audioEncodingIsFinished = NO; readerAudioTrackOutput = output; } else if( [output.mediaType isEqualToString:AVMediaTypeVideo] ) { readerVideoTrackOutput = output; } }

    if ([reader startReading] == NO) { NSLog(@"Error reading from file at URL: %@", self.url); return; }

    __unsafe_unretained GPUImageMovie *weakSelf = self;

    if (synchronizedMovieWriter != nil) { [synchronizedMovieWriter setVideoInputReadyCallback:^{ return [weakSelf readNextVideoFrameFromOutput:readerVideoTrackOutput]; }];

    [synchronizedMovieWriter setAudioInputReadyCallback:^{
        return [weakSelf readNextAudioSampleFromOutput:readerAudioTrackOutput];
    }];
    
    [synchronizedMovieWriter enableSynchronizationCallbacks];
    

    } else { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

        while (reader.status == AVAssetReaderStatusReading && (!_shouldRepeat || keepLooping))
        {
            [weakSelf readNextVideoFrameFromOutput:readerVideoTrackOutput];
    
            if ( (readerAudioTrackOutput) && (!audioEncodingIsFinished) )
            {
                [weakSelf readNextAudioSampleFromOutput:readerAudioTrackOutput];
            }
    
        }
    
        if (reader.status == AVAssetWriterStatusCompleted) {
    
            [reader cancelReading];
    
            if (keepLooping) {
                reader = nil;
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self startProcessing];
                });
            } else {
                [weakSelf endProcessing];
            }
    
        }
    });
    

    } }

balabs avatar Sep 20 '13 07:09 balabs

balabs, thanks a lot

kirill-c avatar Jan 11 '14 12:01 kirill-c

For anyone still relying on this hack, I found it will make the video playback at inconsistent rates on iOS 8. Generally slow and stuttered.

The problem it patched seems to be resolved in the latest version of GPUImage. Removing the dispatch_async wrapper in the else statement fixed the video playback for me.

mikemuzza avatar Aug 20 '14 00:08 mikemuzza

Hmm, maybe try changing "DISPATCH_QUEUE_PRIORITY_BACKGROUND" to "DISPATCH_QUEUE_PRIORITY_HIGH" or "DISPATCH_QUEUE_PRIORITY_LOW"? Background priority is not really meant for anything except IO "Items dispatched to the queue run at background priority; the queue is scheduled for execution after all high priority queues have been scheduled and the system runs items on a thread whose priority is set for background status. Such a thread has the lowest priority and any disk I/O is throttled to minimize the impact on the system" https://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html#//apple_ref/c/macro/DISPATCH_QUEUE_PRIORITY_BACKGROUND

seosgithub avatar Aug 28 '14 14:08 seosgithub