CGDisplayStream.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /* CoreGraphics - CGDisplayStream.h
  2. Copyright (c) 2011-2013 Apple Inc.
  3. All rights reserved. */
  4. #ifndef CGDISPLAYSTREAM_H_
  5. #define CGDISPLAYSTREAM_H_
  6. #include <CoreFoundation/CoreFoundation.h>
  7. #include <CoreFoundation/CFAvailability.h>
  8. #include <stdint.h>
  9. #include <dispatch/dispatch.h>
  10. #include <CoreGraphics/CGDirectDisplay.h>
  11. CF_IMPLICIT_BRIDGING_ENABLED
  12. CF_ASSUME_NONNULL_BEGIN
  13. #ifdef __BLOCKS__
  14. /*!
  15. @typedef CGDisplayStreamRef
  16. @abstract An opaque reference to a CGDisplayStream object
  17. @discussion A CGDisplayStream provides a streaming API for capturing display updates in a realtime manner. It can also provide
  18. scaling and color space conversion services, as well as allow capturing sub regions of the display. Callbacks can be targetted
  19. at either a traditional CFRunLoop, or at a dispatch queue.
  20. */
  21. typedef struct CF_BRIDGED_TYPE(id) CGDisplayStream *CGDisplayStreamRef;
  22. /*!
  23. @typedef CGDisplayStreamUpdateRef
  24. @abstract An opaque reference to a single frame's extra metadata that describes useful frame delta information
  25. @discussion A CGDisplayStreamUpdate encapsulates information about what portions of a frame have changed relative to
  26. a previously delivered frame. This includes regions that were changed in any way, which ones were actually redrawn, and which
  27. regions were merely copied from one place to another. A routine is provided to merge two update refs together in cases
  28. where apps need to coalesce the values because they decided to skip processing for one or more frames.
  29. */
  30. typedef const struct CF_BRIDGED_TYPE(id)
  31. CGDisplayStreamUpdate *CGDisplayStreamUpdateRef;
  32. /*!
  33. @enum CGDisplayStreamUpdateRectType
  34. @abstract Used to select which array of rectangles to be returned by CGDisplayUpdateGetRects
  35. @const kCGDisplayStreamUpdateRefreshedRects The rectangles that were refreshed on the display, not counting moved rectangles
  36. @const kCGDisplayStreamUpdateMovedRects The rectangles that were simply moved from one part of the display to another
  37. @const kCGDisplayStreamUpdateDirtyRects The union of both refreshed and moved rects
  38. @const kCGDisplayStreamUpdateReducedDirtyRects A possibly simplified (but overstated) array of dirty rectangles
  39. */
  40. typedef CF_ENUM(int32_t, CGDisplayStreamUpdateRectType) {
  41. kCGDisplayStreamUpdateRefreshedRects,
  42. kCGDisplayStreamUpdateMovedRects,
  43. kCGDisplayStreamUpdateDirtyRects,
  44. kCGDisplayStreamUpdateReducedDirtyRects,
  45. };
  46. /*!
  47. @enum CGDisplayStreamFrameStatus
  48. @abstract Provides information about incoming frame updates
  49. @const kCGDisplayStreamFrameStatusFrameComplete A new frame has been generated by the Window Server for a particular display at time displayTime
  50. @const kCGDisplayStreamFrameStatusFrameIdle The Window Server did not generate a new frame for displayTime
  51. @const kCGDisplayStreamFrameStatusFrameBlank As of displayTime, the display has gone blank
  52. @const kCGDisplayStreamFrameStatusStopped The display stream has stopped and no more calls will be made to the handler until the stream is started.
  53. */
  54. typedef CF_ENUM(int32_t, CGDisplayStreamFrameStatus) {
  55. kCGDisplayStreamFrameStatusFrameComplete,
  56. kCGDisplayStreamFrameStatusFrameIdle,
  57. kCGDisplayStreamFrameStatusFrameBlank,
  58. kCGDisplayStreamFrameStatusStopped,
  59. };
  60. /*
  61. @callback CGDisplayStreamFrameAvailableHandler
  62. @abstract The block prototype used for new frame delivery by CGDisplayStream objects
  63. @discussion For each frame that is generated by the WindowServer for a particular display, the user provided block is invoked and provides the user with an IOSurfaceRef
  64. that contains the pixel data for the new frame, as well as a CGDisplayStreamUpdateRef that contains all of the metadata associated with that IOSurface.
  65. @param frameSurface The IOSurfaceRef for the current frame. May be NULL in some cases. If you intend to hold on to the IOSurface beyond the lifetime of
  66. the handler call, you must CFRetain() the IOSurface until you are done with it *and* you must call IOSurfaceIncrementUseCount() to let the CGDisplayStream know
  67. that the frame is not ready for re-use. Once you are finished using the IOSurfaceRef you must then call IOSurfaceDecrementUseCount(). If you are maintaing
  68. any kind of external cache of information about the IOSurface (such as a GL texture object), you must keep a CFRetain() on the IOSurface to prevent it from going
  69. away until you remove it from your cache. You can not depend on the set of IOSurfaces being used by the display stream as being static, so you should implement
  70. some kind of age-out behavior for your cache for IOSurfaces that have not been re-used in a while.
  71. @param displayTime The mach_absolute_time() of when the corresponding frame was to be displayed by the WindowServer
  72. @param updateRef The CGDisplayStreamUpdateRef for the current frame. Note: If you want to keep the CGDisplayStreamUpdateRef around beyond the lifetime
  73. of the handler, you must CFRetain() it, as it will be CFRelease()'d by the CGDisplayStream after the handler returns. The updateRef will be NULL in cases
  74. when status is not kCGDisplayStreamFrameStatusFrameComplete.
  75. */
  76. typedef void (^CGDisplayStreamFrameAvailableHandler)(
  77. CGDisplayStreamFrameStatus status, uint64_t displayTime,
  78. IOSurfaceRef __nullable frameSurface,
  79. CGDisplayStreamUpdateRef __nullable updateRef);
  80. /*!
  81. @function CGDisplayStreamUpdateGetTypeID
  82. @abstract Returns the CF "class" ID for CGDisplayStreamUpdate
  83. @result The CFTypeID of the CGDisplayStreamUpdate class.
  84. */
  85. CG_EXTERN CFTypeID
  86. CGDisplayStreamUpdateGetTypeID(void) CG_AVAILABLE_BUT_DEPRECATED(
  87. 11.0, 14.0,
  88. "There is no direct replacement for this function. Please use ScreenCaptureKit API's SCStreamOutputType instead");
  89. /*!
  90. @function CGDisplayStreamUpdateGetRects
  91. @abstract Returns a pointer to an array of CGRect structs that describe what parts of the frame have changed relative
  92. to the previously delivered frame. This rectangle list encapsulates both the update rectangles and movement rectangles.
  93. @param updateRef The CGDisplayStreamUpdateRef
  94. @param rectCount A pointer to where the count of the number of rectangles in the array is to be returned. Must not be NULL.
  95. @result A pointer to the array of CGRectangles. This array should not be freed by the caller.
  96. */
  97. CG_EXTERN const CGRect *__nullable CGDisplayStreamUpdateGetRects(
  98. CGDisplayStreamUpdateRef __nullable updateRef,
  99. CGDisplayStreamUpdateRectType rectType, size_t *rectCount)
  100. CG_AVAILABLE_BUT_DEPRECATED(
  101. 11.0, 14.0,
  102. "Please use ScreenCaptureKit API's SCStreamFrameInfo with SCStreamFrameInfoContentRect instead");
  103. /*!
  104. @function CGDisplayStreamUpdateCreateMerged
  105. @abstract Merge two CGDisplayUpdateRefs into a new one.
  106. @discussion In cases where the client wishes to drop certain frame updates, this function may be used to merge two
  107. CGDisplayUpdateRefs together. The core bit of functionality here is generating a new set of refresh/move/dirty
  108. rectangle arrays that properly represent the union of the deltas between the two frames. Note that the ordering of
  109. the two refs is important.
  110. @param firstUpdate The first update (in a temporal sense)
  111. @param secondUpdate The second update (in a temporal sense)
  112. @result The new CGDisplayStreamUpdateRef
  113. */
  114. CG_EXTERN CGDisplayStreamUpdateRef __nullable
  115. CGDisplayStreamUpdateCreateMergedUpdate(
  116. CGDisplayStreamUpdateRef __nullable firstUpdate,
  117. CGDisplayStreamUpdateRef __nullable secondUpdate)
  118. CG_AVAILABLE_BUT_DEPRECATED(
  119. 11.0, 14.0,
  120. "There is no direct replacement for this function. Please use ScreenCaptureKit API's SCStreamFrameInfo to replace CGDisplayStreamUpdate");
  121. /*!
  122. @function CGDisplayStreamUpdateGetMovedRectsDelta
  123. @abstract Return the movement dx and dy values for a single update
  124. @param updateRef The CGDisplayStreamUpdateRef
  125. @param dx A pointer to a CGFloat to store the x component of the movement delta
  126. @param dy A pointer to a CGFloat to store the y component of the movement delta
  127. @discussion The delta values describe the offset from the moved rectangles back to the source location.
  128. */
  129. CG_EXTERN void CGDisplayStreamUpdateGetMovedRectsDelta(
  130. CGDisplayStreamUpdateRef __nullable updateRef, CGFloat *dx, CGFloat *dy)
  131. CG_AVAILABLE_BUT_DEPRECATED(
  132. 11.0, 14.0,
  133. "Please use ScreenCaptureKit API's SCStreamFrameInfo with SCStreamFrameInfoContentRect instead");
  134. /*!
  135. @function CGDisplayStreamGetDropCount
  136. @abstract Return how many frames (if any) have been dropped since the last call to the handler.
  137. @param updateRef The CGDisplayStreamUpdateRef
  138. @result The number of dropped frames
  139. @discussion This call is primarily useful for performance measurement to determine if the client is keeping up with
  140. all WindowServer updates.
  141. */
  142. CG_EXTERN size_t
  143. CGDisplayStreamUpdateGetDropCount(CGDisplayStreamUpdateRef __nullable updateRef)
  144. CG_AVAILABLE_BUT_DEPRECATED(
  145. 11.0, 14.0,
  146. "There is no direct replacement for this function. Please use ScreenCaptureKit API's SCStreamFrameInfo to replace CGDisplayStreamUpdate");
  147. /* Optional CGDisplayStream Properties */
  148. /*!
  149. @const kCGDisplayStreamSourceRect
  150. @discussion This may be used to request a subregion of the display to be provided as the source of the display stream. Use
  151. CGRectCreateDictionaryRepresentation to convert from a CGRect to the value used here. Note: The coordinate system for the
  152. source rectangle is specified in display logical coordinates and not in pixels, in order to match the normal convention on
  153. HiDPI displays.
  154. */
  155. CG_EXTERN const CFStringRef kCGDisplayStreamSourceRect CG_AVAILABLE_BUT_DEPRECATED(
  156. 11.0, 14.0,
  157. "Please use ScreenCaptureKit API's SCStreamConfiguration sourceRect property instead"); /* Source rectangle to capture - defaults to entire display */
  158. /*!
  159. @const kCGDisplayStreamDestinationRect
  160. @discussion This may be used to request where within the destination buffer the display updates should be placed. Use
  161. CGRectCreateDictionaryRepresentation to convert from a CGRect to the value used here. Note: The coordinate system for
  162. the destination rectangle is always specified in output pixels to match the fact that the output buffer size is also
  163. specified in terms of pixels.
  164. */
  165. CG_EXTERN const CFStringRef kCGDisplayStreamDestinationRect CG_AVAILABLE_BUT_DEPRECATED(
  166. 11.0, 14.0,
  167. "Please use ScreenCaptureKit API's SCStreamConfiguration destinationRect property instead"); /* Destination rectangle - defaults to entire buffer */
  168. /*!
  169. @const kCGDisplayStreamPreserveAspectRatio
  170. @discussion Enable/disable the work the Window Server will do to preserve the display aspect ratio. By default the Window Server will
  171. assume that it should preserve the original aspect ratio of the source display rect. If the aspect ratio of the source display and
  172. the display stream destination rect are not the same, black borders will be inserted at the top/bottom or right/left sides of the destination
  173. in order to preserve the source aspect ratio.
  174. */
  175. CG_EXTERN const CFStringRef kCGDisplayStreamPreserveAspectRatio CG_AVAILABLE_BUT_DEPRECATED(
  176. 11.0, 14.0,
  177. "Please use ScreenCaptureKit API's SCStreamConfiguration preserveAspectRatio property instead"); /* CFBoolean - defaults to true */
  178. /*!
  179. @const kCGDisplayStreamColorSpace
  180. @discussion Set the desired CGColorSpace of the output frames. By default the color space will be that of the display.
  181. */
  182. CG_EXTERN const CFStringRef kCGDisplayStreamColorSpace CG_AVAILABLE_BUT_DEPRECATED(
  183. 11.0, 14.0,
  184. "Please use ScreenCaptureKit API's SCStreamConfiguration colorSpaceName property instead"); /* Desired output color space (CGColorSpaceRef) - defaults to display color space */
  185. /*!
  186. @const kCGDisplayStreamMinimumFrameTime
  187. @discussion Request that the delta between frame updates be at least as much specified by this value.
  188. */
  189. CG_EXTERN const CFStringRef kCGDisplayStreamMinimumFrameTime CG_AVAILABLE_BUT_DEPRECATED(
  190. 11.0, 14.0,
  191. "Please use ScreenCaptureKit API's SCStreamConfiguration minimumFrameInterval property instead"); /* CFNumber in seconds, defaults to zero. */
  192. /*!
  193. @const kCGDisplayStreamShowCursor
  194. @discussion Controls whether the cursor is embedded within the provided buffers or not.
  195. */
  196. CG_EXTERN const CFStringRef kCGDisplayStreamShowCursor CG_AVAILABLE_BUT_DEPRECATED(
  197. 11.0, 14.0,
  198. "Please use ScreenCaptureKit API's SCStreamConfiguration showsCursor property instead"); /* CFBoolean - defaults to false */
  199. /*!
  200. @const kCGDisplayStreamQueueDepth
  201. @discussion Controls how many frames deep the frame queue will be. Defaults to N.
  202. */
  203. CG_EXTERN const CFStringRef kCGDisplayStreamQueueDepth CG_AVAILABLE_BUT_DEPRECATED(
  204. 11.0, 14.0,
  205. "Please use ScreenCaptureKit API's SCStreamConfiguration queueDepth property instead"); /* Queue depth in frames. Defaults to 3. */
  206. /*!
  207. @const kCGDisplayStreamYCbCrMatrix
  208. @discussion When outputting frames in 420v or 420f format, this key may be used to control which YCbCr matrix is used
  209. The value should be one of the three kCGDisplayStreamYCbCrMatrix values specified below.
  210. */
  211. CG_EXTERN const CFStringRef kCGDisplayStreamYCbCrMatrix CG_AVAILABLE_BUT_DEPRECATED(
  212. 11.0, 14.0,
  213. "Please use ScreenCaptureKit API's SCStreamConfiguration colorMatrix property");
  214. /* Supported YCbCr matrices. Note that these strings have identical values to the equivalent CoreVideo strings. */
  215. CG_EXTERN const CFStringRef kCGDisplayStreamYCbCrMatrix_ITU_R_709_2;
  216. CG_EXTERN const CFStringRef kCGDisplayStreamYCbCrMatrix_ITU_R_601_4;
  217. CG_EXTERN const CFStringRef kCGDisplayStreamYCbCrMatrix_SMPTE_240M_1995;
  218. /*!
  219. @function CGDisplayStreamGetTypeID
  220. @abstract Returns the CF "class" ID for CGDisplayStream
  221. @result The CFTypeID of the CGDisplayStream class.
  222. */
  223. CG_EXTERN CFTypeID CGDisplayStreamGetTypeID(void) CG_AVAILABLE_BUT_DEPRECATED(
  224. 11.0, 14.0,
  225. "There is no direct replacement for this function. Please use ScreenCaptureKit API's SCStream to replace CGDisplayStream");
  226. /*!
  227. @function CGDisplayStreamCreate
  228. @abstract Creates a new CGDisplayStream intended to be used with a CFRunLoop
  229. @discussion This function creates a new CGDisplayStream that is to be used to get a stream of frame updates
  230. from a particular display.
  231. @param display The CGDirectDisplayID to use as the source for generated frames
  232. @param outputWidth The output width (in pixels, not points) of the frames to be generated. Must not be zero.
  233. @param outputHeight The output height (in pixels, not points) of the frames to be generated. Must not be zero.
  234. @param pixelFormat The desired CoreVideo/CoreMedia-style pixel format of the output IOSurfaces. The currently
  235. supported values are:
  236. 'BGRA' Packed Little Endian ARGB8888
  237. 'l10r' Packed Little Endian ARGB2101010
  238. '420v' 2-plane "video" range YCbCr 4:2:0
  239. '420f' 2-plane "full" range YCbCr 4:2:0
  240. @param properties Any optional properties of the CGDisplayStream
  241. @param handler A block that will be called for frame deliver.
  242. @result The new CGDisplayStream object.
  243. */
  244. CG_EXTERN CGDisplayStreamRef __nullable CGDisplayStreamCreate(
  245. CGDirectDisplayID display, size_t outputWidth, size_t outputHeight,
  246. int32_t pixelFormat, CFDictionaryRef __nullable properties,
  247. CGDisplayStreamFrameAvailableHandler __nullable handler)
  248. CG_AVAILABLE_BUT_DEPRECATED(
  249. 11.0, 14.0,
  250. "Please use ScreenCaptureKit API's initWithFilter:configuration:delegate: instead");
  251. /*!
  252. @function CGDisplayStreamCreateWithDispatchQueue
  253. @abstract Creates a new CGDisplayStream intended to be serviced by a block handler
  254. @discussion This function creates a new CGDisplayStream that is to be used to get a stream of frame updates
  255. from a particular display.
  256. @param display The CGDirectDisplayID to use as the source for generated frames
  257. @param outputWidth The output width (in pixels, not points) of the frames to be generated. Must not be zero.
  258. @param outputHeight The output height (in pixels, not points) of the frames to be generated. Must not be zero.
  259. @param pixelFormat The desired CoreVideo/CoreMedia-style pixel format of the output IOSurfaces
  260. @param properties Any optional properties of the CGDisplayStream
  261. @param queue The dispatch_queue_t that will be used to invoke the callback handler.
  262. @param handler A block that will be called for frame deliver.
  263. @result The new CGDisplayStream object.
  264. */
  265. CG_EXTERN CGDisplayStreamRef __nullable CGDisplayStreamCreateWithDispatchQueue(
  266. CGDirectDisplayID display, size_t outputWidth, size_t outputHeight,
  267. int32_t pixelFormat, CFDictionaryRef __nullable properties,
  268. dispatch_queue_t queue,
  269. CGDisplayStreamFrameAvailableHandler __nullable handler)
  270. CG_AVAILABLE_BUT_DEPRECATED(
  271. 11.0, 14.0,
  272. "Please use ScreenCaptureKit API's initWithFilter:configuration:delegate: instead");
  273. /*!
  274. @function CGDisplayStreamStart
  275. @abstract Begin delivering frame updates to the handler block.
  276. @param displayStream to be started
  277. @result kCGErrorSuccess If the display stream was started, otherwise an error.
  278. */
  279. CG_EXTERN CGError
  280. CGDisplayStreamStart(CGDisplayStreamRef cg_nullable displayStream)
  281. CG_AVAILABLE_BUT_DEPRECATED(
  282. 11.0, 14.0,
  283. "Please use ScreenCaptureKit API's startCaptureWithCompletionHandler: to start a stream instead");
  284. /*!
  285. @function CGDisplayStreamStop
  286. @abstract End delivery of frame updates to the handler block.
  287. @param displayStream to be stopped
  288. @result kCGErrorSuccess If the display stream was stopped, otherwise an error.
  289. @discussion After this call returns, the CGDisplayStream callback function will eventually be called with a
  290. status of kCGDisplayStreamFrameStatusStopped. After that point it is safe to release the CGDisplayStream.
  291. It is safe to call this function from within the handler block, but the previous caveat still applies.
  292. */
  293. CG_EXTERN CGError
  294. CGDisplayStreamStop(CGDisplayStreamRef cg_nullable displayStream)
  295. CG_AVAILABLE_BUT_DEPRECATED(
  296. 11.0, 14.0,
  297. "Please use ScreenCaptureKit API's stopCaptureWithCompletionHandler: to stop a stream instead");
  298. /*!
  299. @function CGDisplayStreamGetRunLoopSource
  300. @abstract Return the singleton CFRunLoopSourceRef for a CGDisplayStream.
  301. @param displayStream The CGDisplayStream object
  302. @result The CFRunLoopSource for this displayStream. Note: This function will return NULL if the
  303. display stream was created via CGDisplayStreamCreateWithDispatchQueue().
  304. */
  305. CG_EXTERN CFRunLoopSourceRef __nullable
  306. CGDisplayStreamGetRunLoopSource(CGDisplayStreamRef cg_nullable displayStream)
  307. CG_AVAILABLE_BUT_DEPRECATED(
  308. 11.0, 14.0,
  309. "There is no direct replacement for this function. Please use ScreenCaptureKit API's SCStream to replace CGDisplayStream");
  310. #endif /* __BLOCKS__ */
  311. CF_ASSUME_NONNULL_END
  312. CF_IMPLICIT_BRIDGING_DISABLED
  313. #endif /* CGDISPLAYSTREAM_H_ */