KSYAVFCapture.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // KSYAVFCapture.h
  3. // KSYStreamer
  4. //
  5. // Created by yiqian on 1/30/16.
  6. // Copyright © 2016 ksyun. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import <AVFoundation/AVFoundation.h>
  10. #import <CoreMedia/CoreMedia.h>
  11. #import <UIKit/UIKit.h>
  12. /**
  13. 基于 AVFoundation的 音视频采集模块
  14. * 通过回调将采集的音频和视频数据传出
  15. * 将摄像头和音频的常用操作进行封装
  16. * 注意: 使用AVFoundation的音频采集时,可能无法进行后台采集
  17. */
  18. @interface KSYAVFCapture : NSObject <AVCaptureVideoDataOutputSampleBufferDelegate, AVCaptureAudioDataOutputSampleBufferDelegate>
  19. {
  20. AVCaptureSession *_captureSession;
  21. AVCaptureDevice *_inputCamera;
  22. AVCaptureDevice *_microphone;
  23. AVCaptureDeviceInput *videoInput;
  24. AVCaptureVideoDataOutput *videoOutput;
  25. }
  26. /// Whether or not the underlying AVCaptureSession is running
  27. @property(readonly, nonatomic) BOOL isRunning;
  28. /// The AVCaptureSession used to capture from the camera
  29. @property(readonly, retain, nonatomic) AVCaptureSession *captureSession;
  30. /// This enables the capture session preset to be changed on the fly
  31. @property (readwrite, nonatomic, copy) NSString *captureSessionPreset;
  32. /** 采集帧率 有效范围为 0~30 */
  33. @property (readwrite) int32_t frameRate;
  34. /** 输出图像的像素格式 有效值 为 NV12 和BGRA
  35. - kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange
  36. - kCVPixelFormatType_420YpCbCr8BiPlanarFullRange
  37. - kCVPixelFormatType_32BGRA
  38. */
  39. @property (readwrite) OSType outputPixelFmt;
  40. /// Easy way to tell which cameras are present on device
  41. @property (readonly, getter = isFrontFacingCameraPresent) BOOL frontFacingCameraPresent;
  42. /// Easy way to tell which cameras are present on device
  43. @property (readonly, getter = isBackFacingCameraPresent) BOOL backFacingCameraPresent;
  44. /// Use this property to manage camera settings. Focus point, exposure point, etc.
  45. @property(readonly) AVCaptureDevice *inputCamera;
  46. /// This determines the rotation applied to the output image, based on the source material
  47. @property(readwrite, nonatomic) UIInterfaceOrientation outputImageOrientation;
  48. /// These properties determine whether or not the two camera orientations should be mirrored. By default, (YES, NO).
  49. @property(readwrite, nonatomic) BOOL bMirrorFrontCamera, bMirrorRearCamera;
  50. /// 后置摄像头是否存在
  51. + (BOOL)isBackFacingCameraPresent;
  52. /// 前置摄像头是否存在
  53. + (BOOL)isFrontFacingCameraPresent;
  54. /** UIInterfaceOrientation 转为 AVCaptureVideoOrientation
  55. @param orien UI 的朝向,比如状态栏相对Home键的位置
  56. */
  57. + (AVCaptureVideoOrientation) uiOrientationToAVOrientation: (UIInterfaceOrientation) orien;
  58. /// @name Initialization and teardown
  59. /** Begin a capture session
  60. See AVCaptureSession for acceptable values
  61. @param sessionPreset Session preset to use
  62. @param cameraPosition Camera to capture from
  63. */
  64. - (id)initWithSessionPreset:(NSString *)sessionPreset
  65. cameraPosition:(AVCaptureDevicePosition)cameraPosition;
  66. /** Add audio capture to the session. Adding inputs and outputs freezes the capture session momentarily, so you
  67. can use this method to add the audio inputs and outputs early, if you're going to set the audioEncodingTarget
  68. later. Returns YES is the audio inputs and outputs were added, or NO if they had already been added.
  69. */
  70. - (BOOL)addAudioInputsAndOutputs;
  71. /** Remove the audio capture inputs and outputs from this session. Returns YES if the audio inputs and outputs
  72. were removed, or NO is they hadn't already been added.
  73. */
  74. - (BOOL)removeAudioInputsAndOutputs;
  75. /** Tear down the capture session
  76. */
  77. - (void)removeInputsAndOutputs;
  78. /// @name Manage the camera video stream
  79. /** Start camera capturing
  80. */
  81. - (void)startCameraCapture;
  82. /** Stop camera capturing
  83. */
  84. - (void)stopCameraCapture;
  85. /** Pause camera capturing
  86. */
  87. - (void)pauseCameraCapture;
  88. /** Resume camera capturing
  89. */
  90. - (void)resumeCameraCapture;
  91. /** Get the position (front, rear) of the source camera
  92. */
  93. - (AVCaptureDevicePosition)cameraPosition;
  94. /** Get the AVCaptureConnection of the source camera
  95. */
  96. - (AVCaptureConnection *)videoCaptureConnection;
  97. /** This flips between the front and rear cameras
  98. */
  99. - (void)rotateCamera;
  100. /**
  101. @abstract 查询实际的采集分辨率
  102. @discussion 参见iOS的 AVCaptureSessionPresetXXX的定义
  103. */
  104. - (CGSize) captureDimension;
  105. #pragma mark - Torch
  106. /**
  107. @abstract 当前采集设备是否支持闪光灯
  108. @return YES / NO
  109. @discussion 通常只有后置摄像头支持闪光灯
  110. */
  111. - (BOOL) isTorchSupported;
  112. /**
  113. @abstract 开关闪光灯
  114. @discussion 切换闪光灯的开关状态 开 <--> 关
  115. */
  116. - (void) toggleTorch;
  117. /**
  118. @abstract 设置闪光灯
  119. @param mode AVCaptureTorchModeOn/Off
  120. @discussion 设置闪光灯的开关状态
  121. @discussion 开始预览后开始有效
  122. @discussion 请参考 AVCaptureTorchMode
  123. */
  124. - (void) setTorchMode: (AVCaptureTorchMode)mode;
  125. #pragma mark - raw data
  126. /**
  127. @abstract 音频处理回调接口
  128. @discussion sampleBuffer 原始采集到的音频数据
  129. @discussion 请注意本函数的执行时间,如果太长可能导致不可预知的问题@discussion 请参考 CMSampleBufferRef
  130. @discussion 请参考 CMSampleBufferRef
  131. */
  132. @property(nonatomic, copy) void(^audioProcessingCallback)(CMSampleBufferRef sampleBuffer);
  133. /**
  134. @abstract 视频处理回调接口
  135. @discussion sampleBuffer 原始采集到的视频数据
  136. @discussion 请注意本函数的执行时间,如果太长可能导致不可预知的问题
  137. @discussion 请参考 CMSampleBufferRef
  138. */
  139. @property(nonatomic, copy) void(^videoProcessingCallback)(CMSampleBufferRef sampleBuffer);
  140. /**
  141. @abstract 采集被打断的消息通知
  142. @discussion bInterrupt 为YES, 表明被打断, 采集暂停
  143. @discussion bInterrupt 为NO, 表明恢复采集
  144. */
  145. @property(nonatomic, copy) void(^interruptCallback)(BOOL bInterrupt);
  146. @end