Banuba SDK
effect_player.hpp
Go to the documentation of this file.
1 /// \file
2 /// \addtogroup EffectPlayer
3 /// @{
4 ///
5 // AUTOGENERATED FILE - DO NOT MODIFY!
6 // This file was generated by Djinni from effect_player.djinni
7 
8 #pragma once
9 
10 #include "bnb/scene/interfaces/render_backend_type.hpp"
14 #include <bnb/types/base_types.hpp>
15 #include <bnb/types/full_image.hpp>
16 #include <bnb/utils/defs.hpp>
17 #include <cstdint>
18 #include <memory>
19 #include <optional>
20 #include <string>
21 #include <vector>
22 
23 namespace bnb { namespace interfaces {
24 
25 class camera_poi_listener;
26 class effect_activation_completion_listener;
27 class effect_info_listener;
28 class effect_manager;
29 class effect_player_configuration;
30 class face_number_listener;
31 class frame_data_listener;
32 class frame_duration_listener;
33 class frame_processor;
34 class input_manager;
35 enum class camera_orientation;
36 enum class consistency_mode;
38 
39 /**
40  * The EffectPlayer class provides ability to play AR effects on set of images or video.
41  * <h2>Lifecycle</h2>
42  * In order to support an application lifecycle EffectPlayer provides special methods
43  * to control it during application states like pause or losing focus.
44  * <h3>Effect playback</h3>
45  * Initial playback state of newly created Effect Player instance is `active`. With playback
46  * control methods described below, the player can be launched/resumed or paused. State of effect
47  * player becomes stopped/inactive at the moment of surface loss or by calling the
48  * EffectPlayer::playback_stop method.
49  * One can use next three methods to control effect playback:
50  * <br/> - `EffectPlayer.playback_play`. Switching to active state attempt. Possible from
51  * paused or stopped state and has no effect if effect playback is already active. Playback
52  * resumes from the position saved before `EffectPlayer.playbackPause` call.
53  * <br/> - `EffectPlayer.playback_pause`. Suspend current playback attempt. The recognizer thread
54  * is stopped and all the video textures and audio units playback is stopped as well. Effect
55  * player doesn't react on EffectPlayer::pushFrame calls in suspended state except
56  * asynchronous-inconsistent mode.
57  * <br/> - `EffectPlayer.playbackStop`. Switch to inactive state. In addition to pause clears
58  * recognizer's buffer. The next switch to active state will result in total rerun of active
59  * effect which means that it will be started from the very beginning.
60  * <h2>Android usage example</h2>
61  * If you want an application to be active on windowed mode and if the
62  * focus is on another application you can switch EffectPlayer into active state when
63  * `onStart` activity callback occurs and switch to paused when `onStop` occurs. Otherwise you
64  * can suspend `EffectPlayer` on `onPause` and resume on `onResume` callback to make an
65  * instance of the `EffectPlayer` inactive while activity losing focus.
66  * <h2> Context/surface control</h2>
67  * There are two methods that have to be used to ensure correct operating of the
68  * EffectPlayer. Losing context without notifying the EffectPlayer
69  * leads to application crash. Methods for context changes notification:
70  * <br/> - `EffectPlayer.surfaceCreated`
71  * <br/> - `EffectPlayer.surfaceDestroyed`
72  * <h2> Multi-Threading performance and safety. Listeners.</h2>
73  * EffectPlayer allows to call some of its methods from several threads (simultaneously) for convenience and increased performance.
74  * This is allowed only during normal operation, all invocations from other threads MUST finish before
75  * you start destroying the EffectPlayer instance.
76  * Most methods belong to one of the two groups:
77  * <br/> - MUST be called from the "Main" (also "Render") thread ("Not Reentrant, Not Thread-safe")
78  * <br/> - MAY be called from "Any" thread. Most are "Thread-safe"
79  * Listener callbacks can be called back from any thread.
80  * Generally for best performance and responsiveness you'll have 3 threads:
81  * <br/> - Main(render) thread for drawing-related operations
82  * <br/> - "Camera" thread for handling and push()-ing frames into EffectPlayer
83  * <br/> - UI Thread for handling user interactions and other tasks
84  */
85 class BNB_EXPORT effect_player {
86 public:
87  virtual ~effect_player() {}
88 
89  static std::shared_ptr<effect_player> create(const std::shared_ptr<effect_player_configuration> & configuration);
90 
91  /**
92  * Add callback to receive FPS information.
93  * Thread-safe. May be called from any thread
94  */
95  virtual void add_frame_duration_listener(const std::shared_ptr<frame_duration_listener> & frame_duration_listener) = 0;
96 
97  /**
98  * Remove callback to receive FPS information.
99  * Thread-safe. May be called from any thread
100  */
101  virtual void remove_frame_duration_listener(const std::shared_ptr<frame_duration_listener> & frame_duration_listener) = 0;
102 
103  /**
104  * Add callback to receive faces count in frame.
105  * Thread-safe. May be called from any thread
106  */
107  virtual void add_face_number_listener(const std::shared_ptr<face_number_listener> & face_number_listener) = 0;
108 
109  /**
110  * Remove callback to receive faces count in frame.
111  * Thread-safe. May be called from any thread
112  */
113  virtual void remove_face_number_listener(const std::shared_ptr<face_number_listener> & face_number_listener) = 0;
114 
115  /**
116  * Add callback to receive frame data right after processing in recognizer.
117  * Thread-safe. May be called from any thread
118  */
119  virtual void add_frame_data_listener(const std::shared_ptr<frame_data_listener> & frame_data_listener) = 0;
120 
121  /**
122  * Remove callback to receive frame data right after processing in recognizer.
123  * Thread-safe. May be called from any thread
124  */
125  virtual void remove_frame_data_listener(const std::shared_ptr<frame_data_listener> & frame_data_listener) = 0;
126 
127  /**
128  * Add callback to receive center of the face in frame relative to top-left
129  * corner in [0, 1] space.
130  * Thread-safe. May be called from any thread
131  */
132  virtual void add_camera_poi_listener(const std::shared_ptr<camera_poi_listener> & camera_poi_listener) = 0;
133 
134  /**
135  * Remove callback to receive center of the face in frame relative to top-left
136  * Thread-safe. May be called from any thread
137  */
138  virtual void remove_camera_poi_listener(const std::shared_ptr<camera_poi_listener> & camera_poi_listener) = 0;
139 
140  /**
141  * Add callback to receive current effect info from Effect Player.
142  * Thread-safe. May be called from any thread
143  */
144  virtual void add_effect_info_listener(const std::shared_ptr<effect_info_listener> & effect_info_listener) = 0;
145 
146  /**
147  * Remove callback to receive current effect info from Effect Player.
148  * Thread-safe. May be called from any thread
149  */
150  virtual void remove_effect_info_listener(const std::shared_ptr<effect_info_listener> & effect_info_listener) = 0;
151 
152  /**
153  * Add callback to receive current effect activation notification from Effect Player.
154  * Thread-safe. May be called from any thread
155  */
156  virtual void add_effect_activation_completion_listener(const std::shared_ptr<effect_activation_completion_listener> & effect_activation_completion_listener) = 0;
157 
158  /**
159  * Remove callback to receive current effect activation notification from Effect Player.
160  * Thread-safe. May be called from any thread
161  */
162  virtual void remove_effect_activation_completion_listener(const std::shared_ptr<effect_activation_completion_listener> & effect_activation_completion_listener) = 0;
163 
164  /**
165  * Sets maximum allowed face results, if face tracking feature is present.
166  * Thread-safe. May be called from any thread
167  * This option is relevant only for GLFX effects.
168  */
169  virtual void set_max_faces(int32_t max_faces) = 0;
170 
171  /**
172  * Set OpenGL name of input external texture.
173  * https://developer.android.com/reference/android/graphics/SurfaceTexture .
174  * MUST be called from the render thread
175  *
176  * @param name Texture name
177  * @param width Textute width
178  * @param height Texture height
179  *
180  * @see setUseExtCamTex
181  *
182  * <br/> Note: This function is Android only.
183  * <br/> Note: We use texture size to calculate aspect ratio only.
184  */
185  virtual void set_external_camera_texture(int32_t name, int32_t width, int32_t height) = 0;
186 
187  /**
188  * Use external texture as input source.
189  * MUST be called from the render thread
190  * @see setExternalCameraTexture
191  * @note This function is Android only
192  */
193  virtual void set_use_ext_cam_tex(bool value) = 0;
194 
195  /**
196  * Use to notify the EffectPlayer that the surface exists and effect can be played.
197  * 0, 0 for width and height are valid values.
198  * MUST be called from the render thread
199  */
200  virtual void surface_created(int32_t width, int32_t height) = 0;
201 
202  /**
203  * Notify about rendering surface being resized.
204  * MUST be called from the render thread
205  */
206  virtual void surface_changed(int32_t width, int32_t height) = 0;
207 
208  /**
209  * This method should be called right before an active context will become invalid.
210  * Switches playback state to inactive state. If it's not done an application will be
211  * crashed on next draw iteration. After losing the surface effect playback can't be
212  * resumed from last position.
213  * MUST be called from the render thread
214  */
215  virtual void surface_destroyed() = 0;
216 
217  /**
218  * Draw the current effect into the current OpenGL framebuffer. Uses internal frame_data
219  * object obtained from latest push_frame recognition result.
220  *
221  * Return current frame number if drawing was performed and caller should swap buffers
222  * otherwise `DRAW_SKIPPED`(-1)
223  * MUST be called from the render thread
224  */
225  virtual int64_t draw() = 0;
226 
227  /**
228  * Draw the current effect into the current OpenGL framebuffer. Uses externally provided frame_data
229  * object instead of internal one obtained from latest push_frame recognition result.
230  *
231  * Return frame number from provided frame_data if drawing was performed and caller should swap buffers
232  * otherwise `DRAW_SKIPPED`(-1)
233  * MUST be called from the render thread
234  */
235  virtual int64_t draw_with_external_frame_data(const std::shared_ptr<::bnb::interfaces::frame_data> & frame_data) = 0;
236 
237  /**
238  * Record last rendering ("draw_()") result into current OpenGL framebuffer.
239  * Content is cropped to maintain effect_size() aspect ratio
240  * MUST be called from the render thread
241  */
242  virtual void capture_blit(int32_t capture_width, int32_t capture_height) = 0;
243 
244  /**
245  * Receive the last rendered frame in binary form.
246  *
247  * @return 4-byte per pixel data, size is `fx_width * fx_height * 4`
248  * MUST be called from the render thread
249  */
250  virtual ::bnb::interfaces::pixel_buffer read_pixels() = 0;
251 
252  /** Thread-safe. May be called from any thread */
253  virtual void set_render_consistency_mode(consistency_mode value) = 0;
254 
255  /**
256  * Process an image with current effect.
257  *
258  * Must be called from the render thread.
259  *
260  * @param input_image to avoid conversion recommended to use YUV image
261  * @param output_pixel_format to avoid conversion recommended to use RGBA
262  * @param params extra image processing arguments
263  */
264  virtual ::bnb::data_t process_image(::bnb::full_image_t input_image, ::bnb::interfaces::pixel_format output_pixel_format) = 0;
265 
266  /**
267  * Process an image with current effect.
268  * Prefer this method over `processImage` when you have extra input data
269  * besides just an input image.
270  *
271  * Must be called from render thread.
272  *
273  * @param input_frame_data `FrameData` with an image
274  * @param output_pixel_format to avoid conversion recommended to use RGBA
275  * @param params extra image processing arguments
276  */
277  virtual ::bnb::data_t process_image_frame_data(const std::shared_ptr<::bnb::interfaces::frame_data> & input_frame_data, ::bnb::interfaces::pixel_format output_pixel_format) = 0;
278 
279  /**
280  * Process image with current effect.
281  * Must be called from render thread.
282  * NOTE: input_image have copy overhead, preferable to use process_image method
283  * MUST be called from the render thread
284  * @param input_image input image with `width * height * 4` size
285  * @param output_pixel_format to avoid conversion recommended to use RGBA
286  */
287  virtual std::vector<uint8_t> process_image_data(const std::vector<uint8_t> & input_image, int32_t width, int32_t height, camera_orientation orientation, bool is_mirrored, ::bnb::interfaces::pixel_format input_pixel_format, ::bnb::interfaces::pixel_format output_pixel_format) = 0;
288 
289  /**
290  * Provides image to process and to play effect.
291  * Thread-safe. May be called from any thread
292  */
293  virtual void push_frame(::bnb::full_image_t full_image) = 0;
294 
295  /**
296  * Provides image to process and to play effect.
297  * Thread-safe. May be called from any thread
298  */
299  virtual void push_frame_with_number(::bnb::full_image_t full_image, int64_t frame_number) = 0;
300 
301  /**
302  * Provides FrameData to process and to play effect.
303  * Must contain full image.
304  * Thread-safe. May be called from any thread
305  */
306  virtual void push_frame_data(const std::shared_ptr<::bnb::interfaces::frame_data> & frame_data) = 0;
307 
308  /**
309  * Provides FrameData with frame number to process and to play effect.
310  * Must contain full image.
311  * Thread-safe. May be called from any thread
312  */
313  virtual void push_frame_data_with_number(const std::shared_ptr<::bnb::interfaces::frame_data> & frame_data, int64_t frame_number) = 0;
314 
315  /** MUST be called from the main(render) thread */
316  virtual void playback_play() = 0;
317 
318  virtual void playback_pause() = 0;
319 
320  virtual void playback_stop() = 0;
321 
322  /** Thread-safe. May be called from any thread */
323  virtual effect_player_playback_state get_playback_state() = 0;
324 
325  /**
326  * Get interface to control user iterations. This events will be passed to effect.
327  * Thread-safe. May be called from any thread
328  */
329  virtual std::shared_ptr<input_manager> get_input_manager() = 0;
330 
331  /**
332  * Initialize video processing. To provide frames use *process_video_frame* methods.
333  * Effect audio is recorded as well and can be accessed using *process_recorded_audio*
334  * and *write_recorded_audio*.
335  * *push_frame* should not be called during processing.
336  * MUST be called from the render thread
337  * @throw Exception Must be aborted with stop_video_processing()
338  */
339  virtual void start_video_processing(int64_t screen_width, int64_t screen_height, camera_orientation orientation, bool reset_effect, bool offline_mode) = 0;
340 
341  /**
342  * Finish processing and return renderer to normal state.
343  * MUST be called from the render thread
344  */
345  virtual void stop_video_processing(bool reset_effect) = 0;
346 
347  /**
348  * Provide frame data to evaluate video processing.
349  * MUST be called from the render thread
350  * @param input_frame_data Frame Data to process.
351  * @param params Processing params.
352  * @param recognizer_iterations Number of processing iterations. Higher number means higher processing quality, but lower speed. Must be greater than 1. Pass null value for default number.
353  * @return Frame data with frame processing results.
354  */
355  virtual std::shared_ptr<::bnb::interfaces::frame_data> process_video_frame_data(const std::shared_ptr<::bnb::interfaces::frame_data> & input_frame_data, std::optional<int32_t> recognizer_iterations) = 0;
356 
357  /**
358  * Provide frame to evaluate video processing.
359  * MUST be called from the render thread
360  * @param input_image Image to process.
361  * @param params Processing params.
362  * @param recognizer_iterations 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  virtual std::shared_ptr<::bnb::interfaces::frame_data> process_video_frame(::bnb::full_image_t input_image, std::optional<int32_t> recognizer_iterations) = 0;
366 
367  /**
368  * Draw processed frame to image buffer.
369  * MUST be called from the render thread
370  * @param input_frame_data FrameData to draw.
371  * @param time_ns Frame position on timeline.
372  * @param output_pixel_format Output image format.
373  * @return Buffer with processed image in selected format.
374  */
375  virtual ::bnb::data_t draw_video_frame(const std::shared_ptr<::bnb::interfaces::frame_data> & input_frame_data, int64_t time_ns, ::bnb::interfaces::pixel_format output_pixel_format) = 0;
376 
377  /**
378  * The same as *draw_video_frame* but accepts preallocated buffer to avoid recreation for every frame.
379  * MUST be called from the render thread
380  */
381  virtual ::bnb::data_t draw_video_frame_allocated(const std::shared_ptr<::bnb::interfaces::frame_data> & input_frame_data, int64_t time_ns, ::bnb::interfaces::pixel_format output_pixel_format, ::bnb::data_t allocated_buffer) = 0;
382 
383  /**
384  * Save recorded audio.
385  * Not thread-safe but can be called from any thread.
386  * @param filename ".wav" extension must be used.
387  * @param length_ms Track length. Set to '0' to use full duration.
388  */
389  virtual void write_recorded_audio(const std::string & filename, int64_t length_ms) = 0;
390 
391  /**
392  * Trigger record start events in EP and Effect.
393  * Records all sounds during recording
394  * to be replayed by `processRecordedAudio`.
395  * If @param play_audio_while_recording is true, the audio will continue to play. Otherwise, the audio will be muted.
396  * Thread-safe. May be called from any thread
397  */
398  virtual void on_video_record_start(bool play_audio_while_recording) = 0;
399 
400  /**
401  * Triggers record stop events in EP and Effect.
402  * Thread-safe. May be called from any thread
403  */
404  virtual void on_video_record_end() = 0;
405 
406  /**
407  * Get effect manager object
408  * Thread-safe. May be called from any thread
409  */
410  virtual std::shared_ptr<::bnb::interfaces::effect_manager> effect_manager() = 0;
411 
412  /**
413  * Force recognizer offline mode
414  * Thread-safe. May be called from any thread
415  */
416  virtual void set_recognizer_offline_mode(bool on) = 0;
417 
418  /**
419  * Set future frame filtrarion mode.
420  * Produce smoother recognition result (anti jitter), however adds inconsistency in push'ed/draw'ed frames (one frame lag)
421  * Example: push frame 1 - draw frame 1, push frame 2 - draw frame 1, push frame 3 - draw frame 2, ...
422  */
423  virtual void set_recognizer_use_future_filter(bool on) = 0;
424 
425  /**
426  * Set future frame interpolation mode.
427  * Produce faster recognition result (skip even frames), however adds inconsistency in push'ed/pop'ed frames (one frame lag)
428  * Example: push frame 1 - pop frame 1, push frame 2 - pop frame 1, push frame 3 - pop frame 2, ...
429  */
430  virtual void set_recognizer_use_future_interpolate(bool on) = 0;
431 
432  /**
433  * Set frame processor as current
434  * Thread-safe. May be called from any thread
435  */
436  virtual void set_frame_processor(const std::shared_ptr<::bnb::interfaces::frame_processor> & processor) = 0;
437 
438  /**
439  * Get current frame processor
440  * Thread-safe. May be called from any thread
441  */
442  virtual std::shared_ptr<::bnb::interfaces::frame_processor> frame_processor() = 0;
443 
444  static void set_render_backend(::bnb::interfaces::render_backend_type backend_type);
445 
446  static ::bnb::interfaces::render_backend_type get_current_render_backend_type();
447 };
448 
449 } } // namespace bnb::interfaces
450 /// @}
451 
bnb::interfaces::effect_manager
Definition: effect_manager.hpp:27
bnb::interfaces::frame_processor
Definition: frame_processor.hpp:21
frame_data.hpp
bnb::interfaces::effect_player_playback_state
effect_player_playback_state
The EffectPlayerPlaybackState enum values are available states of effect player playback life cycle.
Definition: effect_player_playback_state.hpp:15
bnb::interfaces::effect_info_listener
Callback interface to receive effect info changes.
Definition: effect_info_listener.hpp:17
bnb::interfaces::frame_duration_listener
Callback interface to receive FPS timings.
Definition: frame_duration_listener.hpp:15
bnb::interfaces::consistency_mode
consistency_mode
These modes describe the relationship between processing loop and render loop frames.
Definition: consistency_mode.hpp:34
pixel_format.hpp
bnb::interfaces::camera_poi_listener
Callback to get center of square bounding the face.
Definition: camera_poi_listener.hpp:15
bnb::interfaces::face_number_listener
Callback to get face number detected.
Definition: face_number_listener.hpp:16
bnb::interfaces::frame_data
getters throw exceptions when data are not available android NNs usually output gpu masks
Definition: frame_data.hpp:42
bnb::interfaces::frame_data_listener
Callback to get freshly processed frame_data.
Definition: frame_data_listener.hpp:17
bnb::interfaces::effect_player
The EffectPlayer class provides ability to play AR effects on set of images or video.
Definition: effect_player.hpp:85
bnb::interfaces::effect_activation_completion_listener
Callback interface to receive effect activation notification.
Definition: effect_activation_completion_listener.hpp:16
pixel_buffer.hpp