| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | use std::cell::RefCell; |
| 9 | use std::rc::Rc; |
| 10 | |
| 11 | use crate::decoder::stateless::PoolLayer; |
| 12 | use crate::decoder::stateless::StatelessCodec; |
| 13 | use crate::decoder::stateless::StatelessDecoderBackend; |
| 14 | use crate::decoder::stateless::StatelessDecoderBackendPicture; |
| 15 | use crate::decoder::stateless::TryFormat; |
| 16 | use crate::decoder::DecodedHandle; |
| 17 | use crate::decoder::DynHandle; |
| 18 | use crate::decoder::FramePool; |
| 19 | use crate::decoder::MappableHandle; |
| 20 | use crate::decoder::StreamInfo; |
| 21 | use crate::DecodedFormat; |
| 22 | use crate::Resolution; |
| 23 | |
| 24 | #[derive(Default)] |
| 25 | pub struct BackendHandle(()); |
| 26 | |
| 27 | impl MappableHandle for BackendHandle { |
| 28 | fn read(&mut self, _: &mut [u8]) -> anyhow::Result<()> { |
| 29 | Ok(()) |
| 30 | } |
| 31 | |
| 32 | fn image_size(&mut self) -> usize { |
| 33 | 1 |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl<'a> DynHandle for std::cell::RefMut<'a, BackendHandle> { |
| 38 | fn dyn_mappable_handle<'b>(&'b mut self) -> anyhow::Result<Box<dyn MappableHandle + 'b>> { |
| 39 | Ok(Box::<BackendHandle>::default()) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | pub struct Handle { |
| 44 | pub handle: Rc<RefCell<BackendHandle>>, |
| 45 | } |
| 46 | |
| 47 | impl Clone for Handle { |
| 48 | fn clone(&self) -> Self { |
| 49 | Self { handle: Rc::clone(&self.handle) } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | impl DecodedHandle for Handle { |
| 54 | type Descriptor = (); |
| 55 | |
| 56 | fn coded_resolution(&self) -> Resolution { |
| 57 | Default::default() |
| 58 | } |
| 59 | |
| 60 | fn display_resolution(&self) -> Resolution { |
| 61 | Default::default() |
| 62 | } |
| 63 | |
| 64 | fn timestamp(&self) -> u64 { |
| 65 | 0 |
| 66 | } |
| 67 | |
| 68 | fn dyn_picture<'a>(&'a self) -> Box<dyn DynHandle + 'a> { |
| 69 | Box::new(self.handle.borrow()) |
| 70 | } |
| 71 | |
| 72 | fn sync(&self) -> anyhow::Result<()> { |
| 73 | Ok(()) |
| 74 | } |
| 75 | |
| 76 | fn is_ready(&self) -> bool { |
| 77 | true |
| 78 | } |
| 79 | |
| 80 | fn resource(&self) -> std::cell::Ref<()> { |
| 81 | std::cell::Ref::map(self.handle.borrow(), |h| &h.0) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | |
| 86 | pub struct Backend { |
| 87 | stream_info: StreamInfo, |
| 88 | } |
| 89 | |
| 90 | impl Backend { |
| 91 | pub(crate) fn new() -> Self { |
| 92 | Self { |
| 93 | stream_info: StreamInfo { |
| 94 | format: DecodedFormat::I420, |
| 95 | min_num_frames: 4, |
| 96 | coded_resolution: Resolution::from((320, 200)), |
| 97 | display_resolution: Resolution::from((320, 200)), |
| 98 | }, |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | impl FramePool for Backend { |
| 104 | type Descriptor = (); |
| 105 | |
| 106 | fn coded_resolution(&self) -> Resolution { |
| 107 | Resolution::from((320, 200)) |
| 108 | } |
| 109 | |
| 110 | fn set_coded_resolution(&mut self, _resolution: Resolution) {} |
| 111 | |
| 112 | fn add_frames(&mut self, _descriptors: Vec<Self::Descriptor>) -> Result<(), anyhow::Error> { |
| 113 | Ok(()) |
| 114 | } |
| 115 | |
| 116 | fn num_free_frames(&self) -> usize { |
| 117 | 4 |
| 118 | } |
| 119 | |
| 120 | fn num_managed_frames(&self) -> usize { |
| 121 | 4 |
| 122 | } |
| 123 | |
| 124 | fn clear(&mut self) {} |
| 125 | } |
| 126 | |
| 127 | impl<Codec: StatelessCodec> StatelessDecoderBackendPicture<Codec> for Backend { |
| 128 | type Picture = (); |
| 129 | } |
| 130 | |
| 131 | impl<Codec: StatelessCodec> TryFormat<Codec> for Backend { |
| 132 | fn try_format(&mut self, _: &Codec::FormatInfo, _: DecodedFormat) -> anyhow::Result<()> { |
| 133 | Ok(()) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | impl StatelessDecoderBackend for Backend { |
| 138 | type Handle = Handle; |
| 139 | |
| 140 | type FramePool = Self; |
| 141 | |
| 142 | fn stream_info(&self) -> Option<&StreamInfo> { |
| 143 | Some(&self.stream_info) |
| 144 | } |
| 145 | |
| 146 | fn frame_pool(&mut self, _: PoolLayer) -> Vec<&mut Self::FramePool> { |
| 147 | vec![self] |
| 148 | } |
| 149 | } |