|
| 1 | +#[cfg(test)] |
| 2 | +mod video_orientation_extension_test; |
| 3 | + |
| 4 | +use std::convert::{TryFrom, TryInto}; |
| 5 | + |
| 6 | +use bytes::BufMut; |
| 7 | +use util::{marshal::Unmarshal, Marshal, MarshalSize}; |
| 8 | + |
| 9 | +use crate::Error; |
| 10 | + |
| 11 | +// One byte header size |
| 12 | +pub const VIDEO_ORIENTATION_EXTENSION_SIZE: usize = 1; |
| 13 | + |
| 14 | +/// Coordination of Video Orientation in RTP streams. |
| 15 | +/// |
| 16 | +/// Coordination of Video Orientation consists in signaling of the current |
| 17 | +/// orientation of the image captured on the sender side to the receiver for |
| 18 | +/// appropriate rendering and displaying. |
| 19 | +/// |
| 20 | +/// C = Camera: indicates the direction of the camera used for this video |
| 21 | +/// stream. It can be used by the MTSI client in receiver to e.g. display |
| 22 | +/// the received video differently depending on the source camera. |
| 23 | +/// |
| 24 | +/// 0: Front-facing camera, facing the user. If camera direction is |
| 25 | +/// unknown by the sending MTSI client in the terminal then this is the |
| 26 | +/// default value used. |
| 27 | +/// 1: Back-facing camera, facing away from the user. |
| 28 | +/// |
| 29 | +/// F = Flip: indicates a horizontal (left-right flip) mirror operation on |
| 30 | +/// the video as sent on the link. |
| 31 | +/// |
| 32 | +/// 0 1 |
| 33 | +/// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 34 | +/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 35 | +/// | ID | len=0 |0 0 0 0 C F R R| |
| 36 | +/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 37 | +#[derive(PartialEq, Eq, Debug, Default, Copy, Clone)] |
| 38 | +pub struct VideoOrientationExtension { |
| 39 | + pub direction: CameraDirection, |
| 40 | + pub flip: bool, |
| 41 | + pub rotation: VideoRotation, |
| 42 | +} |
| 43 | + |
| 44 | +#[derive(PartialEq, Eq, Debug, Default, Copy, Clone)] |
| 45 | +pub enum CameraDirection { |
| 46 | + #[default] |
| 47 | + Front = 0, |
| 48 | + Back = 1, |
| 49 | +} |
| 50 | + |
| 51 | +#[derive(PartialEq, Eq, Debug, Default, Copy, Clone)] |
| 52 | +pub enum VideoRotation { |
| 53 | + #[default] |
| 54 | + Degree0 = 0, |
| 55 | + Degree90 = 1, |
| 56 | + Degree180 = 2, |
| 57 | + Degree270 = 3, |
| 58 | +} |
| 59 | + |
| 60 | +impl MarshalSize for VideoOrientationExtension { |
| 61 | + fn marshal_size(&self) -> usize { |
| 62 | + VIDEO_ORIENTATION_EXTENSION_SIZE |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl Unmarshal for VideoOrientationExtension { |
| 67 | + fn unmarshal<B>(buf: &mut B) -> util::Result<Self> |
| 68 | + where |
| 69 | + Self: Sized, |
| 70 | + B: bytes::Buf, |
| 71 | + { |
| 72 | + if buf.remaining() < VIDEO_ORIENTATION_EXTENSION_SIZE { |
| 73 | + return Err(Error::ErrBufferTooSmall.into()); |
| 74 | + } |
| 75 | + |
| 76 | + let b = buf.get_u8(); |
| 77 | + |
| 78 | + let c = (b & 0b1000) >> 3; |
| 79 | + let f = b & 0b0100; |
| 80 | + let r = b & 0b0011; |
| 81 | + |
| 82 | + Ok(VideoOrientationExtension { |
| 83 | + direction: c.try_into()?, |
| 84 | + flip: f > 0, |
| 85 | + rotation: r.try_into()?, |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl Marshal for VideoOrientationExtension { |
| 91 | + fn marshal_to(&self, mut buf: &mut [u8]) -> util::Result<usize> { |
| 92 | + let c = (self.direction as u8) << 3; |
| 93 | + let f = if self.flip { 0b0100 } else { 0 }; |
| 94 | + let r = self.rotation as u8; |
| 95 | + |
| 96 | + buf.put_u8(c | f | r); |
| 97 | + |
| 98 | + Ok(VIDEO_ORIENTATION_EXTENSION_SIZE) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl TryFrom<u8> for CameraDirection { |
| 103 | + type Error = util::Error; |
| 104 | + |
| 105 | + fn try_from(value: u8) -> Result<Self, Self::Error> { |
| 106 | + match value { |
| 107 | + 0 => Ok(CameraDirection::Front), |
| 108 | + 1 => Ok(CameraDirection::Back), |
| 109 | + _ => Err(util::Error::Other(format!( |
| 110 | + "Unhandled camera direction: {}", |
| 111 | + value |
| 112 | + ))), |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +impl TryFrom<u8> for VideoRotation { |
| 118 | + type Error = util::Error; |
| 119 | + |
| 120 | + fn try_from(value: u8) -> Result<Self, Self::Error> { |
| 121 | + match value { |
| 122 | + 0 => Ok(VideoRotation::Degree0), |
| 123 | + 1 => Ok(VideoRotation::Degree90), |
| 124 | + 2 => Ok(VideoRotation::Degree180), |
| 125 | + 3 => Ok(VideoRotation::Degree270), |
| 126 | + _ => Err(util::Error::Other(format!( |
| 127 | + "Unhandled video rotation: {}", |
| 128 | + value |
| 129 | + ))), |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments