2014 snapchat source code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.6 KiB

  1. //
  2. // SCManagedVideoFrameSampler.m
  3. // Snapchat
  4. //
  5. // Created by Michel Loenngren on 3/10/17.
  6. // Copyright © 2017 Snapchat, Inc. All rights reserved.
  7. //
  8. #import "SCManagedVideoFrameSampler.h"
  9. #import <SCFoundation/SCThreadHelpers.h>
  10. #import <SCFoundation/UIImage+CVPixelBufferRef.h>
  11. @import CoreImage;
  12. @import ImageIO;
  13. @interface SCManagedVideoFrameSampler ()
  14. @property (nonatomic, copy) void (^frameSampleBlock)(UIImage *, CMTime);
  15. @property (nonatomic, strong) CIContext *ciContext;
  16. @end
  17. @implementation SCManagedVideoFrameSampler
  18. - (void)sampleNextFrame:(void (^)(UIImage *, CMTime))completeBlock
  19. {
  20. _frameSampleBlock = completeBlock;
  21. }
  22. #pragma mark - SCManagedCapturerListener
  23. - (void)managedCapturer:(id<SCCapturer>)managedCapturer
  24. didAppendVideoSampleBuffer:(CMSampleBufferRef)sampleBuffer
  25. sampleMetadata:(SCManagedCapturerSampleMetadata *)sampleMetadata
  26. {
  27. void (^block)(UIImage *, CMTime) = _frameSampleBlock;
  28. _frameSampleBlock = nil;
  29. if (!block) {
  30. return;
  31. }
  32. CVImageBufferRef cvImageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
  33. CMTime presentationTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
  34. UIImage *image;
  35. if (cvImageBuffer) {
  36. CGImageRef cgImage = SCCreateCGImageFromPixelBufferRef(cvImageBuffer);
  37. image = [[UIImage alloc] initWithCGImage:cgImage scale:1.0 orientation:UIImageOrientationRight];
  38. CGImageRelease(cgImage);
  39. }
  40. runOnMainThreadAsynchronously(^{
  41. block(image, presentationTime);
  42. });
  43. }
  44. - (CIContext *)ciContext
  45. {
  46. if (!_ciContext) {
  47. _ciContext = [CIContext context];
  48. }
  49. return _ciContext;
  50. }
  51. @end