]> git.sesse.net Git - nageru-docs/blob - theme.rst
Syntax fix; Movit effects are italics, not bold.
[nageru-docs] / theme.rst
1 The theme
2 =========
3
4 In Nageru, most of the business logic around how your stream
5 ends up looking is governed by the **theme**, much like how a
6 theme works on a blog or a CMS. Most importantly, the theme
7 governs the look and feel through the different scenes and
8 transitions between them, such that an event gets a consistent
9 visual language even if the operators differ. Instead of requiring the user
10 to modify Nageru's C++ core, themes are written in
11 `Lua <https://www.lua.org/>`_, a lightweight scripting language
12 made for embedding.
13
14 Themes contain a lot of logic, and writing one can seem a bit
15 daunting at first. However, most events will be happy just tweaking
16 one of the themes included with Nageru, and the operator (if different
17 from the visual designer) will not need to worry at all.
18
19 Nageru ships with two themes, a default full-featured two-camera
20 setup with side-by-side for e.g. conferences, and a minimal one
21 that is easier for new users to understand.
22
23
24 Introduction to chains
25 ----------------------
26
27 Anything that's shown on the stream, or on one of the preview displays,
28 is created by a **Movit chain**. `Movit <https://movit.sesse.net/>`_
29 is a library for high-quality, high-performance video filters,
30 and Nageru's themes can use a simplified version of Movit's API where
31 most of the low-level details are abstracted away.
32
33 Every frame, the theme choses a chain and a set of parameters to it,
34 based on what it thinks the picture should look like. Every chain
35 consists of a set of *inputs* (which can be either live video streams
36 or static pictures) and then a set of operators or *effects* to combine
37 or modify each other. Movit compiles these down to a set of shaders
38 that run in high speed on the GPU; the theme doesn't see a pixel,
39 and thus, Lua's performance (even though good for its language class,
40 especially if you use `LuaJIT <http://luajit.org/>`_) will not matter
41 much.
42
43
44 High- and low-quality chains
45 ----------------------------
46
47 The simplest possible chain takes only in an input and sends it on
48 to the display (the output of the last added node is always sent to
49 the screen, and in this case, that would be the input)::
50
51   local chain = EffectChain.new(16, 9)  -- Aspect ratio.
52   local input = chain:add_live_input(false, false)  -- No bounce override, no deinterlacing.
53   input:connect_signal(0)  -- First input card. Can be changed whenever you want.
54   chain:finalize(hq)
55
56 Note the “hq” parameter. Every chain needs to be able to run in two
57 situations: Both for the stream output (the ”live” pane) and for the preview
58 displays (the big “preview” pane and the smaller ones). The details have
59 to do with Nageru internals (high-quality chains need to have an additional
60 Y'CbCr output), but the distinction is also useful for themes. In particular,
61 some operations, like scaling, can be done in various quality levels,
62 and for a low-resolution preview display, you don't need maximum quality.
63 Thus, in a preview chain (hq=false), you can safely take shortcuts.
64
65 The live chain is always processed in full resolution (typically 720p)
66 and then scaled down for the GUI. Preview chains are rendered in exactly
67 the resolution required, although of course, intermediate steps could be
68 bigger.
69
70 Setting parameters, and the get_chain entry point
71 -------------------------------------------------
72
73 Many effects support parameters that can vary per-frame. Imagine,
74 for instance, a theme where you want to supports two inputs and fading between
75 them. This means you will need a chain that produces two inputs and
76 produces a mix of them; Movit's *MixEffect* is exactly what you want here::
77
78   local chain = EffectChain.new(16, 9)
79
80   local input0 = chain:add_live_input(false, false)
81   input0:connect_signal(0)
82   local input1 = chain:add_live_input(false, false)
83   input1:connect_signal(1)
84
85   local mix_effect = chain:add_effect(MixEffect.new(), input0, input1)
86   chain:finalize(hq)
87
88 Every frame, Movit will call your **get_chain** function, which has
89 this signature:
90
91   function get_chain(num, t, width, height, signals)
92
93 “width” and “height” are what you'd expect (the output resolution).
94 t contains the current stream time in seconds. “num” contains 0
95 for the live view, 1 for the preview view, and 2, 3, 4, … for each
96 of the individual stream previews. “signals“ contains a bit of
97 information about each input signal, like its current resolution
98 or frame rate.
99
100 get_chain is in turn responsible for returning two values:
101
102   * The first return value is a Movit chain, as described in these
103     sections. For the live stream (num=0), you should return a high-quality
104     chain; for all others, you should return a low-quality chain.
105   * The second parameter is an *closure* that will be called just before
106     the chain is to be rendered. (The same chain could be used in
107     multiple OpenGL contexts at the same time, so you can't just set the values
108     immediately before returning. If you set them in the closure,
109     Nageru and Movit will deal with all the required threading for you.)
110
111 In the returned closure, you can set the parameters **strength_first**
112 and **strength_second**; for instance like this::
113
114   function get_chain(num, t, width, height, signals)
115     -- Assume num is 0 here; you will need to handle the other
116     -- cases, too.
117     prepare = function()
118       input0:connect_signal(0)
119       input1:connect_signal(1)
120
121       local fade_progress = 0.0
122       if t >= 1.0 and t >= 2.0:  -- Between 1 and 2 seconds; do the fade.
123         fade_progress = t - 1.0
124       elseif t >= 2.0:
125         fade_progress = 1.0
126       end
127
128       mix_effect:set_float("strength_first", 1.0 - fade_progress)
129       mix_effect:set_float("strength_second", fade_progress)
130     end
131     return chain, prepare
132   end
133
134 Note that in the case where fade_progress is 0.0 or 1.0 (you are just
135 showing one of the inputs), you are wasting GPU power by using the
136 fade chain; you should just return a simpler one-input chain instead.
137
138 The get_chain function is the backbone of every Nageru theme.
139 As we shall see, however, it may end up dealing with a fair bit
140 of complexity as the theme grows.
141
142
143 Chain precalculation
144 --------------------
145
146 Setting up and finalizing a chain is relatively fast, but it still
147 takes a measurable amount of CPU time, since it needs to create an OpenGL
148 shader and have it optimized by the driver; 50–100 ms is not uncommon.
149 Given that 60 fps means each frame is 16.7 ms, you cannot create new chains in
150 get_chain; every chain you could be using must be created at program start,
151 when your theme is initialized.
152
153 For any nontrivial theme, there are a lot of possible chains. Let's
154 return to the case of the MixEffect chain from the previous section.
155 Now let us assume that we could deal with signals that come in at
156 1080p instead of the native 720p. In this case, we will want a high-quality
157 scaler before mixing; *ResampleEffect* provides one::
158
159   local chain = EffectChain.new(16, 9)
160
161   local input0 = chain:add_live_input(false, false)
162   input0:connect_signal(0)
163   local input0_scaled = chain:add_effect(ResampleEffect.new())  -- Implicitly uses input0.
164   chain_or_input.resample_effect:set_int("width", 1280)  -- Would normally be set in the prepare function.
165   chain_or_input.resample_effect:set_int("height", 720)
166
167   local input1 = chain:add_live_input(false, false)
168   input1:connect_signal(1)
169
170   -- The rest is unchanged.
171
172 Clearly, there are four options here; both inputs could be unscaled,
173 input0 could be scaled but not input1, input1 could be scaled but not input0,
174 or both could be scaled. That means four chains.
175
176 Now remember that we need to create all your chains both in high-
177 and low-quality versions. In particular, this determines the “hq”
178 parameter to finalize(), but in our case, we would want to replace
179 ResampleEffect by *ResizeEffect* (a simpler scaling algorithm provided
180 directly by the GPU) for the low-quality versions. This makes for
181 eight chains.
182
183 Now also consider that we would want to deal with *interlaced*
184 inputs. (You can check if you get an interlaced input on the Nth
185 input by calling “signals:get_deinterlaced(n)” from get_chain.)
186 This further quadruples the number of chains you'd need to write,
187 and this isn't even including that you'd want the static chains.
188 It is obvious that this should not be done by hand. The default
189 included theme contains a handy Lua shortcut called
190 **make_cartesian_product** where you can declare all the dimensions
191 you would want to specialize your chain over, and have a callback
192 function called for each possible combination. Movit will make sure
193 each and every of those generated chains runs optimally on your GPU.
194
195
196 Transitions
197 -----------
198
199 As we have seen, the theme is king when it determines what to show
200 on screen. However, ultimately, it wants to delegate that power
201 to the operator. The abstraction presented from the theme to the user
202 is in the form of **transitions**. Every frame, Nageru calls the
203 following Lua entry point::
204
205   function get_transitions(t)
206
207 (t is again the stream time, but it is provided only for convenience;
208 not all themes would want to use it.) get_transitions must return an array of
209 (currently exactly) three strings, of which any can be blank. These three
210 strings are used as labels on one button each, and whenever the operator clicks
211 one of them, Nageru calls this function in the theme::
212
213   function transition_clicked(num, t)
214
215 where “num” is 0, 1 or 2, and t is again the theme time.
216
217 It is expected that the theme will use this and its internal state
218 to provide the abstraction (or perhaps illusion) of transitions to
219 the user. For instance, a theme will know that the live stream is
220 currently showing input 0 and the preview stream is showing input 1.
221 In this case, it can use two of the buttons to offer “Cut“ or “Fade”
222 transitions to the user. If the user clicks the cut button, the theme
223 can simply switch input and previews, which will take immediate
224 effect on the next frame. However, if the user clicks the fade button,
225 state will need to be set up so that next time get_chain() runs,
226 it will return the chain with the MixEffect, until it determines
227 the transition is over and changes back to showing only one input
228 (presumably the new one).
229
230
231 Channels
232 --------
233
234 In addition to the live and preview outputs, a theme can declare
235 as many individual **channels** as it wants. These are shown at the
236 bottom of the screen, and are intended for the operator to see
237 what they can put up on the preview (in a sense, a preview of the
238 preview).
239
240 The number of channels is determined by calling this function
241 once at the start of the program::
242
243   function num_channels()
244
245 It should simply return the number of channels (0 is allowed,
246 but doesn't make a lot of sense). Live and preview comes in addition to this.
247
248 Each channel will have a label on it; Nageru asks the theme
249 by calling this function::
250
251   function channel_name(channel)
252
253 Here, channel is 2, 3, 4, etc.—0 is always called “Live” and
254 1 is always called “Preview”.
255
256 Each channel has its own chain, starting from number 2 for the first one
257 (since 0 is live and 1 is preview). The simplest form is simply a direct copy
258 of an input, and most themes will include one such channel for each input.
259 (Below, we will see that there are more types of channels, however.)
260 Since the mapping between the channel UI element and inputs is so typical,
261 Nageru allows the theme to simply declare that a channel corresponds to
262 a given signal, by asking it::
263
264   function channel_signal(channel)
265     if channel == 2 then
266       return 0
267     elseif channel == 3 then
268       return 1
269     else
270       return -1
271     end
272   end
273
274 Here, channels 2 and 3 (the two first ones) correspond directly to inputs
275 0 and 1, respectively. The others don't, and return -1. The effect on the
276 UI is that the user can right-click on the channel and configure the input
277 that way; in fact, this is currently the only way to configure them.
278
279 Furthermore, channels can have a color::
280
281   function channel_color(channel)
282  
283 The theme should return a CSS color (e.g. “#ff0000”, or “cyan”) for each
284 channel when asked; it can vary from frame to frame. A typical use is to mark
285 the currently playing input as red, or the preview as green.
286
287 And finally, there are two entry points related to white balance::
288
289   function supports_set_wb(channel)
290   function set_wb(channel, red, green, blue)
291
292 If the first function returns true (called once, at the start of the program),
293 the channel will get a “Set WB” button next to it, which will activate a color
294 picker. When the user picks a color (ostensibly with a gray point), the second
295 function will be called (with the RGB values in linear light—not sRGB!),
296 and the theme can then use it to adjust the white balance for that channel.
297 The typical way to to this is to have a *WhiteBalanceEffect* on each input
298 and set its “neutral_color” parameter using the “set_vec3” function.
299
300
301 More complicated channels: Scenes
302 ---------------------------------
303
304 Direct inputs are not the only kind of channels possible; again, any chain
305 can be output. The most common case is different kinds of **scenes**,
306 typically showing side-by-side or something similar. The typical UI presented
307 to the user in this case is that you create a channel that consists of the
308 finished setup; you use ResampleEffect (or ResizeEffect for low-quality chains),
309 *PaddingEffect* (to place the rectangles on the screen, one of them with a
310 transparent border) and then *OverlayEffect* (to get both on the screen at
311 the same time). Optionally, you can have a background image at the bottom,
312 and perhaps a logo at the top. This allows the operator to select a pre-made
313 scene, and then transition to and from it from a single camera view (or even
314 between different scenes) as needed.
315
316 Transitions involving scenes tend to be the most complicated parts of the theme
317 logic, but also make for the most distinct parts of your visual look.
318
319
320 Image inputs
321 ------------
322
323 In addition to video inputs, Nageru supports static **image inputs**.
324 These work pretty much the same way as live video inputs; however,
325 they need to be instantiated in a different way. Recall that live inputs
326 were created like this::
327
328   input = chain:add_live_input(false, deint)
329
330 Image inputs are instead created by instantiating *ImageInput* and
331 adding them manually to the chain::
332
333   input = chain:add_effect(ImageInput.new("bg.jpeg"))
334
335 Note that add_effect returns its input for convenience.
336
337 All image types supported by FFmpeg are supported; if you give in a video,
338 only the first frame is used. The file is checked once every second,
339 so if you update the file on-disk, it will be available in Nageru without
340 a restart. (If the file contains an error, the update will be ignored.)
341 This allows you to e.g. have simple message overlays that you can change
342 without restarting Nageru.