Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Use synchronized
Browse files Browse the repository at this point in the history
  • Loading branch information
cbenhagen authored Nov 12, 2021
1 parent ff13ad0 commit d5feb3a
Showing 1 changed file with 40 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,10 @@ - (void)notifyIfFrameAvailable {
} else {
CVPixelBufferRef pixelBuffer = [_videoOutput copyPixelBufferForItemTime:outputItemTime itemTimeForDisplay:NULL];
if (pixelBuffer != NULL) {
CVBufferRelease(_lastValidFrame);
_lastValidFrame = pixelBuffer;
@synchronized (self) {
CVBufferRelease(_lastValidFrame);
_lastValidFrame = pixelBuffer;
}
}
[_frameUpdater notifyFrameAvailable];
}
Expand All @@ -185,9 +187,7 @@ static CVReturn OnDisplayLink(CVDisplayLinkRef CV_NONNULL displayLink,
CVOptionFlags* CV_NONNULL flagsOut,
void* CV_NULLABLE displayLinkContext) {
__weak FLTVideoPlayer* video_player = (__bridge FLTVideoPlayer*)(displayLinkContext);
dispatch_async(dispatch_get_main_queue(), ^{
[video_player notifyIfFrameAvailable];
});
[video_player notifyIfFrameAvailable];
return kCVReturnSuccess;
}

Expand Down Expand Up @@ -403,9 +403,7 @@ - (void)seekTo:(int)location {
[_player seekToTime:CMTimeMake(location, 1000)
toleranceBefore:kCMTimeZero
toleranceAfter:kCMTimeZero];
dispatch_async(dispatch_get_main_queue(), ^{
[self notifyIfFrameAvailable];
});
[self notifyIfFrameAvailable];
}

- (void)setIsLooping:(bool)isLooping {
Expand Down Expand Up @@ -445,28 +443,41 @@ - (CVPixelBufferRef)copyPixelBuffer {
//
// Unlike on iOS, the macOS embedder does show the last frame when
// we return NULL from `copyPixelBuffer`.
CVPixelBufferLockBaseAddress(_lastValidFrame, kCVPixelBufferLock_ReadOnly);
int bufferWidth = (int)CVPixelBufferGetWidth(_lastValidFrame);
int bufferHeight = (int)CVPixelBufferGetHeight(_lastValidFrame);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(_lastValidFrame);
uint8_t *baseAddress = CVPixelBufferGetBaseAddress(_lastValidFrame);
NSDictionary* pixBuffAttributes = @{
(id)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA),
(id)kCVPixelBufferIOSurfacePropertiesKey : @{},
(id)kCVPixelBufferOpenGLCompatibilityKey : @YES,
(id)kCVPixelBufferMetalCompatibilityKey : @YES,
};

CVPixelBufferRef pixelBufferCopy = NULL;
CVPixelBufferCreate(kCFAllocatorDefault, bufferWidth, bufferHeight,kCVPixelFormatType_32BGRA,
CFBridgingRetain(pixBuffAttributes), &pixelBufferCopy);
CVPixelBufferLockBaseAddress(pixelBufferCopy, 0);
uint8_t *copyBaseAddress = CVPixelBufferGetBaseAddress(pixelBufferCopy);
memcpy(copyBaseAddress, baseAddress, bufferHeight * bytesPerRow);

CVPixelBufferUnlockBaseAddress(_lastValidFrame, kCVPixelBufferLock_ReadOnly);
CVPixelBufferUnlockBaseAddress(pixelBufferCopy, 0);
return pixelBufferCopy;
if (_lastValidFrame == NULL) {
return NULL;
}

@synchronized (self) {
CVPixelBufferLockBaseAddress(_lastValidFrame, kCVPixelBufferLock_ReadOnly);
int bufferWidth = (int)CVPixelBufferGetWidth(_lastValidFrame);
int bufferHeight = (int)CVPixelBufferGetHeight(_lastValidFrame);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(_lastValidFrame);
uint8_t *baseAddress = CVPixelBufferGetBaseAddress(_lastValidFrame);

if (baseAddress == NULL) {
NSLog(@"----> baseadress is NULL");
return NULL;
}

NSDictionary* pixBuffAttributes = @{
(id)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA),
(id)kCVPixelBufferIOSurfacePropertiesKey : @{},
(id)kCVPixelBufferOpenGLCompatibilityKey : @YES,
(id)kCVPixelBufferMetalCompatibilityKey : @YES,
};

CVPixelBufferRef pixelBufferCopy = NULL;
CVPixelBufferCreate(kCFAllocatorDefault, bufferWidth, bufferHeight,kCVPixelFormatType_32BGRA,
CFBridgingRetain(pixBuffAttributes), &pixelBufferCopy);
CVPixelBufferLockBaseAddress(pixelBufferCopy, 0);
uint8_t *copyBaseAddress = CVPixelBufferGetBaseAddress(pixelBufferCopy);
memcpy(copyBaseAddress, baseAddress, bufferHeight * bytesPerRow);

CVPixelBufferUnlockBaseAddress(_lastValidFrame, kCVPixelBufferLock_ReadOnly);
CVPixelBufferUnlockBaseAddress(pixelBufferCopy, 0);
return pixelBufferCopy;
}
}

- (void)onTextureUnregistered:(NSObject<FlutterTexture>*)texture {
Expand Down

0 comments on commit d5feb3a

Please sign in to comment.