]> git.sesse.net Git - nageru-docs/blob - streaming.rst
Drop documentation of very old versions.
[nageru-docs] / streaming.rst
1 Streaming and recording
2 =======================
3
4 One of the primary purposes of Nageru is to prepare a stream
5 that is suitable for live broadcast. However, it is *not*
6 suitable for serving end-users directly; other software is
7 much more suitable.
8
9 Depending on the amount of CPU power and bandwidth in your
10 production machine (the one you run Nageru on) and other
11 machines you may have available, you can choose two different
12 approaches for streaming: **Transcoded** or **direct**.
13
14
15 .. _transcoded-streaming:
16
17 Transcoded streaming
18 --------------------
19
20 Transcoded streaming is in many ways the conceptually simplest from Nageru's point of
21 view. In this mode, Nageru outputs its “digital intermediate”
22 H.264 stream (see :ref:`digital-intermediate`), and you are
23 responsible for transcoding it into a format that is suitable
24 for streaming to clients. Thus, you can run Nageru on a regular
25 laptop and then use e.g. a short-term virtual machine in the cloud
26 to do the heavy lifting for video compression.
27
28 Nageru has a built-in HTTP server that listens on port 9095.
29 You can get the intermediate by using any filename; by default,
30 the NUT mux is used, so e.g. “http://yourserver.example.org:9095/stream.nut”
31 would be appropriate. The NUT mux is chosen because it is among
32 the few that can carry uncompressed audio easily. It is private
33 to FFmpeg, and thus understood by almost everything in the free
34 software world, but perhaps not by all other services. You can
35 change it with the “--http-mux” parameter, although you might
36 then also want to change the audio codec to using “--http-audio-codec”
37 and “--http-audio-bitrate” to something your mux can transport
38 (see below for more information about audio transcoding).
39
40 The stream be transcoded by a number of programs, most notably
41 `VLC <http://www.videolan.org/>`_. Here's an example line
42 transcoding to 1.5 Mbit/sec H.264 suitable for streaming to
43 most browsers in the \<video\> tag::
44
45   while :; do
46     vlc -I dummy -v --network-caching 3000 \
47       http://yourserver.example.org:9095/stream.nut vlc://quit \
48       --sout '#transcode{vcodec=h264,vb=1500,acodec=mp4a,aenc=fdkaac,ab=128}:std{mux=ffmpeg{mux=mp4},access=http{mime=video/mp4},dst=:1994}' \
49       --sout-avformat-options '{movflags=empty_moov+frag_keyframe+default_base_moof}' \
50       --sout-x264-vbv-maxrate 1500 --sout-x264-vbv-bufsize 1500 \
51       --sout-x264-keyint 50 --sout-x264-tune film --sout-x264-preset slow \
52       --sout-mux-caching 3000
53     sleep 1
54   done
55
56 (The for loop is to restart VLC if Nageru should be restarted.
57 You can make similar loops around the other example scripts,
58 or you can e.g. make systemd units for transcoding if you wish.)
59
60 Of course, you may need to adjust the bitrate (and then also
61 the VBV settings) and preset for your content and CPU usage.
62 1.5 Mbit/sec is in the lower end of the spectrum for good
63 720p60 conference video (most TV channels use 12-15 Mbit/sec
64 for the same format).
65
66 Another variation popular today is to stream using segmented HTTP;
67 you can use e.g. the ffmpeg command-line tool to segment the HLS
68 created by VLC into a stream that will be accepted by most smartphones::
69
70   ffmpeg -i http://127.0.0.1:1994/ -codec copy -f hls \
71     -hls_time 2 -hls_wrap 100 -bsf:v h264_mp4toannexb \
72     -hls_segment_filename $NAME-hls-%03d.ts stream.m3u8
73
74 See also the section on :ref:`Kaeru <kaeru>`, below.
75
76
77 .. _direct-encoding:
78
79 Direct encoding
80 ---------------
81
82 If you do not wish to run an external transcoder, Nageru can
83 encode high-quality H.264 video directly using
84 `x264 <http://www.videolan.org/developers/x264.html>`_.
85 This requires much more CPU power (a fast quadcore is recommended
86 for a 720p60 stream), since it does not have any assistance from
87 the GPU, but produces significantly better quality per bit,
88 and also has much better bitrate control. Even if you use x264,
89 the stream stored to disk is still the full-quality QSV stream.
90
91 Using Nageru's built-in x264 support is strongly preferable to
92 running VLC on the same machine, since it saves one H.264 decoding
93 step, and also uses *speed control*. Speed control automatically
94 turns x264's quality setting up and down to use up all remaining
95 CPU power after Nageru itself has taken what it needs (but no more);
96 it can use a range of settings all the way from about x264's
97 “superfast” preset all the way to about the equivalent of “veryslow”.
98 Note that this comes at the cost of about one second of extra delay.
99
100 Built-in x264 is also useful if you don't have a lot of bandwidth
101 to your external encoding or distribution machine. You can, however,
102 still transcode externally to get e.g. a lower-resolution for low-bandwidth
103 users or segmented HLS; see the previous section for examples.
104
105 The built-in x264 encoder is activated using the “--http-x264-video”
106 flag; e.g.::
107
108   ./nageru --http-x264-video --x264-preset veryfast --x264-tune film \
109     --http-mux mp4 --http-audio-codec libfdk_aac --http-audio-bitrate 128
110
111 Note the use here of the MP4 mux and AAC audio. “libfdk_aac” signals
112 the use of Franhofer's `FDK-AAC <https://github.com/mstorsjo/fdk-aac>`_ encoder
113 from Android; it yields significantly better sound quality than e.g. FAAC,
114 and it is open source, but under a somewhat cumbersome license. For this
115 reason, most distributions do not compile FFmpeg with the FDK-AAC codec,
116 so you will need to compile FFmpeg yourself, or use a worse codec.
117 FFmpeg `recommends <https://trac.ffmpeg.org/wiki/Encode/HighQualityAudio>`_
118 their own native AAC encoder (simply called “aac”) in the absence of any
119 external libraries.
120
121 For speed control, you can use::
122
123   ./nageru --x264-speedcontrol --x264-tune film \
124     --http-mux mp4 --http-audio-codec libfdk_aac --http-audio-bitrate 128
125
126 There are many more parameters, in particular “--x264-bitrate” to control
127 the nominal bitrate (4500 kbit/sec per default, plus audio). Most of them
128 are usually fine at the default, though. Note that you can change the
129 x264 bitrate on-the-fly from the video menu; this is primarily useful
130 if your network conditions change abruptly.
131
132 A particular note about the MP4 mux: If you plan to stream for long periods
133 continuously (more than about 12–24 hours), the 32-bit timestamps may wrap
134 around with the default timebase Nageru is using. If so, please add the
135 “--http-coarse-timebase” flag.
136
137
138 .. _kaeru:
139
140 Transcoding with Kaeru
141 ----------------------
142
143 There is a third option that combines elements from the two previous
144 approaches: Nageru includes **Kaeru**, named after the
145 Japanese verb *kaeru* (換える), meaning roughly to replace or exchange.
146 Kaeru is a command-line tool that is designed to transcode Nageru's streams.
147 In that aspect, it is similar to using VLC as described in the section on
148 :ref:`transcoded streaming <transcoded-streaming>`. However, since it reuses
149 Nageru's decoding and encoding code, it can do almost everything you can do
150 with :ref:`direct encoding <direct-encoding>`, including x264 speed control
151 and Metacube output (see the section on :ref:`Cubemap integration <cubemap>` below).
152
153 Using Kaeru is similar to launching Nageru, e.g. to rescale a stream to 848x480::
154
155   ./kaeru -w 848 -h 480 --http-mux mp4 --http-audio-codec libfdk_aac --http-audio-bitrate 128 \
156     http://yourserver.example.org:9095/stream.nut
157
158 Unlike the VLC option, Kaeru will automatically reconnect if the source goes
159 away, so you do not need a while loop. Note that if you want to keep the audio
160 unchanged (usually audio is more important than video, so it's not uncommon to
161 want to transcode video but not audio), you can do so with the flag
162 *--no-transcode-audio* flag.
163
164 There are some things you should keep in mind about Kaeru:
165
166   - Kaeru is currently quite experimental, and much less tested than Nageru itself.
167   - Kaeru does not use the GPU; it uses FFmpeg's code for scaling and colorspace
168     conversion. This yields somewhat lower scaling quality and uses more CPU power,
169     but means you can run it on a headless server.
170   - There is no support for encoding using Quick Sync Video, since this depends on the GPU.
171   - 10-bit output is currently not supported.
172
173
174 .. _cubemap:
175
176 Cubemap integration
177 -------------------
178
179 Even with built-in x264 support, Nageru is not particularly efficient
180 for delivering streams to end users. For this, a natural choice is
181 `Cubemap <http://cubemap.sesse.net/>`__; Cubemap scales without problems
182 to multiple 10 Gbit/sec NICs on a quite normal machine, and you can easily
183 add multiple Cubemap servers if so needed. Nageru has native support for
184 Cubemap's *Metacube2* transport encoding; simply add “.metacube” to
185 to the end of the URL, e.g. with a cubemap.config fragment like this::
186
187   stream /stream.mp4 src=http://yourserver.example.org:9094/stream.mp4.metacube pacing_rate_kbit=3000 force_prebuffer=1500000
188
189 Note that you will want a pacing rate of about 2:1 to your real average
190 bitrate, in order to provide some for temporary spikes (the default allows
191 spikes of 2x the nominal bitrate, but only on a one-second basis) and
192 TCP retransmits. See the Cubemap documentation for more information about
193 how to set up pacing.
194
195 For transcoded Cubemap output from VLC you can take exactly the same line as
196 earlier, just adding “metacube” to the HTTP options::
197
198   while :; do
199     vlc -I dummy -v --network-caching 3000 \
200       http://http://yourserver.example.org:9095/stream.nut vlc://quit \
201       --sout '#transcode{vcodec=h264,vb=1500,acodec=mp4a,aenc=fdkaac,ab=128}:std{mux=ffmpeg{mux=mp4},access=http{mime=video/mp4,metacube},dst=:1994}' \
202       --sout-avformat-options '{movflags=empty_moov+frag_keyframe+default_base_moof}' \
203       --sout-x264-vbv-maxrate 1500 --sout-x264-vbv-bufsize 1500 \
204       --sout-x264-keyint 50 --sout-x264-tune film --sout-x264-preset slow \
205       --sout-mux-caching 3000
206     sleep 1
207   done
208
209
210 Single-camera stream
211 --------------------
212
213 In addition to the regular mixed stream, you can
214 siphon out MJPEG streams consisting of a single camera only. This is useful
215 either for running a very cheap secondary stream (say, a static overview camera
216 that you would like to show on a separate screen somewhere), or for simple
217 monitoring during debugging.
218
219 The URL for said stream is “http://yourserver.example.org:9095/feeds/N.mp4”,
220 where N is the card index (starting from zero). The feed is in MJPEG format
221 and MP4 mux, regardless of other settings, just like the multicamera mux
222 for Futatabi. (You are allowed to use a card that is not part of the
223 multicamera mux, if you have limited the number of such cards.) For
224 more technical details, see :ref:`futatabiformat`. Kaeru can transcode such
225 streams to a more manageable bitrate/format, if you wish.