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::{BitrateMode, 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 BitrateMode, 58 BitratePeak, 59 PrependSpsPpsToIdr, 60 } 61 62 #[derive(Debug, Clone)] 63 pub enum CtrlVal { 64 Bitrate(u32), 65 Profile(Profile), 66 Level(Level), 67 ForceKeyframe, 68 BitrateMode(BitrateMode), 69 BitratePeak(u32), 70 PrependSpsPpsToIdr(bool), 71 } 72 73 impl Response for CtrlVal { write(&self, w: &mut Writer) -> Result<(), io::Error>74 fn write(&self, w: &mut Writer) -> Result<(), io::Error> { 75 match self { 76 CtrlVal::Bitrate(r) => w.write_obj(virtio_video_control_val_bitrate { 77 bitrate: Le32::from(*r), 78 ..Default::default() 79 }), 80 CtrlVal::BitratePeak(r) => w.write_obj(virtio_video_control_val_bitrate_peak { 81 bitrate_peak: Le32::from(*r), 82 ..Default::default() 83 }), 84 CtrlVal::BitrateMode(m) => w.write_obj(virtio_video_control_val_bitrate_mode { 85 bitrate_mode: Le32::from(*m as u32), 86 ..Default::default() 87 }), 88 CtrlVal::Profile(p) => w.write_obj(virtio_video_control_val_profile { 89 profile: Le32::from(*p as u32), 90 ..Default::default() 91 }), 92 CtrlVal::Level(l) => w.write_obj(virtio_video_control_val_level { 93 level: Le32::from(*l as u32), 94 ..Default::default() 95 }), 96 CtrlVal::ForceKeyframe => Err(io::Error::new( 97 io::ErrorKind::InvalidInput, 98 "Button controls should not be queried.", 99 )), 100 CtrlVal::PrependSpsPpsToIdr(p) => { 101 w.write_obj(virtio_video_control_val_prepend_spspps_to_idr { 102 prepend_spspps_to_idr: Le32::from(*p as u32), 103 ..Default::default() 104 }) 105 } 106 } 107 } 108 } 109