hvac-transcoder 5.4.0

GPU-accelerated media transcoder (HEVC/h265 via NVENC, VAAPI, VideoToolbox). Single-binary Tdarr alternative.
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use launchdarkly_server_sdk::{
    Client, ConfigBuilder, Context, ContextBuilder, MultiContextBuilder,
};
use std::sync::Arc;
use std::time::{Duration, Instant};

/// LaunchDarkly feature-flag client. All methods are no-ops when no SDK key
/// is supplied; the app runs normally with defaults.
///
/// The SDK key is **CLI-only** by design — it is never read from the
/// environment. Pass it explicitly per invocation via
/// `hvac --launchdarkly-sdk-key <KEY> ...`. This prevents a key in a shell
/// rc from silently controlling every run.
pub struct Flags {
    client: Option<Arc<Client>>,
    hostname: String,
    username: String,
    gpu_name: Option<String>,
    gpu_encoder: Option<String>,
    gpu_kind: Option<String>,
}

impl Flags {
    /// Connect to LaunchDarkly using the supplied SDK key. Pass `None`
    /// (or an empty string) to get a no-op client. Blocks up to 2 s for
    /// initialization when a key is present.
    pub fn new(sdk_key: Option<&str>) -> Self {
        let hostname = get_hostname();
        let username = std::env::var("USER")
            .or_else(|_| std::env::var("LOGNAME"))
            .unwrap_or_else(|_| "unknown".to_string());

        let client = sdk_key.filter(|k| !k.is_empty()).and_then(build_client);
        Flags {
            client,
            hostname,
            username,
            gpu_name: None,
            gpu_encoder: None,
            gpu_kind: None,
        }
    }

    /// Enrich the evaluation context with GPU info after detection.
    pub fn set_gpu(&mut self, name: &str, encoder: &str, kind: &str) {
        self.gpu_name = Some(name.to_string());
        self.gpu_encoder = Some(encoder.to_string());
        self.gpu_kind = Some(kind.to_string());
    }

    // ── Boolean flags ─────────────────────────────────────────────────────────

    /// Master kill-switch: set false to abort processing remaining files.
    pub fn enable_transcoding(&self) -> bool {
        self.bool_flag("enable-transcoding", true)
    }

    /// Pause all encoding. Workers spin until this goes false; in-flight jobs finish.
    pub fn pause_transcoding(&self) -> bool {
        self.bool_flag("pause-transcoding", false)
    }

    // ── Integer flags ─────────────────────────────────────────────────────────

    /// Override parallel job count. 0 = auto-detect from GPU.
    pub fn max_parallel_jobs(&self) -> usize {
        self.int_flag("max-parallel-jobs", 0).max(0) as usize
    }

    // ── Event tracking ────────────────────────────────────────────────────────

    pub fn track_gpu_detected(&self, name: &str, encoder: &str, kind: &str, max_sessions: usize) {
        self.emit_metric(
            "gpu-detected",
            max_sessions as f64,
            serde_json::json!({
                "gpu_name": name,
                "encoder": encoder,
                "kind": kind,
                "max_sessions": max_sessions as i64,
            }),
        );
    }

    pub fn track_scan_completed(&self, file_count: usize, total_size: u64) {
        self.emit_metric(
            "scan-completed",
            file_count as f64,
            serde_json::json!({
                "file_count": file_count as i64,
                "total_size_bytes": total_size as i64,
            }),
        );
    }

    pub fn track_transcode_started(
        &self,
        filename: &str,
        bitrate_kbps: u32,
        duration_secs: f64,
        source_size: u64,
        pix_fmt: &str,
    ) {
        self.emit_metric(
            "transcode-started",
            source_size as f64,
            serde_json::json!({
                "filename": filename,
                "bitrate_kbps": bitrate_kbps as i64,
                "duration_secs": duration_secs,
                "source_size_bytes": source_size as i64,
                "pix_fmt": pix_fmt,
            }),
        );
    }

    pub fn track_transcode_completed(
        &self,
        filename: &str,
        source_size: u64,
        output_size: u64,
        saved_pct: i32,
    ) {
        let bytes_saved = (source_size as i64 - output_size as i64).max(0);
        self.emit_metric(
            "transcode-completed",
            bytes_saved as f64,
            serde_json::json!({
                "filename": filename,
                "source_size_bytes": source_size as i64,
                "output_size_bytes": output_size as i64,
                "saved_pct": saved_pct,
                "bytes_saved": bytes_saved,
            }),
        );
    }

