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
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use fidl_table_validation::*;

use fidl_fuchsia_media::{
    Packet, PacketHeader, StreamBufferConstraints, StreamBufferSettings, StreamOutputConstraints,
};

#[derive(ValidFidlTable)]
#[fidl_table_src(StreamBufferConstraints)]
#[fidl_table_validator(StreamBufferConstraintsValidator)]
pub struct ValidStreamBufferConstraints {
    pub buffer_constraints_version_ordinal: u64,
    #[fidl_field_type(optional)]
    pub default_settings: Option<StreamBufferSettings>,
    #[fidl_field_type(optional)]
    pub per_packet_buffer_bytes_min: Option<u32>,
    #[fidl_field_type(optional)]
    pub per_packet_buffer_bytes_recommended: Option<u32>,
    #[fidl_field_type(optional)]
    pub per_packet_buffer_bytes_max: Option<u32>,
    #[fidl_field_type(optional)]
    pub packet_count_for_server_min: Option<u32>,
    #[fidl_field_type(optional)]
    pub packet_count_for_server_recommended: Option<u32>,
    #[fidl_field_type(optional)]
    pub packet_count_for_server_recommended_max: Option<u32>,
    #[fidl_field_type(optional)]
    pub packet_count_for_server_max: Option<u32>,
    #[fidl_field_type(optional)]
    pub packet_count_for_client_min: Option<u32>,
    #[fidl_field_type(optional)]
    pub packet_count_for_client_max: Option<u32>,
    #[fidl_field_type(optional)]
    pub single_buffer_mode_allowed: Option<bool>,
    #[fidl_field_type(optional)]
    pub is_physically_contiguous_required: Option<bool>,
}

pub struct StreamBufferConstraintsValidator;

#[derive(Debug)]
pub enum StreamBufferConstraintsError {
    VersionOrdinalZero,
    SingleBufferMode,
    ConstraintsNoBtiHandleForPhysicalBuffers,
}

impl Validate<ValidStreamBufferConstraints> for StreamBufferConstraintsValidator {
    type Error = StreamBufferConstraintsError;
    fn validate(candidate: &ValidStreamBufferConstraints) -> std::result::Result<(), Self::Error> {
        if candidate.buffer_constraints_version_ordinal == 0 {
            // An ordinal of 0 in StreamBufferConstraints is not allowed.
            return Err(StreamBufferConstraintsError::VersionOrdinalZero);
        }

        Ok(())
    }
}

#[derive(ValidFidlTable)]
#[fidl_table_src(StreamOutputConstraints)]
pub struct ValidStreamOutputConstraints {
    pub stream_lifetime_ordinal: u64,
    pub buffer_constraints_action_required: bool,
    pub buffer_constraints: ValidStreamBufferConstraints,
}

#[derive(ValidFidlTable, Clone, Copy, Debug, PartialEq)]
#[fidl_table_src(PacketHeader)]
pub struct ValidPacketHeader {
    pub buffer_lifetime_ordinal: u64,
    pub packet_index: u32,
}

#[derive(ValidFidlTable, Clone, Copy, Debug, PartialEq)]
#[fidl_table_src(Packet)]
pub struct ValidPacket {
    pub header: ValidPacketHeader,
    pub buffer_index: u32,
    pub stream_lifetime_ordinal: u64,
    pub start_offset: u32,
    pub valid_length_bytes: u32,
    #[fidl_field_type(optional)]
    pub timestamp_ish: Option<u64>,
    #[fidl_field_type(default = false)]
    pub start_access_unit: bool,
    #[fidl_field_type(default = false)]
    pub known_end_access_unit: bool,
}

#[cfg(test)]
mod test {

    use super::*;

    #[test]
    fn validate_stream_buffer_constraints() {
        let invalid_version_ordinal = StreamBufferConstraints {
            buffer_constraints_version_ordinal: Some(0),
            ..Default::default()
        };

        let err = ValidStreamBufferConstraints::try_from(invalid_version_ordinal);

        assert!(err.is_err());

        let invalid_single_buffer = StreamBufferConstraints {
            buffer_constraints_version_ordinal: Some(0),
            ..Default::default()
        };

        let err = ValidStreamBufferConstraints::try_from(invalid_single_buffer);

        assert!(err.is_err());

        let invalid_continuous = StreamBufferConstraints {
            buffer_constraints_version_ordinal: Some(0),
            ..Default::default()
        };

        let err = ValidStreamBufferConstraints::try_from(invalid_continuous);

        assert!(err.is_err());
    }
}