Banuba SDK
BNBEffectPlayer.h
Go to the documentation of this file.
1 // AUTOGENERATED FILE - DO NOT MODIFY!
2 // This file was generated by Djinni from effect_player.djinni
3 
5 #import "BNBConsistencyMode.h"
7 #import "BNBFrameData.h"
8 #import "BNBPixelBuffer.h"
9 #import "BNBPixelFormat.h"
10 #import "BNBRenderBackendType.h"
11 #import "BanubaEffectPlayer/BNBFullImageData.h"
12 #import <Foundation/Foundation.h>
13 @class BNBEffectManager;
14 @class BNBEffectPlayer;
16 @class BNBFrameProcessor;
17 @class BNBInputManager;
18 @protocol BNBCameraPoiListener;
20 @protocol BNBEffectInfoListener;
21 @protocol BNBFaceNumberListener;
22 @protocol BNBFrameDataListener;
23 @protocol BNBFrameDurationListener;
24 
25 
26 /**
27  * The EffectPlayer class provides ability to play AR effects on set of images or video.
28  * <h2>Lifecycle</h2>
29  * In order to support an application lifecycle EffectPlayer provides special methods
30  * to control it during application states like pause or losing focus.
31  * <h3>Effect playback</h3>
32  * Initial playback state of newly created Effect Player instance is `active`. With playback
33  * control methods described below, the player can be launched/resumed or paused. State of effect
34  * player becomes stopped/inactive at the moment of surface loss or by calling the
35  * EffectPlayer::playback_stop method.
36  * One can use next three methods to control effect playback:
37  * <br/> - `EffectPlayer.playback_play`. Switching to active state attempt. Possible from
38  * paused or stopped state and has no effect if effect playback is already active. Playback
39  * resumes from the position saved before `EffectPlayer.playbackPause` call.
40  * <br/> - `EffectPlayer.playback_pause`. Suspend current playback attempt. The recognizer thread
41  * is stopped and all the video textures and audio units playback is stopped as well. Effect
42  * player doesn't react on EffectPlayer::pushFrame calls in suspended state except
43  * asynchronous-inconsistent mode.
44  * <br/> - `EffectPlayer.playbackStop`. Switch to inactive state. In addition to pause clears
45  * recognizer's buffer. The next switch to active state will result in total rerun of active
46  * effect which means that it will be started from the very beginning.
47  * <h2>Android usage example</h2>
48  * If you want an application to be active on windowed mode and if the
49  * focus is on another application you can switch EffectPlayer into active state when
50  * `onStart` activity callback occurs and switch to paused when `onStop` occurs. Otherwise you
51  * can suspend `EffectPlayer` on `onPause` and resume on `onResume` callback to make an
52  * instance of the `EffectPlayer` inactive while activity losing focus.
53  * <h2> Context/surface control</h2>
54  * There are two methods that have to be used to ensure correct operating of the
55  * EffectPlayer. Losing context without notifying the EffectPlayer
56  * leads to application crash. Methods for context changes notification:
57  * <br/> - `EffectPlayer.surfaceCreated`
58  * <br/> - `EffectPlayer.surfaceDestroyed`
59  * <h2> Multi-Threading performance and safety. Listeners.</h2>
60  * EffectPlayer allows to call some of its methods from several threads (simultaneously) for convenience and increased performance.
61  * This is allowed only during normal operation, all invocations from other threads MUST finish before
62  * you start destroying the EffectPlayer instance.
63  * Most methods belong to one of the two groups:
64  * <br/> - MUST be called from the "Main" (also "Render") thread ("Not Reentrant, Not Thread-safe")
65  * <br/> - MAY be called from "Any" thread. Most are "Thread-safe"
66  * Listener callbacks can be called back from any thread.
67  * Generally for best performance and responsiveness you'll have 3 threads:
68  * <br/> - Main(render) thread for drawing-related operations
69  * <br/> - "Camera" thread for handling and push()-ing frames into EffectPlayer
70  * <br/> - UI Thread for handling user interactions and other tasks
71  */
72 
73 #ifndef DJINNI_EXPORT
74  #define DJINNI_EXPORT __attribute__((__visibility__("default")))
75 #endif
76 
78 @interface BNBEffectPlayer : NSObject
79 
80 + (nullable BNBEffectPlayer *)create:(nullable BNBEffectPlayerConfiguration *)configuration;
81 
82 /**
83  * Add callback to receive FPS information.
84  * Thread-safe. May be called from any thread
85  */
86 - (void)addFrameDurationListener:(nullable id<BNBFrameDurationListener>)frameDurationListener;
87 
88 /**
89  * Remove callback to receive FPS information.
90  * Thread-safe. May be called from any thread
91  */
92 - (void)removeFrameDurationListener:(nullable id<BNBFrameDurationListener>)frameDurationListener;
93 
94 /**
95  * Add callback to receive faces count in frame.
96  * Thread-safe. May be called from any thread
97  */
98 - (void)addFaceNumberListener:(nullable id<BNBFaceNumberListener>)faceNumberListener;
99 
100 /**
101  * Remove callback to receive faces count in frame.
102  * Thread-safe. May be called from any thread
103  */
104 - (void)removeFaceNumberListener:(nullable id<BNBFaceNumberListener>)faceNumberListener;
105 
106 /**
107  * Add callback to receive frame data right after processing in recognizer.
108  * Thread-safe. May be called from any thread
109  */
110 - (void)addFrameDataListener:(nullable id<BNBFrameDataListener>)frameDataListener;
111 
112 /**
113  * Remove callback to receive frame data right after processing in recognizer.
114  * Thread-safe. May be called from any thread
115  */
116 - (void)removeFrameDataListener:(nullable id<BNBFrameDataListener>)frameDataListener;
117 
118 /**
119  * Add callback to receive center of the face in frame relative to top-left
120  * corner in [0, 1] space.
121  * Thread-safe. May be called from any thread
122  */
123 - (void)addCameraPoiListener:(nullable id<BNBCameraPoiListener>)cameraPoiListener;
124 
125 /**
126  * Remove callback to receive center of the face in frame relative to top-left
127  * Thread-safe. May be called from any thread
128  */
129 - (void)removeCameraPoiListener:(nullable id<BNBCameraPoiListener>)cameraPoiListener;
130 
131 /**
132  * Add callback to receive current effect info from Effect Player.
133  * Thread-safe. May be called from any thread
134  */
135 - (void)addEffectInfoListener:(nullable id<BNBEffectInfoListener>)effectInfoListener;
136 
137 /**
138  * Remove callback to receive current effect info from Effect Player.
139  * Thread-safe. May be called from any thread
140  */
141 - (void)removeEffectInfoListener:(nullable id<BNBEffectInfoListener>)effectInfoListener;
142 
143 /**
144  * Add callback to receive current effect activation notification from Effect Player.
145  * Thread-safe. May be called from any thread
146  */
147 - (void)addEffectActivationCompletionListener:(nullable id<BNBEffectActivationCompletionListener>)effectActivationCompletionListener;
148 
149 /**
150  * Remove callback to receive current effect activation notification from Effect Player.
151  * Thread-safe. May be called from any thread
152  */
153 - (void)removeEffectActivationCompletionListener:(nullable id<BNBEffectActivationCompletionListener>)effectActivationCompletionListener;
154 
155 /**
156  * Sets maximum allowed face results, if face tracking feature is present.
157  * Thread-safe. May be called from any thread
158  * This option is relevant only for GLFX effects.
159  */
160 - (void)setMaxFaces:(int32_t)maxFaces;
161 
162 /**
163  * Set OpenGL name of input external texture.
164  * https://developer.android.com/reference/android/graphics/SurfaceTexture .
165  * MUST be called from the render thread
166  *
167  * @param name Texture name
168  * @param width Textute width
169  * @param height Texture height
170  *
171  * @see setUseExtCamTex
172  *
173  * <br/> Note: This function is Android only.
174  * <br/> Note: We use texture size to calculate aspect ratio only.
175  */
176 - (void)setExternalCameraTexture:(int32_t)name
177  width:(int32_t)width
178  height:(int32_t)height;
179 
180 /**
181  * Use external texture as input source.
182  * MUST be called from the render thread
183  * @see setExternalCameraTexture
184  * @note This function is Android only
185  */
186 - (void)setUseExtCamTex:(BOOL)value;
187 
188 /**
189  * Use to notify the EffectPlayer that the surface exists and effect can be played.
190  * 0, 0 for width and height are valid values.
191  * MUST be called from the render thread
192  */
193 - (void)surfaceCreated:(int32_t)width
194  height:(int32_t)height;
195 
196 /**
197  * Notify about rendering surface being resized.
198  * MUST be called from the render thread
199  */
200 - (void)surfaceChanged:(int32_t)width
201  height:(int32_t)height;
202 
203 /**
204  * This method should be called right before an active context will become invalid.
205  * Switches playback state to inactive state. If it's not done an application will be
206  * crashed on next draw iteration. After losing the surface effect playback can't be
207  * resumed from last position.
208  * MUST be called from the render thread
209  */
210 - (void)surfaceDestroyed;
211 
212 /**
213  * Draw the current effect into the current OpenGL framebuffer. Uses internal frame_data
214  * object obtained from latest push_frame recognition result.
215  *
216  * Return current frame number if drawing was performed and caller should swap buffers
217  * otherwise `DRAW_SKIPPED`(-1)
218  * MUST be called from the render thread
219  */
220 - (int64_t)draw;
221 
222 /**
223  * Draw the current effect into the current OpenGL framebuffer. Uses externally provided frameData
224  * object instead of internal one obtained from latest push_frame recognition result.
225  *
226  * Return frame number from provided frameData if drawing was performed and caller should swap buffers
227  * otherwise `DRAW_SKIPPED`(-1)
228  * MUST be called from the render thread
229  */
230 - (int64_t)drawWithExternalFrameData:(nullable BNBFrameData *)frameData;
231 
232 /**
233  * Record last rendering ("draw_()") result into current OpenGL framebuffer.
234  * Content is cropped to maintain effect_size() aspect ratio
235  * MUST be called from the render thread
236  */
237 - (void)captureBlit:(int32_t)captureWidth
238  captureHeight:(int32_t)captureHeight;
239 
240 /**
241  * Receive the last rendered frame in binary form.
242  *
243  * @return 4-byte per pixel data, size is `fx_width * fx_height * 4`
244  * MUST be called from the render thread
245  */
246 - (nonnull BNBPixelBuffer *)readPixels;
247 
248 /** Thread-safe. May be called from any thread */
249 - (void)setRenderConsistencyMode:(BNBConsistencyMode)value;
250 
251 /**
252  * Process an image with current effect.
253  *
254  * Must be called from the render thread.
255  *
256  * @param inputImage to avoid conversion recommended to use YUV image
257  * @param outputPixelFormat to avoid conversion recommended to use RGBA
258  * @param params extra image processing arguments
259  */
260 - (nonnull NSData *)processImage:(nonnull BNBFullImageData *)inputImage
261  outputPixelFormat:(BNBPixelFormat)outputPixelFormat;
262 
263 /**
264  * Process an image with current effect.
265  * Prefer this method over `processImage` when you have extra input data
266  * besides just an input image.
267  *
268  * Must be called from render thread.
269  *
270  * @param inputFrameData `FrameData` with an image
271  * @param outputPixelFormat to avoid conversion recommended to use RGBA
272  * @param params extra image processing arguments
273  */
274 - (nonnull NSData *)processImageFrameData:(nullable BNBFrameData *)inputFrameData
275  outputPixelFormat:(BNBPixelFormat)outputPixelFormat;
276 
277 /**
278  * Process image with current effect.
279  * Must be called from render thread.
280  * NOTE: inputImage have copy overhead, preferable to use process_image method
281  * MUST be called from the render thread
282  * @param inputImage input image with `width * height * 4` size
283  * @param outputPixelFormat to avoid conversion recommended to use RGBA
284  */
285 - (nonnull NSData *)processImageData:(nonnull NSData *)inputImage
286  width:(int32_t)width
287  height:(int32_t)height
288  orientation:(BNBCameraOrientation)orientation
289  isMirrored:(BOOL)isMirrored
290  inputPixelFormat:(BNBPixelFormat)inputPixelFormat
291  outputPixelFormat:(BNBPixelFormat)outputPixelFormat;
292 
293 /**
294  * Provides image to process and to play effect.
295  * Thread-safe. May be called from any thread
296  */
297 - (void)pushFrame:(nonnull BNBFullImageData *)fullImage;
298 
299 /**
300  * Provides image to process and to play effect.
301  * Thread-safe. May be called from any thread
302  */
303 - (void)pushFrameWithNumber:(nonnull BNBFullImageData *)fullImage
304  frameNumber:(int64_t)frameNumber;
305 
306 /**
307  * Provides FrameData to process and to play effect.
308  * Must contain full image.
309  * Thread-safe. May be called from any thread
310  */
311 - (void)pushFrameData:(nullable BNBFrameData *)frameData;
312 
313 /**
314  * Provides FrameData with frame number to process and to play effect.
315  * Must contain full image.
316  * Thread-safe. May be called from any thread
317  */
318 - (void)pushFrameDataWithNumber:(nullable BNBFrameData *)frameData
319  frameNumber:(int64_t)frameNumber;
320 
321 /** MUST be called from the main(render) thread */
322 - (void)playbackPlay;
323 
324 - (void)playbackPause;
325 
326 - (void)playbackStop;
327 
328 /** Thread-safe. May be called from any thread */
329 - (BNBEffectPlayerPlaybackState)getPlaybackState;
330 
331 /**
332  * Get interface to control user iterations. This events will be passed to effect.
333  * Thread-safe. May be called from any thread
334  */
335 - (nullable BNBInputManager *)getInputManager;
336 
337 /**
338  * Initialize video processing. To provide frames use *process_video_frame* methods.
339  * Effect audio is recorded as well and can be accessed using *process_recorded_audio*
340  * and *write_recorded_audio*.
341  * *push_frame* should not be called during processing.
342  * MUST be called from the render thread
343  * @throw Exception Must be aborted with stop_video_processing()
344  */
345 - (void)startVideoProcessing:(int64_t)screenWidth
346  screenHeight:(int64_t)screenHeight
347  orientation:(BNBCameraOrientation)orientation
348  resetEffect:(BOOL)resetEffect
349  offlineMode:(BOOL)offlineMode;
350 
351 /**
352  * Finish processing and return renderer to normal state.
353  * MUST be called from the render thread
354  */
355 - (void)stopVideoProcessing:(BOOL)resetEffect;
356 
357 /**
358  * Provide frame data to evaluate video processing.
359  * MUST be called from the render thread
360  * @param inputFrameData Frame Data to process.
361  * @param params Processing params.
362  * @param recognizerIterations Number of processing iterations. Higher number means higher processing quality, but lower speed. Must be greater than 1. Pass null value for default number.
363  * @return Frame data with frame processing results.
364  */
365 - (nullable BNBFrameData *)processVideoFrameData:(nullable BNBFrameData *)inputFrameData
366  recognizerIterations:(nullable NSNumber *)recognizerIterations;
367 
368 /**
369  * Provide frame to evaluate video processing.
370  * MUST be called from the render thread
371  * @param inputImage Image to process.
372  * @param params Processing params.
373  * @param recognizerIterations Number of processing iterations. Higher number means higher processing quality, but lower speed. Must be greater than 1. Pass null value for default number.
374  * @return Frame data with frame processing results.
375  */
376 - (nullable BNBFrameData *)processVideoFrame:(nonnull BNBFullImageData *)inputImage
377  recognizerIterations:(nullable NSNumber *)recognizerIterations;
378 
379 /**
380  * Draw processed frame to image buffer.
381  * MUST be called from the render thread
382  * @param inputFrameData FrameData to draw.
383  * @param timeNs Frame position on timeline.
384  * @param outputPixelFormat Output image format.
385  * @return Buffer with processed image in selected format.
386  */
387 - (nonnull NSData *)drawVideoFrame:(nullable BNBFrameData *)inputFrameData
388  timeNs:(int64_t)timeNs
389  outputPixelFormat:(BNBPixelFormat)outputPixelFormat;
390 
391 /**
392  * The same as *draw_video_frame* but accepts preallocated buffer to avoid recreation for every frame.
393  * MUST be called from the render thread
394  */
395 - (nonnull NSData *)drawVideoFrameAllocated:(nullable BNBFrameData *)inputFrameData
396  timeNs:(int64_t)timeNs
397  outputPixelFormat:(BNBPixelFormat)outputPixelFormat
398  allocatedBuffer:(nonnull NSData *)allocatedBuffer;
399 
400 /**
401  * Save recorded audio.
402  * Not thread-safe but can be called from any thread.
403  * @param filename ".wav" extension must be used.
404  * @param lengthMs Track length. Set to '0' to use full duration.
405  */
406 - (void)writeRecordedAudio:(nonnull NSString *)filename
407  lengthMs:(int64_t)lengthMs;
408 
409 /**
410  * Trigger record start events in EP and Effect.
411  * Records all sounds during recording
412  * to be replayed by `processRecordedAudio`.
413  * If @param playAudioWhileRecording is true, the audio will continue to play. Otherwise, the audio will be muted.
414  * Thread-safe. May be called from any thread
415  */
416 - (void)onVideoRecordStart:(BOOL)playAudioWhileRecording;
417 
418 /**
419  * Triggers record stop events in EP and Effect.
420  * Thread-safe. May be called from any thread
421  */
422 - (void)onVideoRecordEnd;
423 
424 /**
425  * Get effect manager object
426  * Thread-safe. May be called from any thread
427  */
428 - (nullable BNBEffectManager *)effectManager;
429 
430 /**
431  * Force recognizer offline mode
432  * Thread-safe. May be called from any thread
433  */
434 - (void)setRecognizerOfflineMode:(BOOL)on;
435 
436 /**
437  * Set future frame filtrarion mode.
438  * Produce smoother recognition result (anti jitter), however adds inconsistency in push'ed/draw'ed frames (one frame lag)
439  * Example: push frame 1 - draw frame 1, push frame 2 - draw frame 1, push frame 3 - draw frame 2, ...
440  */
441 - (void)setRecognizerUseFutureFilter:(BOOL)on;
442 
443 /**
444  * Set future frame interpolation mode.
445  * Produce faster recognition result (skip even frames), however adds inconsistency in push'ed/pop'ed frames (one frame lag)
446  * Example: push frame 1 - pop frame 1, push frame 2 - pop frame 1, push frame 3 - pop frame 2, ...
447  */
448 - (void)setRecognizerUseFutureInterpolate:(BOOL)on;
449 
450 /**
451  * Set frame processor as current
452  * Thread-safe. May be called from any thread
453  */
454 - (void)setFrameProcessor:(nullable BNBFrameProcessor *)processor;
455 
456 /**
457  * Get current frame processor
458  * Thread-safe. May be called from any thread
459  */
460 - (nullable BNBFrameProcessor *)frameProcessor;
461 
462 + (void)setRenderBackend:(BNBRenderBackendType)backendType;
463 
464 + (BNBRenderBackendType)getCurrentRenderBackendType;
465 
466 @end
-[BNBEffectPlayer effectManager]
nullable BNBEffectManager * effectManager()
Get effect manager object Thread-safe.
BNBPixelFormat.h
BNBPixelBuffer.h
-[BNBEffectPlayer draw]
int64_t draw()
Draw the current effect into the current OpenGL framebuffer.
BNBEffectInfoListener-p
Callback interface to receive effect info changes.
Definition: BNBEffectInfoListener.h:9
BNBRenderBackendType.h
BNBFrameProcessor
Definition: BNBFrameProcessor.h:19
BNBFaceNumberListener-p
Callback to get face number detected.
Definition: BNBFaceNumberListener.h:8
BNBConsistencyMode.h
-[BNBEffectPlayer playbackPlay]
void playbackPlay()
MUST be called from the main(render) thread.
BNBFrameDataListener-p
Callback to get freshly processed frame_data.
Definition: BNBFrameDataListener.h:9
+[BNBEffectPlayer getCurrentRenderBackendType]
BNBRenderBackendType getCurrentRenderBackendType()
BNBPixelBuffer
Definition: BNBPixelBuffer.h:13
-[BNBEffectPlayer playbackStop]
void playbackStop()
-[BNBEffectPlayer frameProcessor]
nullable BNBFrameProcessor * frameProcessor()
Get current frame processor Thread-safe.
BNBEffectPlayer
The EffectPlayer class provides ability to play AR effects on set of images or video.
Definition: BNBEffectPlayer.h:78
BNBEffectPlayerConfiguration
Definition: BNBEffectPlayerConfiguration.h:15
BNBCameraPoiListener-p
Callback to get center of square bounding the face.
Definition: BNBCameraPoiListener.h:8
-[BNBEffectPlayer readPixels]
nonnull BNBPixelBuffer * readPixels()
Receive the last rendered frame in binary form.
-[BNBEffectPlayer onVideoRecordEnd]
void onVideoRecordEnd()
Triggers record stop events in EP and Effect.
DJINNI_EXPORT
#define DJINNI_EXPORT
for order of values in action units array see action_units_indices enum
Definition: BNBActionUnits.h:9
-[BNBEffectPlayer playbackPause]
void playbackPause()
BNBInputManager
Class to accepts touches from user.
Definition: BNBInputManager.h:19
BNBFrameDurationListener-p
Callback interface to receive FPS timings.
Definition: BNBFrameDurationListener.h:8
BNBEffectManager
Definition: BNBEffectManager.h:22
BNBCameraOrientation.h
-[BNBEffectPlayer surfaceDestroyed]
void surfaceDestroyed()
This method should be called right before an active context will become invalid.
-[BNBEffectPlayer getInputManager]
nullable BNBInputManager * getInputManager()
Get interface to control user iterations.
BNBFrameData.h
BNBFrameData
getters throw exceptions when data are not available android NNs usually output gpu masks
Definition: BNBFrameData.h:35
-[BNBEffectPlayer getPlaybackState]
BNBEffectPlayerPlaybackState getPlaybackState()
Thread-safe.
BNBEffectPlayerPlaybackState.h
BNBEffectActivationCompletionListener-p
Callback interface to receive effect activation notification.
Definition: BNBEffectActivationCompletionListener.h:8