1 // Copyright 2020 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 //! Implementation of data structures for virtio-video controls.
6 
7 use std::convert::From;
8 use std::io;
9 
10 use data_model::Le32;
11 
12 use crate::virtio::video::format::{Format, Level, Profile};
13 use crate::virtio::video::protocol::*;
14 use crate::virtio::video::response::Response;
15 use crate::virtio::Writer;
16 
17 #[derive(Debug)]
18 pub enum QueryCtrlType {
19     Profile(Format),
20     Level(Format),
21 }
22 
23 #[derive(Debug, Clone)]
24 pub enum QueryCtrlResponse {
25     Profile(Vec<Profile>),
26     #[allow(dead_code)]
27     Level(Vec<Level>),
28 }
29 
30 impl Response for QueryCtrlResponse {
write(&self, w: &mut Writer) -> Result<(), io::Error>31     fn write(&self, w: &mut Writer) -> Result<(), io::Error> {
32         match self {
33             QueryCtrlResponse::Profile(ps) => {
34                 w.write_obj(virtio_video_query_control_resp_profile {
35                     num: Le32::from(ps.len() as u32),
36                     ..Default::default()
37                 })?;
38                 w.write_iter(ps.iter().map(|p| Le32::from(*p as u32)))
39             }
40             QueryCtrlResponse::Level(ls) => {
41                 w.write_obj(virtio_video_query_control_resp_level {
42                     num: Le32::from(ls.len() as u32),
43                     ..Default::default()
44                 })?;
45                 w.write_iter(ls.iter().map(|l| Le32::from(*l as u32)))
46             }
47         }
48     }
49 }
50 
51 #[derive(Debug)]
52 pub enum CtrlType {
53     Bitrate,
54     Profile,
55     Level,
56     ForceKeyframe,
57 }
58 
59 #[derive(Debug, Clone)]
60 pub enum CtrlVal {
61     Bitrate(u32),
62     Profile(Profile),
63     Level(Level),
64     ForceKeyframe(),
65 }
66 
67 impl Response for CtrlVal {
write(&self, w: &mut Writer) -> Result<(), io::Error>68     fn write(&self, w: &mut Writer) -> Result<(), io::Error> {
69         match self {
70             CtrlVal::Bitrate(r) => w.write_obj(virtio_video_control_val_bitrate {
71                 bitrate: Le32::from(*r),
72                 ..Default::default()
73             }),
74             CtrlVal::Profile(p) => w.write_obj(virtio_video_control_val_profile {
75                 profile: Le32::from(*p as u32),
76                 ..Default::default()
77             }),
78             CtrlVal::Level(l) => w.write_obj(virtio_video_control_val_level {
79                 level: Le32::from(*l as u32),
80                 ..Default::default()
81             }),
82             CtrlVal::ForceKeyframe() => Err(io::Error::new(
83                 io::ErrorKind::InvalidInput,
84                 "Button controls should not be queried.",
85             )),
86         }
87     }
88 }
89