direct_play_nice 0.1.0-beta.2

CLI program that converts video files to direct-play-compatible formats.
Documentation
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
//! Shared device-profile model and aggregation utilities for cross-device compatibility constraints.

use std::{cmp::Ordering, convert::TryFrom};

use anyhow::{anyhow, bail, Result};
use rusty_ffmpeg::ffi;
use strum::IntoEnumIterator;
use strum_macros::EnumIter;

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
/// Named output resolutions used for device constraints.
pub enum Resolution {
    /// 640x480.
    Resolution480p,
    /// 1280x720.
    Resolution720p,
    /// 1920x1080.
    Resolution1080p,
    /// 2560x1440.
    Resolution1440p,
    /// 3840x2160.
    Resolution2160p,
}

impl Resolution {
    /// Returns canonical `(width, height)` dimensions.
    pub fn to_dimensions(self) -> (u32, u32) {
        match self {
            Resolution::Resolution480p => (640, 480),
            Resolution::Resolution720p => (1280, 720),
            Resolution::Resolution1080p => (1920, 1080),
            Resolution::Resolution1440p => (2560, 1440),
            Resolution::Resolution2160p => (3840, 2160),
        }
    }

    /// Maps known dimensions to a [`Resolution`].
    ///
    /// Unknown values default to `1080p`.
    pub fn from_dimensions(x: u32, y: u32) -> Resolution {
        match (x, y) {
            (640, 480) => Resolution::Resolution480p,
            (1280, 720) => Resolution::Resolution720p,
            (1920, 1080) => Resolution::Resolution1080p,
            (2560, 1440) => Resolution::Resolution1440p,
            (3840, 2160) => Resolution::Resolution2160p,
            _ => Resolution::Resolution1080p,
        }
    }
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, EnumIter)]
/// H.264 level values used for compatibility checks.
pub enum H264Level {
    /// H.264 level 1.0.
    Level1 = 10,
    /// H.264 level 1.1.
    Level1_1 = 11,
    /// H.264 level 1.2.
    Level1_2 = 12,
    /// H.264 level 1.3.
    Level1_3 = 13,
    /// H.264 level 2.0.
    Level2 = 20,
    /// H.264 level 2.1.
    Level2_1 = 21,
    /// H.264 level 2.2.
    Level2_2 = 22,
    /// H.264 level 3.0.
    Level3 = 30,
    /// H.264 level 3.1.
    Level3_1 = 31,
    /// H.264 level 3.2.
    Level3_2 = 32,
    /// H.264 level 4.0.
    Level4 = 40,
    /// H.264 level 4.1.
    Level4_1 = 41,
    /// H.264 level 4.2.
    Level4_2 = 42,
    /// H.264 level 5.0.
    Level5 = 50,
    /// H.264 level 5.1.
    Level5_1 = 51,
    /// H.264 level 5.2.
    Level5_2 = 52,
}

impl PartialOrd for H264Level {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for H264Level {
    fn cmp(&self, other: &Self) -> Ordering {
        (*self as u32).cmp(&(*other as u32))
    }
}

impl TryFrom<i32> for H264Level {
    type Error = &'static str;

