1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//! Internal live HLS state — all `unsafe` `FFmpeg` calls live here.
//!
//! [`LiveHlsInner`] owns a [`MuxerCore`] and the HLS segment duration. It is
//! created by [`crate::live_hls::LiveHlsOutput::build`] and driven by the
//! safe wrappers in [`crate::live_hls`].
//!
//! Public methods on `LiveHlsInner` are safe; all raw `FFmpeg` calls are
//! confined to `unsafe {}` blocks inside this file.
// This module is intentionally unsafe — it drives the FFmpeg C API directly.
#![allow(unsafe_code)]
#![allow(clippy::ptr_as_ptr)]
#![allow(clippy::cast_possible_wrap)]
#![allow(clippy::cast_sign_loss)]
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::borrow_as_ptr)]
#![allow(clippy::ref_as_ptr)]
#![allow(clippy::too_many_lines)]
use std::ffi::CString;
use std::path::Path;
use std::ptr;
use ff_format::{AudioFrame, VideoFrame};
use ff_sys::{
AVCodecContext, AVPixelFormat_AV_PIX_FMT_YUV420P, av_opt_set, avformat_alloc_output_context2,
avformat_free_context, avformat_new_stream, avformat_write_header,
};
use crate::codec_utils::{ffmpeg_err, ffmpeg_err_msg};
use crate::error::StreamError;
use crate::muxer_core::MuxerCore;
// ============================================================================
// LiveHlsInner
// ============================================================================
/// Owns the shared `FFmpeg` muxer state for a live HLS output session.
///
/// Created by [`LiveHlsInner::open`]; consumed by [`LiveHlsInner::flush_and_close`].
/// After `flush_and_close` returns, calling any other method is undefined behaviour;
/// the safe wrapper in `live_hls.rs` prevents this via the `finished` guard.
pub(crate) struct LiveHlsInner {
core: MuxerCore,
}
// SAFETY: LiveHlsInner exclusively owns all FFmpeg contexts via MuxerCore.
unsafe impl Send for LiveHlsInner {}
impl LiveHlsInner {
/// Open all `FFmpeg` contexts and write the HLS header.
///
/// # Parameters
///
/// - `output_dir`: directory where `index.m3u8` and `.ts` segments are written.
/// - `segment_secs`: target HLS segment duration in seconds.
/// - `playlist_size`: maximum number of segments kept in the sliding playlist.
/// - `enc_width`, `enc_height`, `fps_int`: video encoder dimensions and frame rate.
/// - `video_bitrate`: video encoder bit rate in bits/s.
/// - `audio`: optional `(sample_rate, nb_channels, bit_rate)` tuple; `None` skips audio.
#[allow(clippy::too_many_arguments)]
pub(crate) fn open(
output_dir: &str,
segment_secs: u32,
playlist_size: u32,
enc_width: i32,
enc_height: i32,
fps_int: i32,
video_bitrate: u64,
audio: Option<(i32, i32, i64)>,
segment_format: crate::hls::HlsSegmentFormat,
) -> Result<Self, StreamError> {
// SAFETY: All FFmpeg resources are managed within this function; the
// returned LiveHlsInner takes exclusive ownership of every pointer.
unsafe {
Self::open_unsafe(
output_dir,
segment_secs,
playlist_size,
enc_width,
enc_height,
fps_int,
video_bitrate,
audio,
segment_format,
)
}
}
/// Encode and mux one video frame.
pub(crate) fn push_video(&mut self, frame: &VideoFrame) -> Result<(), StreamError> {
// SAFETY: self was initialised by open() and is not yet finished.
unsafe { self.core.push_video_unsafe(frame) }
}
/// Encode and mux one audio frame.
///
/// If audio was not configured at `open` time, this is a silent no-op.
pub(crate) fn push_audio(&mut self, frame: &AudioFrame) {
// SAFETY: self was initialised by open() and is not yet finished.
unsafe {
self.core.push_audio_unsafe(frame);
}
}
/// Flush both encoders and write the HLS trailer. Consumes `self`.
pub(crate) fn flush_and_close(mut self) {
// SAFETY: self was initialised by open(); flush_and_close is called once.
unsafe {
self.core.flush_and_close_unsafe();
}
}
// ── Private unsafe implementations ───────────────────────────────────────
#[allow(unsafe_op_in_unsafe_fn)]
#[allow(clippy::too_many_arguments)]
unsafe fn open_unsafe(
output_dir: &str,
segment_secs: u32,
playlist_size: u32,
enc_width: i32,
enc_height: i32,
fps_int: i32,
video_bitrate: u64,
audio: Option<(i32, i32, i64)>,
segment_format: crate::hls::HlsSegmentFormat,
) -> Result<Self, StreamError> {
ff_sys::ensure_initialized();
// ── 1. Allocate HLS output context ────────────────────────────────────
let playlist_path = format!("{output_dir}/index.m3u8");
let c_playlist = CString::new(playlist_path.as_str())
.map_err(|_| ffmpeg_err_msg("playlist path contains null byte"))?;
let mut out_ctx: *mut ff_sys::AVFormatContext = ptr::null_mut();
let ret = avformat_alloc_output_context2(
&mut out_ctx,
ptr::null_mut(),
c"hls".as_ptr(),
c_playlist.as_ptr(),
);
if ret < 0 || out_ctx.is_null() {
return Err(ffmpeg_err(ret));
}
// ── 2. Set HLS muxer options ──────────────────────────────────────────
let seg_time_str = format!("{segment_secs}");
let list_size_str = format!("{playlist_size}");
let use_fmp4 = segment_format == crate::hls::HlsSegmentFormat::Fmp4;
let seg_ext = if use_fmp4 { "m4s" } else { "ts" };
let seg_filename = format!("{output_dir}/segment%03d.{seg_ext}");
if let (Ok(c_seg_time), Ok(c_list_size), Ok(c_seg_file)) = (
CString::new(seg_time_str.as_str()),
CString::new(list_size_str.as_str()),
CString::new(seg_filename.as_str()),
) {
let ret = av_opt_set(
(*out_ctx).priv_data,
c"hls_time".as_ptr(),
c_seg_time.as_ptr(),
0,
);
if ret < 0 {
log::warn!(
"live_hls hls_time option not supported, using default \
requested={seg_time_str} error={}",
ff_sys::av_error_string(ret)
);
}
let ret = av_opt_set(
(*out_ctx).priv_data,
c"hls_list_size".as_ptr(),
c_list_size.as_ptr(),
0,
);
if ret < 0 {
log::warn!(
"live_hls hls_list_size option not supported, using default \
requested={list_size_str} error={}",
ff_sys::av_error_string(ret)
);
}
let ret = av_opt_set(
(*out_ctx).priv_data,
c"hls_flags".as_ptr(),
c"delete_segments".as_ptr(),
0,
);
if ret < 0 {
log::warn!(
"live_hls hls_flags option not supported error={}",
ff_sys::av_error_string(ret)
);
}
let ret = av_opt_set(
(*out_ctx).priv_data,
c"hls_segment_filename".as_ptr(),
c_seg_file.as_ptr(),
0,
);
if ret < 0 {
log::warn!(
"live_hls hls_segment_filename option not supported, using default \
requested={seg_filename} error={}",
ff_sys::av_error_string(ret)
);
}
if use_fmp4 {
let ret = av_opt_set(
(*out_ctx).priv_data,
c"hls_segment_type".as_ptr(),
c"fmp4".as_ptr(),
0,
);
if ret < 0 {
log::warn!(
"live_hls hls_segment_type fmp4 option not supported error={}",
ff_sys::av_error_string(ret)
);
}
}
}
// ── 3. Open H.264 video encoder ───────────────────────────────────────
let vid_enc_codec =
crate::codec_utils::select_h264_encoder("live_hls").ok_or_else(|| {
avformat_free_context(out_ctx);
ffmpeg_err_msg(
"no H.264 encoder available \
(tried h264_nvenc, h264_qsv, h264_amf, h264_videotoolbox, libx264, mpeg4)",
)
})?;
let mut vid_enc_ctx = ff_sys::avcodec::alloc_context3(vid_enc_codec).map_err(|e| {
avformat_free_context(out_ctx);
ffmpeg_err(e)
})?;
(*vid_enc_ctx).width = enc_width;
(*vid_enc_ctx).height = enc_height;
(*vid_enc_ctx).pix_fmt = AVPixelFormat_AV_PIX_FMT_YUV420P;
(*vid_enc_ctx).time_base.num = 1;
(*vid_enc_ctx).time_base.den = fps_int;
(*vid_enc_ctx).framerate.num = fps_int;
(*vid_enc_ctx).framerate.den = 1;
(*vid_enc_ctx).gop_size = fps_int * segment_secs as i32;
(*vid_enc_ctx).bit_rate = video_bitrate as i64;
ff_sys::avcodec::open2(vid_enc_ctx, vid_enc_codec, ptr::null_mut()).map_err(|e| {
ff_sys::avcodec::free_context(&mut vid_enc_ctx as *mut *mut _);
avformat_free_context(out_ctx);
ffmpeg_err(e)
})?;
// ── 4. Add video output stream ────────────────────────────────────────
let vid_out_stream = avformat_new_stream(out_ctx, vid_enc_codec);
if vid_out_stream.is_null() {
ff_sys::avcodec::free_context(&mut vid_enc_ctx as *mut *mut _);
avformat_free_context(out_ctx);
return Err(ffmpeg_err_msg("cannot create video output stream"));
}
(*vid_out_stream).time_base = (*vid_enc_ctx).time_base;
let vid_out_stream_idx = ((*out_ctx).nb_streams - 1) as i32;
// SAFETY: vid_out_stream and vid_enc_ctx are valid; avcodec_open2 has been called.
ff_sys::avcodec::parameters_from_context((*vid_out_stream).codecpar, vid_enc_ctx).map_err(
|e| {
ff_sys::avcodec::free_context(&mut vid_enc_ctx as *mut *mut _);
avformat_free_context(out_ctx);
ffmpeg_err(e)
},
)?;
// ── 5. Open AAC audio encoder and add audio stream (optional) ─────────
let mut aud_enc_ctx: *mut AVCodecContext = ptr::null_mut();
let mut aud_out_stream_idx: i32 = -1;
let mut aud_sample_rate = 44100i32;
let mut aud_frame_size = 1024i32;
if let Some((sr, nc, abr)) = audio {
aud_sample_rate = sr;
match crate::codec_utils::open_aac_encoder(sr, nc, abr, "live_hls") {
Ok(ctx) => {
aud_enc_ctx = ctx;
aud_frame_size = if (*aud_enc_ctx).frame_size > 0 {
(*aud_enc_ctx).frame_size
} else {
1024
};
let aud_out_stream = avformat_new_stream(out_ctx, ptr::null());
if aud_out_stream.is_null() {
ff_sys::avcodec::free_context(&mut aud_enc_ctx as *mut *mut _);
log::warn!("live_hls cannot create audio output stream, skipping audio");
} else {
(*aud_out_stream).time_base.num = 1;
(*aud_out_stream).time_base.den = sr;
aud_out_stream_idx = ((*out_ctx).nb_streams - 1) as i32;
// SAFETY: aud_out_stream and aud_enc_ctx are valid.
if ff_sys::avcodec::parameters_from_context(
(*aud_out_stream).codecpar,
aud_enc_ctx,
)
.is_err()
{
log::warn!("live_hls audio stream codecpar copy failed");
}
}
}
Err(e) => {
log::warn!("live_hls aac encoder unavailable: {e}, skipping audio");
}
}
}
// ── 6. Open output file and write header ──────────────────────────────
let pb = ff_sys::avformat::open_output(
Path::new(&playlist_path),
ff_sys::avformat::avio_flags::WRITE,
)
.map_err(|e| {
if !aud_enc_ctx.is_null() {
ff_sys::avcodec::free_context(&mut aud_enc_ctx as *mut *mut _);
}
ff_sys::avcodec::free_context(&mut vid_enc_ctx as *mut *mut _);
avformat_free_context(out_ctx);
ffmpeg_err(e)
})?;
(*out_ctx).pb = pb;
let ret = avformat_write_header(out_ctx, ptr::null_mut());
if ret < 0 {
ff_sys::avformat::close_output(&mut (*out_ctx).pb);
if !aud_enc_ctx.is_null() {
ff_sys::avcodec::free_context(&mut aud_enc_ctx as *mut *mut _);
}
ff_sys::avcodec::free_context(&mut vid_enc_ctx as *mut *mut _);
avformat_free_context(out_ctx);
return Err(ffmpeg_err(ret));
}
// Close pb so the HLS muxer can rename its .tmp playlist files without
// hitting a locked-file error on Windows.
ff_sys::avformat::close_output(&mut (*out_ctx).pb);
log::info!(
"live_hls output opened \
output_dir={output_dir} segment_duration={segment_secs}s \
playlist_size={playlist_size} width={enc_width} height={enc_height} \
fps={fps_int} audio={}",
aud_out_stream_idx >= 0
);
// ── 7. Build MuxerCore ────────────────────────────────────────────────
let core = MuxerCore::new(
out_ctx,
vid_enc_ctx,
aud_enc_ctx,
vid_out_stream_idx,
aud_out_stream_idx,
fps_int,
enc_width,
enc_height,
aud_frame_size,
aud_sample_rate,
"live_hls",
false, // close_pb_after_trailer: pb already closed after header write
)
.inspect_err(|_| {
if !aud_enc_ctx.is_null() {
ff_sys::avcodec::free_context(&mut aud_enc_ctx as *mut *mut _);
}
ff_sys::avcodec::free_context(&mut vid_enc_ctx as *mut *mut _);
avformat_free_context(out_ctx);
})?;
Ok(Self { core })
}
}