    pub fn track_transcode_failed(&self, filename: &str, error_type: &str) {
        self.emit(
            "transcode-failed",
            serde_json::json!({
                "filename": filename,
                "error_type": error_type,
            }),
        );
    }

    pub fn track_disk_wait(&self, filename: &str, available_gb: f64) {
        self.emit_metric(
            "disk-wait",
            available_gb,
            serde_json::json!({
                "filename": filename,
                "available_gb": available_gb,
            }),
        );
    }

    pub fn track_session_limit_hit(&self, active_sessions: u32) {
        self.emit(
            "session-limit-hit",
            serde_json::json!({ "active_sessions": active_sessions as i64 }),
        );
    }

    pub fn track_subtitle_retry(&self, filename: &str) {
        self.emit(
            "subtitle-retry",
            serde_json::json!({ "filename": filename }),
        );
    }

    pub fn track_auto_ramp_increased(&self, old_max: u32, new_max: u32, total_speed: u64) {
        self.emit_metric(
            "auto-ramp-increased",
            total_speed as f64,
            serde_json::json!({
                "old_max": old_max as i64,
                "new_max": new_max as i64,
                "total_speed": total_speed as i64,
            }),
        );
    }

    // ── Pause / resume ────────────────────────────────────────────────────────

    pub fn track_transcoding_paused(&self, active_workers: usize) {
        self.emit(
            "transcoding-paused",
            serde_json::json!({ "active_workers": active_workers as i64 }),
        );
    }

    pub fn track_transcoding_resumed(&self, active_workers: usize) {
        self.emit(
            "transcoding-resumed",
            serde_json::json!({ "active_workers": active_workers as i64 }),
        );
    }

    // ── Run start ─────────────────────────────────────────────────────────────

    pub fn track_run_started(
        &self,
        total_files: usize,
        total_bytes: u64,
        jobs: usize,
        auto_ramp: bool,
    ) {
        self.emit_metric(
            "run-started",
            total_files as f64,
            serde_json::json!({
                "total_files": total_files as i64,
                "total_bytes": total_bytes as i64,
                "jobs": jobs as i64,
                "auto_ramp": auto_ramp,
            }),
        );
    }

    // ── Transcode queue / retry ───────────────────────────────────────────────

    pub fn track_transcode_queued(&self, filename: &str, position: usize, max: u32) {
        self.emit(
            "transcode-queued",
            serde_json::json!({
                "filename": filename,
                "queue_position": position as i64,
                "max_parallel": max as i64,
            }),
        );
    }

    pub fn track_transcode_retry(&self, filename: &str, attempt: u32, reason: &str) {
        self.emit(
            "transcode-retry",
            serde_json::json!({
                "filename": filename,
                "attempt": attempt as i64,
                "reason": reason,
            }),
        );
    }

    pub fn track_auto_ramp_stopped(&self, final_max: u32, reverted: bool) {
        self.emit(
            "auto-ramp-stopped",
            serde_json::json!({
                "final_max": final_max as i64,
                "reverted": reverted,
            }),
        );
    }

    pub fn track_run_completed(
        &self,
        transcoded: u32,
        skipped: u32,
        errors: u32,
        bytes_saved: u64,
        bytes_input: u64,
        bytes_output: u64,
    ) {
        let ratio = if bytes_input > 0 {
            bytes_output as f64 / bytes_input as f64
        } else {
            1.0
        };
        self.emit_metric(
            "run-completed",
            bytes_saved as f64,
            serde_json::json!({
                "transcoded": transcoded as i64,
                "skipped": skipped as i64,
                "errors": errors as i64,
                "bytes_saved": bytes_saved as i64,
                "bytes_input": bytes_input as i64,
                "bytes_output": bytes_output as i64,
                "compression_ratio": ratio,
            }),
        );
    }

    /// Flush all buffered events and shut down. Call once at end of main().
    pub fn close(&self) {
        if let Some(ref c) = self.client {
            c.close();
        }
    }

    // ── Private helpers ───────────────────────────────────────────────────────