    fn try_from(value: i32) -> Result<Self, Self::Error> {
        match value {
            10 => Ok(H264Level::Level1),
            11 => Ok(H264Level::Level1_1),
            12 => Ok(H264Level::Level1_2),
            13 => Ok(H264Level::Level1_3),
            20 => Ok(H264Level::Level2),
            21 => Ok(H264Level::Level2_1),
            22 => Ok(H264Level::Level2_2),
            30 => Ok(H264Level::Level3),
            31 => Ok(H264Level::Level3_1),
            32 => Ok(H264Level::Level3_2),
            40 => Ok(H264Level::Level4),
            41 => Ok(H264Level::Level4_1),
            42 => Ok(H264Level::Level4_2),
            50 => Ok(H264Level::Level5),
            51 => Ok(H264Level::Level5_1),
            52 => Ok(H264Level::Level5_2),
            _ => Err("Invalid H.264 level value"),
        }
    }
}

impl H264Level {
    /// Returns FFmpeg's textual representation for this level.
    pub fn ffmpeg_name(&self) -> &'static str {
        match self {
            H264Level::Level1 => "1",
            H264Level::Level1_1 => "1.1",
            H264Level::Level1_2 => "1.2",
            H264Level::Level1_3 => "1.3",
            H264Level::Level2 => "2",
            H264Level::Level2_1 => "2.1",
            H264Level::Level2_2 => "2.2",
            H264Level::Level3 => "3",
            H264Level::Level3_1 => "3.1",
            H264Level::Level3_2 => "3.2",
            H264Level::Level4 => "4",
            H264Level::Level4_1 => "4.1",
            H264Level::Level4_2 => "4.2",
            H264Level::Level5 => "5",
            H264Level::Level5_1 => "5.1",
            H264Level::Level5_2 => "5.2",
        }
    }
}

#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
/// H.264 profile values used for compatibility checks.
pub enum H264Profile {
    /// Baseline profile.
    Baseline = ffi::AV_PROFILE_H264_BASELINE as isize,
    /// Main profile.
    Main = ffi::AV_PROFILE_H264_MAIN as isize,
    /// Extended profile.
    Extended = ffi::AV_PROFILE_H264_EXTENDED as isize,
    /// High profile.
    High = ffi::AV_PROFILE_H264_HIGH as isize,
    /// High 10 profile.
    High10 = ffi::AV_PROFILE_H264_HIGH_10 as isize,
    /// High 4:2:2 profile.
    High422 = ffi::AV_PROFILE_H264_HIGH_422 as isize,
    /// High 4:4:4 profile.
    High444 = ffi::AV_PROFILE_H264_HIGH_444 as isize,
}

impl TryFrom<i32> for H264Profile {
    type Error = &'static str;

    fn try_from(value: i32) -> Result<Self, Self::Error> {
        match value {
            x if x == ffi::AV_PROFILE_H264_BASELINE as i32 => Ok(H264Profile::Baseline),
            x if x == ffi::AV_PROFILE_H264_MAIN as i32 => Ok(H264Profile::Main),
            x if x == ffi::AV_PROFILE_H264_EXTENDED as i32 => Ok(H264Profile::Extended),
            x if x == ffi::AV_PROFILE_H264_HIGH as i32 => Ok(H264Profile::High),
            x if x == ffi::AV_PROFILE_H264_HIGH_10 as i32 => Ok(H264Profile::High10),
            x if x == ffi::AV_PROFILE_H264_HIGH_422 as i32 => Ok(H264Profile::High422),
            x if x == ffi::AV_PROFILE_H264_HIGH_444 as i32 => Ok(H264Profile::High444),
            _ => Err("Invalid H.264 profile value"),
        }
    }
}

impl TryFrom<u32> for H264Profile {
    type Error = &'static str;

    fn try_from(value: u32) -> Result<Self, Self::Error> {
        let value = i32::try_from(value).map_err(|_| "Invalid H.264 profile value")?;
        H264Profile::try_from(value)
    }
}

