]> git.sesse.net Git - nageru-docs/blob - streaming.rst
Small syntax fix.
[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
18 Transcoded streaming was the only option supported before 1.3.0,
19 and in many ways the conceptually simplest from Nageru's point of
20 view. In this mode, Nageru outputs its “digital intermediate”
21 H.264 stream (see :ref:`digital-intermediate`), and you are
22 responsible for transcoding it into a format that is suitable
23 for streaming to clients. Thus, you can run Nageru on a regular
24 laptop and then use e.g. a short-term virtual machine in the cloud
25 to do the heavy lifting for video compression.
26
27 Nageru has a built-in HTTP server that listens on port 9095.
28 You can get the intermediate by using any filename; by default,
29 the NUT mux is used, so e.g. “http://yourserver.example.org:9095/stream.nut”
30 would be appropriate. The NUT mux is chosen because it is among
31 the few that can carry uncompressed audio easily. It is private
32 to FFmpeg, and thus understood by almost everything in the free
33 software world, but perhaps not by all other services. You can
34 change it with the “--http-mux” parameter, although you might
35 then also want to change the audio codec to using “--http-audio-codec”
36 and “--http-audio-bitrate” to something your mux can transport
37 (see below for more information about audio transcoding).
38
39 The stream be transcoded by a number of programs, most notably
40 `VLC <http://www.videolan.org/>`_. Here's an example line
41 transcoding to 1.5 Mbit/sec H.264 suitable for streaming to
42 most browsers in the \<video\> tag::
43
44   while :; do
45     vlc -I dummy -v --network-caching 3000 \
46       http://http://yourserver.example.org:9095/stream.nut vlc://quit \
47       --sout '#transcode{vcodec=h264,vb=1500,acodec=mp4a,aenc=fdkaac,ab=128}:std{mux=ffmpeg{mux=mp4},access=http{mime=video/mp4},dst=:1994}' \
48       --sout-avformat-options '{movflags=empty_moov+frag_keyframe+default_base_moof}' \
49       --sout-x264-vbv-maxrate 1500 --sout-x264-vbv-bufsize 1500 \
50       --sout-x264-keyint 50 --sout-x264-tune film --sout-x264-preset slow \
51       --sout-mux-caching 3000
52     sleep 1
53   done
54
55 (The for loop is to restart VLC if Nageru should be restarted.
56 You can make similar loops around the other example scripts,
57 or you can e.g. make systemd units for transcoding if you wish.)
58
59 Of course, you may need to adjust the bitrate (and then also
60 the VBV settings) and preset for your content and CPU usage.
61 1.5 Mbit/sec is in the lower end of the spectrum for good
62 720p60 conference video (most TV channels use 12-15 Mbit/sec
63 for the same format).
64
65 Another variation popular today is to stream using segmented HTTP;
66 you can use e.g. the ffmpeg command-line tool to segment the HLS
67 created by VLC into a stream that will be accepted by most smartphones::
68
69   ffmpeg -i http://127.0.0.1:1994/ -codec copy -f hls \
70     -hls_time 2 -hls_wrap 100 -bsf:v h264_mp4toannexb \
71     -hls_segment_filename $NAME-hls-%03d.ts stream.m3u8
72
73
74 Direct encoding
75 ---------------
76
77 If you do not wish to run an external transcoder, Nageru can
78 encode high-quality H.264 video directly using
79 `x264 <http://www.videolan.org/developers/x264.html>`_.
80 This requires much more CPU power (a fast quadcore is recommended
81 for a 720p60 stream), since it does not have any assistance from
82 the GPU, but produces significantly better quality per bit,
83 and also has much better bitrate control. Even if you use x264,
84 the stream stored to disk is still the full-quality QSV stream.
85
86 Using Nageru's built-in x264 support is strongly preferable to
87 running VLC on the same machine, since it saves one H.264 decoding
88 step, and also uses *speed control*. Speed control automatically
89 turns x264's quality setting up and down to use up all remaining
90 CPU power after Nageru itself has taken what it needs (but no more);
91 it can use a range of settings all the way from about x264's
92 “superfast” preset all the way to about the equivalent of “veryslow”.
93 Note that this comes at the cost of about one second of extra delay.
94
95 Built-in x264 is also useful if you don't have a lot of bandwidth
96 to your external encoding or distribution machine. You can, however,
97 still transcode externally to get e.g. a lower-resolution for low-bandwidth
98 users or segmented HLS; see the previous section for examples.
99
100 The built-in x264 encoder is activated using the “--http-x264-video”
101 flag; e.g.::
102
103   ./nageru --http-x264-video --x264-preset veryfast --x264-tune film \
104     --http-mux mp4 --http-audio-codec libfdk_aac --http-audio-bitrate 128
105
106 Note the use here of the MP4 mux and AAC audio. “libfdk_aac” signals
107 the use of Franhofer's `FDK-AAC <https://github.com/mstorsjo/fdk-aac>`_ encoder
108 from Android; it yields significantly better sound quality than e.g. FAAC,
109 and it is open source, but under a somewhat cumbersome license. For this
110 reason, most distributions do not compile FFmpeg with the FDK-AAC codec,
111 so you will need to compile FFmpeg yourself, or use a worse codec.
112 FFmpeg `recommends <https://trac.ffmpeg.org/wiki/Encode/HighQualityAudio>`_
113 their own native AAC encoder (simply called “aac”) in the absence of any
114 external libraries.
115
116 For speed control, you can use::
117
118   ./nageru --x264-speedcontrol --x264-tune film \
119     --http-mux mp4 --http-audio-codec libfdk_aac --http-audio-bitrate 128
120
121 There are many more parameters, in particular “--x264-bitrate” to control
122 the nominal bitrate (4500 kbit/sec per default, plus audio). Most of them
123 are usually fine at the default, though. Note that you can change the
124 x264 bitrate on-the-fly from the video menu; this is primarily useful
125 if your network conditions change abruptly.
126
127 A particular note about the MP4 mux: If you plan to stream for long periods
128 continuously (more than about 12–24 hours), the 32-bit timestamps may wrap
129 around with the default timebase Nageru is using. If so, please add the
130 “--http-coarse-timebase” flag.
131
132
133 Cubemap integration
134 -------------------
135
136 Even with built-in x264 support, Nageru is not particularly efficient
137 for delivering streams to end users. For this, a natural choice is
138 `Cubemap <http://cubemap.sesse.net/>`_; Cubemap scales without problems
139 to multiple 10 Gbit/sec NICs on a quite normal machine, and you can easily
140 add multiple Cubemap servers if so needed. Nageru has native support for
141 Cubemap's *Metacube2* transport encoding; simply add “.metacube” to
142 to the end of the URL, e.g. with a cubemap.config fragment like this::
143
144   stream /stream.mp4 src=http://yourserver.example.org:9094/stream.mp4.metacube pacing_rate_kbit=3000 force_prebuffer=1500000
145
146 Note that you will want a pacing rate of about 2:1 to your real average
147 bitrate, in order to provide some for temporary spikes (the default allows
148 spikes of 2x the nominal bitrate, but only on a one-second basis) and
149 TCP retransmits. See the cubemap documentation for more information about
150 how to set up pacing.
151
152 For transcoded Cubemap output from VLC you can take exactly the same line as
153 earlier, just adding “metacube” to the HTTP options::
154
155   while :; do
156     vlc -I dummy -v --network-caching 3000 \
157       http://http://yourserver.example.org:9095/stream.nut vlc://quit \
158       --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}' \
159       --sout-avformat-options '{movflags=empty_moov+frag_keyframe+default_base_moof}' \
160       --sout-x264-vbv-maxrate 1500 --sout-x264-vbv-bufsize 1500 \
161       --sout-x264-keyint 50 --sout-x264-tune film --sout-x264-preset slow \
162       --sout-mux-caching 3000
163     sleep 1
164   done
165