GPUImageAVCamera.m 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. #import "GPUImageAVCamera.h"
  2. #import "GPUImageMovieWriter.h"
  3. #import "GPUImageFilter.h"
  4. NSString *const kGPUImageYUVVideoRangeConversionForRGFragmentShaderString = SHADER_STRING
  5. (
  6. varying vec2 textureCoordinate;
  7. uniform sampler2D luminanceTexture;
  8. uniform sampler2D chrominanceTexture;
  9. void main()
  10. {
  11. vec3 yuv;
  12. vec3 rgb;
  13. yuv.x = texture2D(luminanceTexture, textureCoordinate).r;
  14. yuv.yz = texture2D(chrominanceTexture, textureCoordinate).rg - vec2(0.5, 0.5);
  15. // BT.601, which is the standard for SDTV is provided as a reference
  16. /*
  17. rgb = mat3( 1, 1, 1,
  18. 0, -.39465, 2.03211,
  19. 1.13983, -.58060, 0) * yuv;
  20. */
  21. // Using BT.709 which is the standard for HDTV
  22. rgb = mat3( 1, 1, 1,
  23. 0, -.21482, 2.12798,
  24. 1.28033, -.38059, 0) * yuv;
  25. gl_FragColor = vec4(rgb, 1);
  26. }
  27. );
  28. NSString *const kGPUImageYUVVideoRangeConversionForLAFragmentShaderString = SHADER_STRING
  29. (
  30. varying vec2 textureCoordinate;
  31. uniform sampler2D luminanceTexture;
  32. uniform sampler2D chrominanceTexture;
  33. void main()
  34. {
  35. vec3 yuv;
  36. vec3 rgb;
  37. yuv.x = texture2D(luminanceTexture, textureCoordinate).r;
  38. yuv.yz = texture2D(chrominanceTexture, textureCoordinate).ra - vec2(0.5, 0.5);
  39. // BT.601, which is the standard for SDTV is provided as a reference
  40. /*
  41. rgb = mat3( 1, 1, 1,
  42. 0, -.39465, 2.03211,
  43. 1.13983, -.58060, 0) * yuv;
  44. */
  45. // Using BT.709 which is the standard for HDTV
  46. rgb = mat3( 1, 1, 1,
  47. 0, -.21482, 2.12798,
  48. 1.28033, -.38059, 0) * yuv;
  49. gl_FragColor = vec4(rgb, 1);
  50. }
  51. );
  52. #pragma mark -
  53. #pragma mark Private methods and instance variables
  54. @interface GPUImageAVCamera ()
  55. {
  56. AVCaptureDeviceInput *audioInput;
  57. AVCaptureAudioDataOutput *audioOutput;
  58. NSDate *startingCaptureTime;
  59. NSInteger _frameRate;
  60. dispatch_queue_t cameraProcessingQueue, audioProcessingQueue;
  61. GLProgram *yuvConversionProgram;
  62. GLint yuvConversionPositionAttribute, yuvConversionTextureCoordinateAttribute;
  63. GLint yuvConversionLuminanceTextureUniform, yuvConversionChrominanceTextureUniform;
  64. int imageBufferWidth, imageBufferHeight;
  65. }
  66. - (void)updateOrientationSendToTargets;
  67. - (void)convertYUVToRGBOutput;
  68. @end
  69. @implementation GPUImageAVCamera
  70. @synthesize captureSessionPreset = _captureSessionPreset;
  71. @synthesize captureSession = _captureSession;
  72. @synthesize inputCamera = _inputCamera;
  73. @synthesize runBenchmark = _runBenchmark;
  74. @synthesize delegate = _delegate;
  75. @synthesize horizontallyMirrorFrontFacingCamera = _horizontallyMirrorFrontFacingCamera, horizontallyMirrorRearFacingCamera = _horizontallyMirrorRearFacingCamera;
  76. #pragma mark -
  77. #pragma mark Initialization and teardown
  78. + (NSArray *)connectedCameraDevices;
  79. {
  80. NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
  81. return devices;
  82. }
  83. - (id)init;
  84. {
  85. if (!(self = [self initWithSessionPreset:AVCaptureSessionPreset640x480 cameraDevice:nil]))
  86. {
  87. return nil;
  88. }
  89. return self;
  90. }
  91. - (id)initWithDeviceUniqueID:(NSString *)deviceUniqueID;
  92. {
  93. if (!(self = [self initWithSessionPreset:AVCaptureSessionPreset640x480 deviceUniqueID:deviceUniqueID]))
  94. {
  95. return nil;
  96. }
  97. return self;
  98. }
  99. - (id)initWithSessionPreset:(NSString *)sessionPreset deviceUniqueID:(NSString *)deviceUniqueID;
  100. {
  101. if (!(self = [self initWithSessionPreset:sessionPreset cameraDevice:[AVCaptureDevice deviceWithUniqueID:deviceUniqueID]]))
  102. {
  103. return nil;
  104. }
  105. return self;
  106. }
  107. - (id)initWithSessionPreset:(NSString *)sessionPreset cameraDevice:(AVCaptureDevice *)cameraDevice;
  108. {
  109. if (!(self = [super init]))
  110. {
  111. return nil;
  112. }
  113. cameraProcessingQueue = dispatch_queue_create("com.sunsetlakesoftware.GPUImage.cameraProcessingQueue", NULL);
  114. audioProcessingQueue = dispatch_queue_create("com.sunsetlakesoftware.GPUImage.audioProcessingQueue", NULL);
  115. frameRenderingSemaphore = dispatch_semaphore_create(1);
  116. _frameRate = 0; // This will not set frame rate unless this value gets set to 1 or above
  117. _runBenchmark = NO;
  118. capturePaused = NO;
  119. outputRotation = kGPUImageNoRotation;
  120. // captureAsYUV = YES;
  121. captureAsYUV = NO;
  122. runSynchronouslyOnVideoProcessingQueue(^{
  123. if (captureAsYUV)
  124. {
  125. [GPUImageContext useImageProcessingContext];
  126. // if ([GPUImageContext deviceSupportsRedTextures])
  127. // {
  128. // yuvConversionProgram = [[GPUImageContext sharedImageProcessingContext] programForVertexShaderString:kGPUImageVertexShaderString fragmentShaderString:kGPUImageYUVVideoRangeConversionForRGFragmentShaderString];
  129. // }
  130. // else
  131. // {
  132. yuvConversionProgram = [[GPUImageContext sharedImageProcessingContext] programForVertexShaderString:kGPUImageVertexShaderString fragmentShaderString:kGPUImageYUVVideoRangeConversionForLAFragmentShaderString];
  133. // }
  134. if (!yuvConversionProgram.initialized)
  135. {
  136. [yuvConversionProgram addAttribute:@"position"];
  137. [yuvConversionProgram addAttribute:@"inputTextureCoordinate"];
  138. if (![yuvConversionProgram link])
  139. {
  140. NSString *progLog = [yuvConversionProgram programLog];
  141. NSLog(@"Program link log: %@", progLog);
  142. NSString *fragLog = [yuvConversionProgram fragmentShaderLog];
  143. NSLog(@"Fragment shader compile log: %@", fragLog);
  144. NSString *vertLog = [yuvConversionProgram vertexShaderLog];
  145. NSLog(@"Vertex shader compile log: %@", vertLog);
  146. yuvConversionProgram = nil;
  147. NSAssert(NO, @"Filter shader link failed");
  148. }
  149. }
  150. yuvConversionPositionAttribute = [yuvConversionProgram attributeIndex:@"position"];
  151. yuvConversionTextureCoordinateAttribute = [yuvConversionProgram attributeIndex:@"inputTextureCoordinate"];
  152. yuvConversionLuminanceTextureUniform = [yuvConversionProgram uniformIndex:@"luminanceTexture"];
  153. yuvConversionChrominanceTextureUniform = [yuvConversionProgram uniformIndex:@"chrominanceTexture"];
  154. [GPUImageContext setActiveShaderProgram:yuvConversionProgram];
  155. glEnableVertexAttribArray(yuvConversionPositionAttribute);
  156. glEnableVertexAttribArray(yuvConversionTextureCoordinateAttribute);
  157. }
  158. });
  159. // Grab the back-facing or front-facing camera
  160. _inputCamera = nil;
  161. if (cameraDevice == nil)
  162. {
  163. _inputCamera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  164. }
  165. else
  166. {
  167. _inputCamera = cameraDevice;
  168. }
  169. if (!_inputCamera) {
  170. return nil;
  171. }
  172. // Create the capture session
  173. _captureSession = [[AVCaptureSession alloc] init];
  174. [_captureSession beginConfiguration];
  175. // Add the video input
  176. NSError *error = nil;
  177. videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:_inputCamera error:&error];
  178. if ([_captureSession canAddInput:videoInput])
  179. {
  180. [_captureSession addInput:videoInput];
  181. }
  182. // Add the video frame output
  183. videoOutput = [[AVCaptureVideoDataOutput alloc] init];
  184. [videoOutput setAlwaysDiscardsLateVideoFrames:NO];
  185. // NSLog(@"Camera: %@", _inputCamera);
  186. // [self printSupportedPixelFormats];
  187. // if (captureAsYUV && [GPUImageContext deviceSupportsRedTextures])
  188. if (captureAsYUV && [GPUImageContext supportsFastTextureUpload])
  189. {
  190. BOOL supportsFullYUVRange = NO;
  191. NSArray *supportedPixelFormats = videoOutput.availableVideoCVPixelFormatTypes;
  192. for (NSNumber *currentPixelFormat in supportedPixelFormats)
  193. {
  194. if ([currentPixelFormat intValue] == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)
  195. {
  196. supportsFullYUVRange = YES;
  197. }
  198. }
  199. if (supportsFullYUVRange)
  200. {
  201. [videoOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
  202. }
  203. else
  204. {
  205. [videoOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
  206. }
  207. }
  208. else
  209. {
  210. // Despite returning a longer list of supported pixel formats, only RGB, RGBA, BGRA, and the YUV 4:2:2 variants seem to return cleanly
  211. [videoOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
  212. // [videoOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_422YpCbCr8_yuvs] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
  213. }
  214. [videoOutput setSampleBufferDelegate:self queue:cameraProcessingQueue];
  215. // [videoOutput setSampleBufferDelegate:self queue:[GPUImageContext sharedContextQueue]];
  216. if ([_captureSession canAddOutput:videoOutput])
  217. {
  218. [_captureSession addOutput:videoOutput];
  219. }
  220. else
  221. {
  222. NSLog(@"Couldn't add video output");
  223. return nil;
  224. }
  225. _captureSessionPreset = sessionPreset;
  226. [_captureSession setSessionPreset:_captureSessionPreset];
  227. // This will let you get 60 FPS video from the 720p preset on an iPhone 4S, but only that device and that preset
  228. // AVCaptureConnection *conn = [videoOutput connectionWithMediaType:AVMediaTypeVideo];
  229. //
  230. // if (conn.supportsVideoMinFrameDuration)
  231. // conn.videoMinFrameDuration = CMTimeMake(1,60);
  232. // if (conn.supportsVideoMaxFrameDuration)
  233. // conn.videoMaxFrameDuration = CMTimeMake(1,60);
  234. [_captureSession commitConfiguration];
  235. return self;
  236. }
  237. - (void)dealloc
  238. {
  239. [self stopCameraCapture];
  240. [videoOutput setSampleBufferDelegate:nil queue:dispatch_get_main_queue()];
  241. [audioOutput setSampleBufferDelegate:nil queue:dispatch_get_main_queue()];
  242. [self removeInputsAndOutputs];
  243. // ARC forbids explicit message send of 'release'; since iOS 6 even for dispatch_release() calls: stripping it out in that case is required.
  244. #if ( (__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0) || (!defined(__IPHONE_6_0)) )
  245. if (cameraProcessingQueue != NULL)
  246. {
  247. dispatch_release(cameraProcessingQueue);
  248. }
  249. if (audioProcessingQueue != NULL)
  250. {
  251. dispatch_release(audioProcessingQueue);
  252. }
  253. if (frameRenderingSemaphore != NULL)
  254. {
  255. dispatch_release(frameRenderingSemaphore);
  256. }
  257. #endif
  258. }
  259. - (void)removeInputsAndOutputs;
  260. {
  261. [_captureSession removeInput:videoInput];
  262. [_captureSession removeOutput:videoOutput];
  263. if (_microphone != nil)
  264. {
  265. [_captureSession removeInput:audioInput];
  266. [_captureSession removeOutput:audioOutput];
  267. }
  268. }
  269. #pragma mark -
  270. #pragma mark Managing targets
  271. - (void)addTarget:(id<GPUImageInput>)newTarget atTextureLocation:(NSInteger)textureLocation;
  272. {
  273. [super addTarget:newTarget atTextureLocation:textureLocation];
  274. [newTarget setInputRotation:outputRotation atIndex:textureLocation];
  275. }
  276. #pragma mark -
  277. #pragma mark Manage the camera video stream
  278. - (void)startCameraCapture;
  279. {
  280. if (![_captureSession isRunning])
  281. {
  282. startingCaptureTime = [NSDate date];
  283. [_captureSession startRunning];
  284. };
  285. }
  286. - (void)stopCameraCapture;
  287. {
  288. if ([_captureSession isRunning])
  289. {
  290. [_captureSession stopRunning];
  291. }
  292. }
  293. - (void)pauseCameraCapture;
  294. {
  295. capturePaused = YES;
  296. }
  297. - (void)resumeCameraCapture;
  298. {
  299. capturePaused = NO;
  300. }
  301. - (void)rotateCamera
  302. {
  303. if (self.frontFacingCameraPresent == NO)
  304. return;
  305. NSError *error;
  306. AVCaptureDeviceInput *newVideoInput;
  307. AVCaptureDevicePosition currentCameraPosition = [[videoInput device] position];
  308. if (currentCameraPosition == AVCaptureDevicePositionBack)
  309. {
  310. currentCameraPosition = AVCaptureDevicePositionFront;
  311. }
  312. else
  313. {
  314. currentCameraPosition = AVCaptureDevicePositionBack;
  315. }
  316. AVCaptureDevice *backFacingCamera = nil;
  317. NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
  318. for (AVCaptureDevice *device in devices)
  319. {
  320. if ([device position] == currentCameraPosition)
  321. {
  322. backFacingCamera = device;
  323. }
  324. }
  325. newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:backFacingCamera error:&error];
  326. if (newVideoInput != nil)
  327. {
  328. [_captureSession beginConfiguration];
  329. [_captureSession removeInput:videoInput];
  330. if ([_captureSession canAddInput:newVideoInput])
  331. {
  332. [_captureSession addInput:newVideoInput];
  333. videoInput = newVideoInput;
  334. }
  335. else
  336. {
  337. [_captureSession addInput:videoInput];
  338. }
  339. //captureSession.sessionPreset = oriPreset;
  340. [_captureSession commitConfiguration];
  341. }
  342. _inputCamera = backFacingCamera;
  343. }
  344. - (AVCaptureDevicePosition)cameraPosition
  345. {
  346. return [[videoInput device] position];
  347. }
  348. - (BOOL)isFrontFacingCameraPresent;
  349. {
  350. NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
  351. for (AVCaptureDevice *device in devices)
  352. {
  353. if ([device position] == AVCaptureDevicePositionFront)
  354. return YES;
  355. }
  356. return NO;
  357. }
  358. - (void)setCaptureSessionPreset:(NSString *)captureSessionPreset;
  359. {
  360. [_captureSession beginConfiguration];
  361. _captureSessionPreset = captureSessionPreset;
  362. [_captureSession setSessionPreset:_captureSessionPreset];
  363. [_captureSession commitConfiguration];
  364. }
  365. - (void)setFrameRate:(NSInteger)frameRate;
  366. {
  367. _frameRate = frameRate;
  368. if (_frameRate > 0)
  369. {
  370. for (AVCaptureConnection *connection in videoOutput.connections)
  371. {
  372. if ([connection respondsToSelector:@selector(setVideoMinFrameDuration:)])
  373. connection.videoMinFrameDuration = CMTimeMake(1, (int32_t)_frameRate);
  374. }
  375. }
  376. else
  377. {
  378. for (AVCaptureConnection *connection in videoOutput.connections)
  379. {
  380. if ([connection respondsToSelector:@selector(setVideoMinFrameDuration:)])
  381. connection.videoMinFrameDuration = kCMTimeInvalid; // This sets videoMinFrameDuration back to default
  382. }
  383. }
  384. }
  385. - (NSInteger)frameRate;
  386. {
  387. return _frameRate;
  388. }
  389. - (AVCaptureConnection *)videoCaptureConnection {
  390. for (AVCaptureConnection *connection in [videoOutput connections] ) {
  391. for ( AVCaptureInputPort *port in [connection inputPorts] ) {
  392. if ( [[port mediaType] isEqual:AVMediaTypeVideo] ) {
  393. return connection;
  394. }
  395. }
  396. }
  397. return nil;
  398. }
  399. #define INITIALFRAMESTOIGNOREFORBENCHMARK 5
  400. - (void)updateTargetsForVideoCameraUsingCacheTextureAtWidth:(int)bufferWidth height:(int)bufferHeight time:(CMTime)currentTime;
  401. {
  402. // First, update all the framebuffers in the targets
  403. for (id<GPUImageInput> currentTarget in targets)
  404. {
  405. if ([currentTarget enabled])
  406. {
  407. NSInteger indexOfObject = [targets indexOfObject:currentTarget];
  408. NSInteger textureIndexOfTarget = [[targetTextureIndices objectAtIndex:indexOfObject] integerValue];
  409. if (currentTarget != self.targetToIgnoreForUpdates)
  410. {
  411. [currentTarget setInputRotation:outputRotation atIndex:textureIndexOfTarget];
  412. [currentTarget setInputSize:CGSizeMake(bufferWidth, bufferHeight) atIndex:textureIndexOfTarget];
  413. if ([currentTarget wantsMonochromeInput] && captureAsYUV)
  414. {
  415. [currentTarget setCurrentlyReceivingMonochromeInput:YES];
  416. // TODO: Replace optimization for monochrome output
  417. [currentTarget setInputFramebuffer:outputFramebuffer atIndex:textureIndexOfTarget];
  418. }
  419. else
  420. {
  421. [currentTarget setCurrentlyReceivingMonochromeInput:NO];
  422. [currentTarget setInputFramebuffer:outputFramebuffer atIndex:textureIndexOfTarget];
  423. }
  424. }
  425. else
  426. {
  427. [currentTarget setInputRotation:outputRotation atIndex:textureIndexOfTarget];
  428. [currentTarget setInputFramebuffer:outputFramebuffer atIndex:textureIndexOfTarget];
  429. }
  430. }
  431. }
  432. // Then release our hold on the local framebuffer to send it back to the cache as soon as it's no longer needed
  433. [outputFramebuffer unlock];
  434. // Finally, trigger rendering as needed
  435. for (id<GPUImageInput> currentTarget in targets)
  436. {
  437. if ([currentTarget enabled])
  438. {
  439. NSInteger indexOfObject = [targets indexOfObject:currentTarget];
  440. NSInteger textureIndexOfTarget = [[targetTextureIndices objectAtIndex:indexOfObject] integerValue];
  441. if (currentTarget != self.targetToIgnoreForUpdates)
  442. {
  443. [currentTarget newFrameReadyAtTime:currentTime atIndex:textureIndexOfTarget];
  444. }
  445. }
  446. }
  447. }
  448. - (void)processVideoSampleBuffer:(CMSampleBufferRef)sampleBuffer;
  449. {
  450. if (capturePaused)
  451. {
  452. return;
  453. }
  454. CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
  455. CVImageBufferRef cameraFrame = CMSampleBufferGetImageBuffer(sampleBuffer);
  456. GLsizei bufferWidth = (GLsizei)CVPixelBufferGetWidth(cameraFrame);
  457. GLsizei bufferHeight = (GLsizei)CVPixelBufferGetHeight(cameraFrame);
  458. CMTime currentTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
  459. [GPUImageContext useImageProcessingContext];
  460. CVPixelBufferLockBaseAddress(cameraFrame, 0);
  461. outputFramebuffer = [[GPUImageContext sharedFramebufferCache] fetchFramebufferForSize:CGSizeMake(bufferWidth, bufferHeight) onlyTexture:YES];
  462. glBindTexture(GL_TEXTURE_2D, [outputFramebuffer texture]);
  463. // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bufferWidth, bufferHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, CVPixelBufferGetBaseAddress(cameraFrame));
  464. // Using BGRA extension to pull in video frame data directly
  465. // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bytesPerRow / 3, bufferHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, CVPixelBufferGetBaseAddress(cameraFrame));
  466. // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bufferWidth, bufferHeight, 0, GL_YCBCR_422_APPLE, GL_UNSIGNED_SHORT_8_8_REV_APPLE, CVPixelBufferGetBaseAddress(cameraFrame));
  467. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bufferWidth, bufferHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, CVPixelBufferGetBaseAddress(cameraFrame));
  468. [self updateTargetsForVideoCameraUsingCacheTextureAtWidth:bufferWidth height:bufferHeight time:currentTime];
  469. // for (id<GPUImageInput> currentTarget in targets)
  470. // {
  471. // if ([currentTarget enabled])
  472. // {
  473. // if (currentTarget != self.targetToIgnoreForUpdates)
  474. // {
  475. // NSInteger indexOfObject = [targets indexOfObject:currentTarget];
  476. // NSInteger textureIndexOfTarget = [[targetTextureIndices objectAtIndex:indexOfObject] integerValue];
  477. //
  478. // [currentTarget setInputSize:CGSizeMake(bufferWidth, bufferHeight) atIndex:textureIndexOfTarget];
  479. // [currentTarget newFrameReadyAtTime:currentTime atIndex:textureIndexOfTarget];
  480. // }
  481. // }
  482. // }
  483. CVPixelBufferUnlockBaseAddress(cameraFrame, 0);
  484. if (_runBenchmark)
  485. {
  486. numberOfFramesCaptured++;
  487. if (numberOfFramesCaptured > INITIALFRAMESTOIGNOREFORBENCHMARK)
  488. {
  489. CFAbsoluteTime currentFrameTime = (CFAbsoluteTimeGetCurrent() - startTime);
  490. totalFrameTimeDuringCapture += currentFrameTime;
  491. NSLog(@"Average frame time : %f ms", [self averageFrameDurationDuringCapture]);
  492. NSLog(@"Current frame time : %f ms", 1000.0 * currentFrameTime);
  493. }
  494. }
  495. }
  496. - (void)processAudioSampleBuffer:(CMSampleBufferRef)sampleBuffer;
  497. {
  498. [self.audioEncodingTarget processAudioBuffer:sampleBuffer];
  499. }
  500. - (void)convertYUVToRGBOutput;
  501. {
  502. [GPUImageContext setActiveShaderProgram:yuvConversionProgram];
  503. outputFramebuffer = [[GPUImageContext sharedFramebufferCache] fetchFramebufferForSize:CGSizeMake(imageBufferWidth, imageBufferHeight) textureOptions:self.outputTextureOptions onlyTexture:NO];
  504. [outputFramebuffer activateFramebuffer];
  505. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  506. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  507. static const GLfloat squareVertices[] = {
  508. -1.0f, -1.0f,
  509. 1.0f, -1.0f,
  510. -1.0f, 1.0f,
  511. 1.0f, 1.0f,
  512. };
  513. static const GLfloat textureCoordinates[] = {
  514. 0.0f, 0.0f,
  515. 1.0f, 0.0f,
  516. 0.0f, 1.0f,
  517. 1.0f, 1.0f,
  518. };
  519. glActiveTexture(GL_TEXTURE4);
  520. glBindTexture(GL_TEXTURE_2D, luminanceTexture);
  521. glUniform1i(yuvConversionLuminanceTextureUniform, 4);
  522. glActiveTexture(GL_TEXTURE5);
  523. glBindTexture(GL_TEXTURE_2D, chrominanceTexture);
  524. glUniform1i(yuvConversionChrominanceTextureUniform, 5);
  525. glVertexAttribPointer(yuvConversionPositionAttribute, 2, GL_FLOAT, 0, 0, squareVertices);
  526. glVertexAttribPointer(yuvConversionTextureCoordinateAttribute, 2, GL_FLOAT, 0, 0, textureCoordinates);
  527. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  528. }
  529. #pragma mark -
  530. #pragma mark Benchmarking
  531. - (CGFloat)averageFrameDurationDuringCapture;
  532. {
  533. return (totalFrameTimeDuringCapture / (CGFloat)(numberOfFramesCaptured - INITIALFRAMESTOIGNOREFORBENCHMARK)) * 1000.0;
  534. }
  535. #pragma mark -
  536. #pragma mark AVCaptureVideoDataOutputSampleBufferDelegate
  537. - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
  538. {
  539. if (captureOutput == audioOutput)
  540. {
  541. // if (dispatch_semaphore_wait(frameRenderingSemaphore, DISPATCH_TIME_NOW) != 0)
  542. // {
  543. // return;
  544. // }
  545. CFRetain(sampleBuffer);
  546. runAsynchronouslyOnVideoProcessingQueue(^{
  547. [self processAudioSampleBuffer:sampleBuffer];
  548. CFRelease(sampleBuffer);
  549. // dispatch_semaphore_signal(frameRenderingSemaphore);
  550. });
  551. }
  552. else
  553. {
  554. if (dispatch_semaphore_wait(frameRenderingSemaphore, DISPATCH_TIME_NOW) != 0)
  555. {
  556. return;
  557. }
  558. CFRetain(sampleBuffer);
  559. runAsynchronouslyOnVideoProcessingQueue(^{
  560. //Feature Detection Hook.
  561. if (self.delegate && [self.delegate respondsToSelector:@selector(willOutputSampleBuffer:)])
  562. {
  563. [self.delegate willOutputSampleBuffer:sampleBuffer];
  564. }
  565. [self processVideoSampleBuffer:sampleBuffer];
  566. CFRelease(sampleBuffer);
  567. dispatch_semaphore_signal(frameRenderingSemaphore);
  568. });
  569. }
  570. }
  571. #pragma mark -
  572. #pragma mark Accessors
  573. - (void)setAudioEncodingTarget:(GPUImageMovieWriter *)newValue;
  574. {
  575. runSynchronouslyOnVideoProcessingQueue(^{
  576. [_captureSession beginConfiguration];
  577. if (newValue == nil)
  578. {
  579. if (audioOutput)
  580. {
  581. [_captureSession removeInput:audioInput];
  582. [_captureSession removeOutput:audioOutput];
  583. audioInput = nil;
  584. audioOutput = nil;
  585. _microphone = nil;
  586. }
  587. }
  588. else
  589. {
  590. _microphone = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
  591. audioInput = [AVCaptureDeviceInput deviceInputWithDevice:_microphone error:nil];
  592. if ([_captureSession canAddInput:audioInput])
  593. {
  594. [_captureSession addInput:audioInput];
  595. }
  596. audioOutput = [[AVCaptureAudioDataOutput alloc] init];
  597. if ([_captureSession canAddOutput:audioOutput])
  598. {
  599. [_captureSession addOutput:audioOutput];
  600. }
  601. else
  602. {
  603. NSLog(@"Couldn't add audio output");
  604. }
  605. [audioOutput setSampleBufferDelegate:self queue:audioProcessingQueue];
  606. }
  607. [_captureSession commitConfiguration];
  608. [super setAudioEncodingTarget:newValue];
  609. });
  610. }
  611. - (void)updateOrientationSendToTargets;
  612. {
  613. runSynchronouslyOnVideoProcessingQueue(^{
  614. // From the iOS 5.0 release notes:
  615. // In previous iOS versions, the front-facing camera would always deliver buffers in AVCaptureVideoOrientationLandscapeLeft and the back-facing camera would always deliver buffers in AVCaptureVideoOrientationLandscapeRight.
  616. outputRotation = kGPUImageNoRotation;
  617. for (id<GPUImageInput> currentTarget in targets)
  618. {
  619. NSInteger indexOfObject = [targets indexOfObject:currentTarget];
  620. [currentTarget setInputRotation:outputRotation atIndex:[[targetTextureIndices objectAtIndex:indexOfObject] integerValue]];
  621. }
  622. });
  623. }
  624. - (void)setHorizontallyMirrorFrontFacingCamera:(BOOL)newValue
  625. {
  626. _horizontallyMirrorFrontFacingCamera = newValue;
  627. [self updateOrientationSendToTargets];
  628. }
  629. - (void)setHorizontallyMirrorRearFacingCamera:(BOOL)newValue
  630. {
  631. _horizontallyMirrorRearFacingCamera = newValue;
  632. [self updateOrientationSendToTargets];
  633. }
  634. - (void)printSupportedPixelFormats;
  635. {
  636. NSArray *supportedPixelFormats = videoOutput.availableVideoCVPixelFormatTypes;
  637. for (NSNumber *currentPixelFormat in supportedPixelFormats)
  638. {
  639. NSString *pixelFormatName = nil;
  640. switch([currentPixelFormat intValue])
  641. {
  642. case kCVPixelFormatType_1Monochrome: pixelFormatName = @"kCVPixelFormatType_1Monochrome"; break;
  643. case kCVPixelFormatType_2Indexed: pixelFormatName = @"kCVPixelFormatType_2Indexed"; break;
  644. case kCVPixelFormatType_4Indexed: pixelFormatName = @"kCVPixelFormatType_4Indexed"; break;
  645. case kCVPixelFormatType_8Indexed: pixelFormatName = @"kCVPixelFormatType_8Indexed"; break;
  646. case kCVPixelFormatType_1IndexedGray_WhiteIsZero: pixelFormatName = @"kCVPixelFormatType_1IndexedGray_WhiteIsZero"; break;
  647. case kCVPixelFormatType_2IndexedGray_WhiteIsZero: pixelFormatName = @"kCVPixelFormatType_2IndexedGray_WhiteIsZero"; break;
  648. case kCVPixelFormatType_4IndexedGray_WhiteIsZero: pixelFormatName = @"kCVPixelFormatType_4IndexedGray_WhiteIsZero"; break;
  649. case kCVPixelFormatType_8IndexedGray_WhiteIsZero: pixelFormatName = @"kCVPixelFormatType_8IndexedGray_WhiteIsZero"; break;
  650. case kCVPixelFormatType_16BE555: pixelFormatName = @"kCVPixelFormatType_16BE555"; break;
  651. case kCVPixelFormatType_16LE555: pixelFormatName = @"kCVPixelFormatType_16LE555"; break;
  652. case kCVPixelFormatType_16LE5551: pixelFormatName = @"kCVPixelFormatType_16LE5551"; break;
  653. case kCVPixelFormatType_16BE565: pixelFormatName = @"kCVPixelFormatType_16BE565"; break;
  654. case kCVPixelFormatType_16LE565: pixelFormatName = @"kCVPixelFormatType_16LE565"; break;
  655. case kCVPixelFormatType_24RGB: pixelFormatName = @"kCVPixelFormatType_24RGB"; break;
  656. case kCVPixelFormatType_24BGR: pixelFormatName = @"kCVPixelFormatType_24BGR"; break;
  657. case kCVPixelFormatType_32ARGB: pixelFormatName = @"kCVPixelFormatType_32ARGB"; break;
  658. case kCVPixelFormatType_32BGRA: pixelFormatName = @"kCVPixelFormatType_32BGRA"; break;
  659. case kCVPixelFormatType_32ABGR: pixelFormatName = @"kCVPixelFormatType_32ABGR"; break;
  660. case kCVPixelFormatType_32RGBA: pixelFormatName = @"kCVPixelFormatType_32RGBA"; break;
  661. case kCVPixelFormatType_64ARGB: pixelFormatName = @"kCVPixelFormatType_64ARGB"; break;
  662. case kCVPixelFormatType_48RGB: pixelFormatName = @"kCVPixelFormatType_48RGB"; break;
  663. case kCVPixelFormatType_32AlphaGray: pixelFormatName = @"kCVPixelFormatType_32AlphaGray"; break;
  664. case kCVPixelFormatType_16Gray: pixelFormatName = @"kCVPixelFormatType_16Gray"; break;
  665. case kCVPixelFormatType_30RGB: pixelFormatName = @"kCVPixelFormatType_30RGB"; break;
  666. case kCVPixelFormatType_422YpCbCr8: pixelFormatName = @"kCVPixelFormatType_422YpCbCr8"; break;
  667. case kCVPixelFormatType_4444YpCbCrA8: pixelFormatName = @"kCVPixelFormatType_4444YpCbCrA8"; break;
  668. case kCVPixelFormatType_4444YpCbCrA8R: pixelFormatName = @"kCVPixelFormatType_4444YpCbCrA8R"; break;
  669. case kCVPixelFormatType_4444AYpCbCr8: pixelFormatName = @"kCVPixelFormatType_4444AYpCbCr8"; break;
  670. case kCVPixelFormatType_4444AYpCbCr16: pixelFormatName = @"kCVPixelFormatType_4444AYpCbCr16"; break;
  671. case kCVPixelFormatType_444YpCbCr8: pixelFormatName = @"kCVPixelFormatType_444YpCbCr8"; break;
  672. case kCVPixelFormatType_422YpCbCr16: pixelFormatName = @"kCVPixelFormatType_422YpCbCr16"; break;
  673. case kCVPixelFormatType_422YpCbCr10: pixelFormatName = @"kCVPixelFormatType_422YpCbCr10"; break;
  674. case kCVPixelFormatType_444YpCbCr10: pixelFormatName = @"kCVPixelFormatType_444YpCbCr10"; break;
  675. case kCVPixelFormatType_420YpCbCr8Planar: pixelFormatName = @"kCVPixelFormatType_420YpCbCr8Planar"; break;
  676. case kCVPixelFormatType_420YpCbCr8PlanarFullRange: pixelFormatName = @"kCVPixelFormatType_420YpCbCr8PlanarFullRange"; break;
  677. case kCVPixelFormatType_422YpCbCr_4A_8BiPlanar: pixelFormatName = @"kCVPixelFormatType_422YpCbCr_4A_8BiPlanar"; break;
  678. case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange: pixelFormatName = @"kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange"; break;
  679. case kCVPixelFormatType_420YpCbCr8BiPlanarFullRange: pixelFormatName = @"kCVPixelFormatType_420YpCbCr8BiPlanarFullRange"; break;
  680. case kCVPixelFormatType_422YpCbCr8_yuvs: pixelFormatName = @"kCVPixelFormatType_422YpCbCr8_yuvs"; break;
  681. case kCVPixelFormatType_422YpCbCr8FullRange: pixelFormatName = @"kCVPixelFormatType_422YpCbCr8FullRange"; break;
  682. case kCVPixelFormatType_OneComponent8: pixelFormatName = @"kCVPixelFormatType_OneComponent8"; break;
  683. case kCVPixelFormatType_TwoComponent8: pixelFormatName = @"kCVPixelFormatType_TwoComponent8"; break;
  684. }
  685. NSLog(@"Supported pixel format: %@", pixelFormatName);
  686. }
  687. }
  688. @end