Skip to content

Commit

Permalink
Merge pull request #2084 from fisch0920/master
Browse files Browse the repository at this point in the history
Fix GPUImageMovieWriter isPaused
  • Loading branch information
BradLarson committed Dec 3, 2015
2 parents 52ee212 + 303f204 commit 92aaa71
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 4 deletions.
3 changes: 3 additions & 0 deletions framework/Source/GPUImageVideoCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ void setColorConversion709( GLfloat conversionMatrix[9] );
__unsafe_unretained id<GPUImageVideoCameraDelegate> _delegate;
}

/// Whether or not the underlying AVCaptureSession is running
@property(readonly, nonatomic) BOOL isRunning;

/// The AVCaptureSession used to capture from the camera
@property(readonly, retain, nonatomic) AVCaptureSession *captureSession;

Expand Down
5 changes: 5 additions & 0 deletions framework/Source/GPUImageVideoCamera.m
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@ - (void)addTarget:(id<GPUImageInput>)newTarget atTextureLocation:(NSInteger)text
#pragma mark -
#pragma mark Manage the camera video stream

- (BOOL)isRunning;
{
return [_captureSession isRunning];
}

- (void)startCameraCapture;
{
if (![_captureSession isRunning])
Expand Down
88 changes: 85 additions & 3 deletions framework/Source/iOS/GPUImageMovieWriter.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ @interface GPUImageMovieWriter ()

GPUImageFramebuffer *firstInputFramebuffer;

BOOL discont;
CMTime startTime, previousFrameTime, previousAudioTime;

CMTime offsetTime;

dispatch_queue_t audioQueue, videoQueue;
BOOL audioEncodingIsFinished, videoEncodingIsFinished;

Expand Down Expand Up @@ -85,6 +87,7 @@ - (id)initWithMovieURL:(NSURL *)newMovieURL size:(CGSize)newSize fileType:(NSStr
videoEncodingIsFinished = NO;
audioEncodingIsFinished = NO;

discont = NO;
videoSize = newSize;
movieURL = newMovieURL;
fileType = newFileType;
Expand Down Expand Up @@ -362,7 +365,7 @@ - (void)finishRecordingWithCompletionHandler:(void (^)(void))handler;

- (void)processAudioBuffer:(CMSampleBufferRef)audioBuffer;
{
if (!isRecording)
if (!isRecording || _paused)
{
return;
}
Expand Down Expand Up @@ -396,6 +399,34 @@ - (void)processAudioBuffer:(CMSampleBufferRef)audioBuffer;
CFRelease(audioBuffer);
return;
}

if (discont) {
discont = NO;

CMTime current;
if (offsetTime.value > 0) {
current = CMTimeSubtract(currentSampleTime, offsetTime);
} else {
current = currentSampleTime;
}

CMTime offset = CMTimeSubtract(current, previousAudioTime);

if (offsetTime.value == 0) {
offsetTime = offset;
} else {
offsetTime = CMTimeAdd(offsetTime, offset);
}
}

if (offsetTime.value > 0) {
CFRelease(audioBuffer);
audioBuffer = [self adjustTime:audioBuffer by:offsetTime];
CFRetain(audioBuffer);
}

// record most recent time so we know the length of the pause
currentSampleTime = CMSampleBufferGetPresentationTimeStamp(audioBuffer);

previousAudioTime = currentSampleTime;

Expand Down Expand Up @@ -666,12 +697,35 @@ - (void)renderAtInternalSizeUsingFramebuffer:(GPUImageFramebuffer *)inputFramebu

- (void)newFrameReadyAtTime:(CMTime)frameTime atIndex:(NSInteger)textureIndex;
{
if (!isRecording)
if (!isRecording || _paused)
{
[firstInputFramebuffer unlock];
return;
}

if (discont) {
discont = NO;
CMTime current;

if (offsetTime.value > 0) {
current = CMTimeSubtract(frameTime, offsetTime);
} else {
current = frameTime;
}

CMTime offset = CMTimeSubtract(current, previousFrameTime);

if (offsetTime.value == 0) {
offsetTime = offset;
} else {
offsetTime = CMTimeAdd(offsetTime, offset);
}
}

if (offsetTime.value > 0) {
frameTime = CMTimeSubtract(frameTime, offsetTime);
}

// Drop frames forced by images and other things with no time constants
// Also, if two consecutive times with the same value are added to the movie, it aborts recording, so I bail on that case
if ( (CMTIME_IS_INVALID(frameTime)) || (CMTIME_COMPARE_INLINE(frameTime, ==, previousFrameTime)) || (CMTIME_IS_INDEFINITE(frameTime)) )
Expand Down Expand Up @@ -931,4 +985,32 @@ - (AVAssetWriter*)assetWriter {
return assetWriter;
}

- (void)setPaused:(BOOL)newValue {
if (_paused != newValue) {
_paused = newValue;

if (_paused) {
discont = YES;
}
}
}

- (CMSampleBufferRef)adjustTime:(CMSampleBufferRef) sample by:(CMTime) offset {
CMItemCount count;
CMSampleBufferGetSampleTimingInfoArray(sample, 0, nil, &count);
CMSampleTimingInfo* pInfo = malloc(sizeof(CMSampleTimingInfo) * count);
CMSampleBufferGetSampleTimingInfoArray(sample, count, pInfo, &count);

for (CMItemCount i = 0; i < count; i++) {
pInfo[i].decodeTimeStamp = CMTimeSubtract(pInfo[i].decodeTimeStamp, offset);
pInfo[i].presentationTimeStamp = CMTimeSubtract(pInfo[i].presentationTimeStamp, offset);
}

CMSampleBufferRef sout;
CMSampleBufferCreateCopyWithNewTiming(nil, sample, count, pInfo, &sout);
free(pInfo);

return sout;
}

@end
5 changes: 4 additions & 1 deletion framework/Source/iOS/GPUImageView.m
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ - (void)layoutSubviews {
runSynchronouslyOnVideoProcessingQueue(^{
[self destroyDisplayFramebuffer];
[self createDisplayFramebuffer];
[self recalculateViewGeometry];
});
} else if (!CGSizeEqualToSize(self.bounds.size, CGSizeZero)) {
[self recalculateViewGeometry];
}
}

Expand Down Expand Up @@ -187,6 +188,8 @@ - (void)createDisplayFramebuffer;
__unused GLuint framebufferCreationStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
NSAssert(framebufferCreationStatus == GL_FRAMEBUFFER_COMPLETE, @"Failure with display framebuffer generation for display of size: %f, %f", self.bounds.size.width, self.bounds.size.height);
boundsSizeAtFrameBufferEpoch = self.bounds.size;

[self recalculateViewGeometry];
}

- (void)destroyDisplayFramebuffer;
Expand Down

0 comments on commit 92aaa71

Please sign in to comment.