    fn make_context(&self) -> Context {
        let mut ub = ContextBuilder::new(&self.hostname);
        ub.kind("user")
            .set_string("hostname", self.hostname.as_str())
            .set_string("username", self.username.as_str());
        let user_ctx = ub
            .build()
            .unwrap_or_else(|_| ContextBuilder::new("fallback").build().expect("fallback"));

        match (&self.gpu_name, &self.gpu_encoder) {
            (Some(gpu_name), Some(gpu_encoder)) => {
                let gpu_key = format!("{}.{}", gpu_encoder, self.hostname);
                let mut db = ContextBuilder::new(&gpu_key);
                db.kind("device")
                    .set_string("gpu_name", gpu_name.as_str())
                    .set_string("encoder", gpu_encoder.as_str());
                if let Some(ref kind) = self.gpu_kind {
                    db.set_string("kind", kind.as_str());
                }
                let device_ctx = db.build().unwrap_or_else(|_| {
                    ContextBuilder::new("fallback-device")
                        .build()
                        .expect("fallback device")
                });

                let mut mb = MultiContextBuilder::new();
                mb.add_context(user_ctx).add_context(device_ctx);
                mb.build().unwrap_or_else(|_| {
                    ContextBuilder::new(&self.hostname)
                        .build()
                        .expect("fallback multi")
                })
            }
            _ => user_ctx,
        }
    }

    fn bool_flag(&self, key: &str, default: bool) -> bool {
        match &self.client {
            None => default,
            Some(c) => {
                let ctx = self.make_context();
                let detail = c.bool_variation_detail(&ctx, key, default);
                let value = detail.value.unwrap_or(default);
                log::debug!("flag {key:?} = {value:?} ({:?})", detail.reason);
                value
            }
        }
    }

    fn int_flag(&self, key: &str, default: i64) -> i64 {
        match &self.client {
            None => default,
            Some(c) => c.int_variation(&self.make_context(), key, default),
        }
    }

    fn emit(&self, key: &str, data: serde_json::Value) {
        if let Some(ref c) = self.client {
            let _ = c.track_data(self.make_context(), key, data);
        }
    }

    fn emit_metric(&self, key: &str, metric: f64, data: serde_json::Value) {
        if let Some(ref c) = self.client {
            c.track_metric(self.make_context(), key, metric, data);
        }
    }
}

fn build_client(sdk_key: &str) -> Option<Arc<Client>> {
    if sdk_key.is_empty() {
        return None;
    }

    let config = match ConfigBuilder::new(sdk_key).build() {
        Ok(c) => c,
        Err(e) => {
            log::warn!("LaunchDarkly config error: {e}");
            return None;
        }
    };
    let client = match Client::build(config) {
        Ok(c) => c,
        Err(e) => {
            log::warn!("LaunchDarkly client build error: {e}");
            return None;
        }
    };
    if let Err(e) = client.start_with_runtime() {
        log::warn!("LaunchDarkly start error: {e}");
        return None;
    }

    // Wait up to 2 s for flag data to arrive.
    let deadline = Instant::now() + Duration::from_secs(2);
    while !client.initialized() && Instant::now() < deadline {
        std::thread::sleep(Duration::from_millis(50));
    }

    if client.initialized() {
        log::info!("LaunchDarkly: connected");
    } else {
        log::warn!("LaunchDarkly: not initialized within 2s — evaluations use defaults");
    }

    Some(Arc::new(client))
}

fn get_hostname() -> String {
    if let Ok(h) = std::env::var("HOSTNAME") {
        if !h.is_empty() {
            return h;
        }
    }
    unsafe {
        let mut buf = vec![0u8; 256];
        if libc::gethostname(buf.as_mut_ptr() as *mut libc::c_char, buf.len()) == 0 {
            let cstr = std::ffi::CStr::from_ptr(buf.as_ptr() as *const libc::c_char);
            if let Ok(s) = cstr.to_str() {
                if !s.is_empty() {
                    return s.to_string();
                }
            }
        }
    }
    "unknown".to_string()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn flags_new_with_none_is_noop() {
        // No SDK key → no client → all flag reads return defaults.
        let flags = Flags::new(None);
        assert!(flags.client.is_none());
        assert!(flags.enable_transcoding()); // default: true
        assert!(!flags.pause_transcoding()); // default: false
    }

    #[test]
    fn flags_new_with_empty_str_is_noop() {
        // Empty string is treated identically to None.
        let flags = Flags::new(Some(""));
        assert!(flags.client.is_none());
    }
}