impl H264Profile {
    /// Returns FFmpeg's textual representation for this profile.
    pub fn ffmpeg_name(&self) -> &'static str {
        match self {
            H264Profile::Baseline => "baseline",
            H264Profile::Main => "main",
            H264Profile::Extended => "extended",
            H264Profile::High => "high",
            H264Profile::High10 => "high10",
            H264Profile::High422 => "high422",
            H264Profile::High444 => "high444",
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
/// Output container format.
pub enum ContainerFormat {
    /// MPEG-4 Part 14 container.
    Mp4,
    /// iTunes-style MPEG-4 variant.
    M4v,
    /// QuickTime container.
    Mov,
    /// Matroska container.
    Mkv,
}

impl ContainerFormat {
    /// Returns the standard lowercase extension for this container.
    pub fn as_str(self) -> &'static str {
        match self {
            ContainerFormat::Mp4 => "mp4",
            ContainerFormat::M4v => "m4v",
            ContainerFormat::Mov => "mov",
            ContainerFormat::Mkv => "mkv",
        }
    }

    /// Parses a file extension into a container format.
    pub fn from_extension(ext: &str) -> Option<Self> {
        match ext.to_ascii_lowercase().as_str() {
            "mp4" => Some(ContainerFormat::Mp4),
            "m4v" => Some(ContainerFormat::M4v),
            "mov" => Some(ContainerFormat::Mov),
            "mkv" => Some(ContainerFormat::Mkv),
            _ => None,
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
/// Supported streaming-device families.
pub enum DeviceFamily {
    /// Google Chromecast and related devices.
    Chromecast,
    /// Roku devices.
    Roku,
    /// Apple TV devices.
    AppleTv,
    /// Amazon Fire TV devices.
    FireTv,
}

impl DeviceFamily {
    /// Returns this family as a canonical identifier string.
    pub fn as_str(self) -> &'static str {
        match self {
            DeviceFamily::Chromecast => "chromecast",
            DeviceFamily::Roku => "roku",
            DeviceFamily::AppleTv => "apple_tv",
            DeviceFamily::FireTv => "fire_tv",
        }
    }

    /// Parses a family identifier like `"roku"` or `"apple_tv"`.
    pub fn from_identifier(id: &str) -> Option<Self> {
        match id.trim().to_ascii_lowercase().as_str() {
            "chromecast" => Some(DeviceFamily::Chromecast),
            "roku" => Some(DeviceFamily::Roku),
            "apple_tv" | "appletv" | "apple-tv" => Some(DeviceFamily::AppleTv),
            "fire_tv" | "firetv" | "fire-tv" => Some(DeviceFamily::FireTv),
            _ => None,
        }
    }
}

#[allow(dead_code)]
#[derive(Clone, Debug, PartialEq, Eq)]
/// Capability profile for a specific streaming device model.
pub struct StreamingDevice {
    /// Human-readable device name.
    pub name: &'static str,
    /// Stable model identifier used by this crate.
    pub model: &'static str,
    /// Device manufacturer.
    pub maker: &'static str,
    /// Device family classification.
    pub family: DeviceFamily,
    /// Supported output containers.
    pub containers: &'static [ContainerFormat],
    /// Supported video codecs.
    pub video_codecs: &'static [ffi::AVCodecID],
    /// Supported audio codecs.
    pub audio_codecs: &'static [ffi::AVCodecID],
    /// Maximum supported H.264 profile.
    pub max_h264_profile: H264Profile,
    /// Maximum supported H.264 level.
    pub max_h264_level: H264Level,
    /// Maximum supported frame rate.
    pub max_fps: u32,
    /// Maximum supported output resolution.
    pub max_resolution: Resolution,
    /// Optional maximum supported video bitrate (bits/sec).
    pub max_video_bitrate: Option<i64>,
    /// Optional maximum supported audio bitrate (bits/sec).
    pub max_audio_bitrate: Option<i64>,
}

#[derive(Clone, Debug)]
/// Device-agnostic constraints resolved across selected targets.
pub struct ResolvedTargetProfile {
    #[allow(dead_code)]
    /// Selected output container.
    pub container: ContainerFormat,
    /// Selected output video codec.
    pub video_codec: ffi::AVCodecID,
    /// Selected output audio codec.
    pub audio_codec: ffi::AVCodecID,
    /// Optional H.264 profile/level constraints.
    pub h264_constraints: Option<(H264Profile, H264Level)>,
    /// Maximum output frame rate.
    pub max_fps: u32,
    /// Maximum output resolution.
    pub max_resolution: Resolution,
    /// Optional maximum output video bitrate (bits/sec).
    pub max_video_bitrate: Option<i64>,
    /// Optional maximum output audio bitrate (bits/sec).
    pub max_audio_bitrate: Option<i64>,
}

#[derive(Clone, Debug)]
#[allow(dead_code)]
/// Relevant media characteristics discovered from an input file.
pub struct InputMediaProfile {
    /// Input container format, if known.
    pub container: Option<ContainerFormat>,
    /// Input video codec.
    pub video_codec: ffi::AVCodecID,
    /// Input audio codec.
    pub audio_codec: ffi::AVCodecID,
    /// Input video bitrate (bits/sec), if known.
    pub video_bitrate: Option<i64>,
    /// Input audio bitrate (bits/sec), if known.
    pub audio_bitrate: Option<i64>,
}

#[derive(Clone, Debug)]
#[allow(dead_code)]
/// Planned output settings after device and input reconciliation.
pub struct PlannedOutputProfile {
    /// Planned output container.
    pub container: ContainerFormat,
    /// Planned output video codec.
    pub video_codec: ffi::AVCodecID,
    /// Planned output audio codec.
    pub audio_codec: ffi::AVCodecID,
    /// Optional H.264 profile/level constraints.
    pub h264_constraints: Option<(H264Profile, H264Level)>,
    /// Planned maximum output frame rate.
    pub max_fps: u32,
    /// Planned maximum output resolution.
    pub max_resolution: Resolution,
    /// Planned target video bitrate (bits/sec), if set.
    pub target_video_bitrate: Option<i64>,
    /// Planned target audio bitrate (bits/sec), if set.
    pub target_audio_bitrate: Option<i64>,
}

impl StreamingDevice {
    /// Returns one video codec shared by all provided devices.
    pub fn get_common_video_codec(devices: &[&StreamingDevice]) -> Result<ffi::AVCodecID> {
        let first = devices
            .first()
            .ok_or_else(|| anyhow!("No streaming devices provided"))?;
        let mut common_codecs = first.video_codecs.to_vec();
        for device in &devices[1..] {
            common_codecs.retain(|codec_id| device.video_codecs.contains(codec_id));
        }
        common_codecs
            .into_iter()
            .next()
            .ok_or_else(|| anyhow!("No common video codec found among the selected devices"))
    }

    /// Returns one audio codec shared by all provided devices.
    pub fn get_common_audio_codec(devices: &[&StreamingDevice]) -> Result<ffi::AVCodecID> {
        let first = devices
            .first()
            .ok_or_else(|| anyhow!("No streaming devices provided"))?;
        let mut common_codecs = first.audio_codecs.to_vec();
        for device in &devices[1..] {
            common_codecs.retain(|codec_id| device.audio_codecs.contains(codec_id));
        }
        common_codecs
            .into_iter()
            .next()
            .ok_or_else(|| anyhow!("No common audio codec found among the selected devices"))
    }

    /// Returns all container formats common to all provided devices.
    pub fn get_common_containers(devices: &[&StreamingDevice]) -> Result<Vec<ContainerFormat>> {
        let first = devices
            .first()
            .ok_or_else(|| anyhow!("No streaming devices provided"))?;
        let mut common = first.containers.to_vec();
        for device in &devices[1..] {
            common.retain(|container| device.containers.contains(container));
        }
        if common.is_empty() {
            bail!("No common output container found among the selected devices");
        }
        Ok(common)
    }

    /// Returns the first common container format across devices.
    pub fn get_common_container(devices: &[&StreamingDevice]) -> Result<ContainerFormat> {
        Self::get_common_containers(devices)?
            .into_iter()
            .next()
            .ok_or_else(|| anyhow!("No common output container found among the selected devices"))
    }

    /// Returns the strictest (lowest) maximum H.264 profile across devices.
    pub fn get_min_h264_profile(devices: &[&StreamingDevice]) -> Result<H264Profile> {
        let mut min_profile = H264Profile::High444;
        for device in devices {
            if device.max_h264_profile < min_profile {
                min_profile = device.max_h264_profile;
            }
        }
        Ok(min_profile)
    }

    /// Returns the strictest (lowest) maximum H.264 level across devices.
    pub fn get_min_h264_level(devices: &[&StreamingDevice]) -> Result<H264Level> {
        let mut min_level = H264Level::iter().max_by_key(|level| *level as i32).unwrap();
        for device in devices {
            if device.max_h264_level < min_level {
                min_level = device.max_h264_level;
            }
        }
        Ok(min_level)
    }

    /// Returns the minimum supported fps limit across devices.
    pub fn get_min_fps(devices: &[&StreamingDevice]) -> Result<u32> {
        let mut min_fps = u32::MAX;
        for device in devices {
            if device.max_fps < min_fps {
                min_fps = device.max_fps;
            }
        }
        Ok(min_fps)
    }

    /// Returns the minimum supported resolution across devices.
    pub fn get_min_resolution(devices: &[&StreamingDevice]) -> Result<Resolution> {
        let mut min_res = (u32::MAX, u32::MAX);
        for device in devices {
            let res = device.max_resolution.to_dimensions();
            if res.0 < min_res.0 || res.1 < min_res.1 {
                min_res = res;
            }
        }
        if min_res.0 < 1 || min_res.1 < 1 {
            bail!(
                "Target resolution ({}, {}) too small. Exiting...",
                min_res.0,
                min_res.1
            );
        }
        Ok(Resolution::from_dimensions(min_res.0, min_res.1))
    }

    /// Returns the smallest video bitrate limit across devices.
    pub fn get_min_video_bitrate(devices: &[&StreamingDevice]) -> Option<i64> {
        devices.iter().filter_map(|d| d.max_video_bitrate).min()
    }

    /// Returns the smallest audio bitrate limit across devices.
    pub fn get_min_audio_bitrate(devices: &[&StreamingDevice]) -> Option<i64> {
        devices.iter().filter_map(|d| d.max_audio_bitrate).min()
    }
}

/// Computes a resolved compatibility profile for selected target devices.
///
/// # Examples
///
/// ```rust
/// let devices = vec![
///     &direct_play_nice::devices::roku::ROKU_ULTRA,
///     &direct_play_nice::devices::apple_tv::APPLE_TV_4K_3RD_GEN,
/// ];
/// let resolved = direct_play_nice::devices::resolve_target_profile(&devices)
///     .expect("should find a compatible intersection");
/// assert!(resolved.max_fps > 0);
/// ```
pub fn resolve_target_profile(devices: &[&StreamingDevice]) -> Result<ResolvedTargetProfile> {
    let video_codec = StreamingDevice::get_common_video_codec(devices)?;
    let audio_codec = StreamingDevice::get_common_audio_codec(devices)?;
    let container = StreamingDevice::get_common_container(devices)?;
    let h264_constraints = if video_codec == ffi::AV_CODEC_ID_H264 {
        Some((
            StreamingDevice::get_min_h264_profile(devices)?,
            StreamingDevice::get_min_h264_level(devices)?,
        ))
    } else {
        None
    };

    Ok(ResolvedTargetProfile {
        container,
        video_codec,
        audio_codec,
        h264_constraints,
        max_fps: StreamingDevice::get_min_fps(devices)?,
        max_resolution: StreamingDevice::get_min_resolution(devices)?,
        max_video_bitrate: StreamingDevice::get_min_video_bitrate(devices),
        max_audio_bitrate: StreamingDevice::get_min_audio_bitrate(devices),
    })
}

#[allow(dead_code)]
/// Produces an output plan for selected devices and input media properties.
///
/// # Examples
///
/// ```rust
/// use rsmpeg::ffi;
///
/// let devices = vec![&direct_play_nice::devices::roku::ROKU_ULTRA];
/// let input = direct_play_nice::devices::InputMediaProfile {
///     container: Some(direct_play_nice::devices::ContainerFormat::Mkv),
///     video_codec: ffi::AV_CODEC_ID_H264,
///     audio_codec: ffi::AV_CODEC_ID_AAC,
///     video_bitrate: Some(8_000_000),
///     audio_bitrate: Some(192_000),
/// };
/// let plan = direct_play_nice::devices::plan_output_profile(&devices, &input)
///     .expect("planning should succeed");
/// assert!(plan.max_fps > 0);
/// ```
pub fn plan_output_profile(
    devices: &[&StreamingDevice],
    input: &InputMediaProfile,
) -> Result<PlannedOutputProfile> {
    let resolved = resolve_target_profile(devices)?;
    let common_containers = StreamingDevice::get_common_containers(devices)?;

    let container = input
        .container
        .filter(|container| common_containers.contains(container))
        .unwrap_or(resolved.container);

    let video_codec = if devices
        .iter()
        .all(|device| device.video_codecs.contains(&input.video_codec))
    {
        input.video_codec
    } else {
        resolved.video_codec
    };
    let audio_codec = if devices
        .iter()
        .all(|device| device.audio_codecs.contains(&input.audio_codec))
    {
        input.audio_codec
    } else {
        resolved.audio_codec
    };

    let target_video_bitrate = match (input.video_bitrate, resolved.max_video_bitrate) {
        (Some(input), Some(limit)) => Some(input.min(limit)),
        (Some(input), None) => Some(input),
        (None, Some(limit)) => Some(limit),
        (None, None) => None,
    };
    let target_audio_bitrate = match (input.audio_bitrate, resolved.max_audio_bitrate) {
        (Some(input), Some(limit)) => Some(input.min(limit)),
        (Some(input), None) => Some(input),
        (None, Some(limit)) => Some(limit),
        (None, None) => None,
    };

    let h264_constraints = if video_codec == ffi::AV_CODEC_ID_H264 {
        resolved.h264_constraints
    } else {
        None
    };

    Ok(PlannedOutputProfile {
        container,
        video_codec,
        audio_codec,
        h264_constraints,
        max_fps: resolved.max_fps,
        max_resolution: resolved.max_resolution,
        target_video_bitrate,
        target_audio_bitrate,
    })
}

impl ResolvedTargetProfile {
    #[allow(dead_code)]
    /// Returns whether this profile is playable by `device`.
    pub fn is_compatible_with_device(&self, device: &StreamingDevice) -> bool {
        let h264_ok = if let Some((profile, level)) = self.h264_constraints {
            profile <= device.max_h264_profile && level <= device.max_h264_level
        } else {
            true
        };

        let bitrate_ok = self
            .max_video_bitrate
            .map(|v| device.max_video_bitrate.map(|d| v <= d).unwrap_or(true))
            .unwrap_or(true)
            && self
                .max_audio_bitrate
                .map(|v| device.max_audio_bitrate.map(|d| v <= d).unwrap_or(true))
                .unwrap_or(true);

        device.containers.contains(&self.container)
            && device.video_codecs.contains(&self.video_codec)
            && device.audio_codecs.contains(&self.audio_codec)
            && self.max_fps <= device.max_fps
            && {
                let (w, h) = self.max_resolution.to_dimensions();
                let (dw, dh) = device.max_resolution.to_dimensions();
                w <= dw && h <= dh
            }
            && h264_ok
            && bitrate_ok
    }
}

#[cfg(test)]
mod tests {
    use crate::devices::device_profile::*;

    #[test]
    fn h264_profile_try_from_accepts_known_profiles() {
        assert_eq!(
            H264Profile::try_from(ffi::AV_PROFILE_H264_BASELINE).expect("baseline should map"),
            H264Profile::Baseline
        );
        assert_eq!(
            H264Profile::try_from(ffi::AV_PROFILE_H264_HIGH).expect("high should map"),
            H264Profile::High
        );
    }

    #[test]
    fn h264_profile_try_from_rejects_unknown_value() {
        assert!(H264Profile::try_from(-1).is_err());
    }
}