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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
use crate::{OtcError, OtcResult};

use std::convert::TryInto;
use std::slice;
use std::sync::atomic::{AtomicPtr, Ordering};

/// Video frame format enumeration.
#[derive(Clone, Copy, Debug)]
pub enum FrameFormat {
    Abgr32,
    Argb32,
    Bgra32,
    Compressed,
    Max,
    Mjpeg,
    Nv12,
    Nv21,
    Rgba32,
    Rgb24,
    Uyvy,
    Yuv420P,
    Yuy2,
    __Unknown,
}

impl From<ffi::otc_video_frame_format> for FrameFormat {
    fn from(type_: ffi::otc_video_frame_format) -> FrameFormat {
        match type_ {
            ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_ABGR32 => FrameFormat::Abgr32,
            ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_ARGB32 => FrameFormat::Argb32,
            ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_BGRA32 => FrameFormat::Bgra32,
            ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_COMPRESSED => {
                FrameFormat::Compressed
            }
            ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_MAX => FrameFormat::Max,
            ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_MJPEG => FrameFormat::Mjpeg,
            ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_NV12 => FrameFormat::Nv12,
            ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_NV21 => FrameFormat::Nv21,
            ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_RGBA32 => FrameFormat::Rgba32,
            ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_RGB24 => FrameFormat::Rgb24,
            ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_UYVY => FrameFormat::Uyvy,
            ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_YUV420P => FrameFormat::Yuv420P,
            ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_YUY2 => FrameFormat::Yuy2,
            _ => FrameFormat::__Unknown,
        }
    }
}

impl From<FrameFormat> for ffi::otc_video_frame_format {
    fn from(type_: FrameFormat) -> ffi::otc_video_frame_format {
        match type_ {
            FrameFormat::Abgr32 => ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_ABGR32,
            FrameFormat::Argb32 => ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_ARGB32,
            FrameFormat::Bgra32 => ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_BGRA32,
            FrameFormat::Compressed => {
                ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_COMPRESSED
            }
            FrameFormat::Max => ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_MAX,
            FrameFormat::Mjpeg => ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_MJPEG,
            FrameFormat::Nv12 => ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_NV12,
            FrameFormat::Nv21 => ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_NV21,
            FrameFormat::Rgba32 => ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_RGBA32,
            FrameFormat::Rgb24 => ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_RGB24,
            FrameFormat::Uyvy => ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_UYVY,
            FrameFormat::Yuv420P => ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_YUV420P,
            FrameFormat::Yuy2 => ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_YUY2,
            FrameFormat::__Unknown => ffi::otc_video_frame_format_OTC_VIDEO_FRAME_FORMAT_UNKNOWN,
        }
    }
}

/// Video frame video plane enumeration.
pub enum FramePlane {
    Packed,
    U,
    UvInterleaved,
    V,
    VuInterleaved,
    Y,
    __Unknown,
}

impl From<ffi::otc_video_frame_plane> for FramePlane {
    fn from(plane: ffi::otc_video_frame_plane) -> FramePlane {
        match plane {
            ffi::otc_video_frame_plane_OTC_VIDEO_FRAME_PLANE_PACKED => FramePlane::Packed,
            ffi::otc_video_frame_plane_OTC_VIDEO_FRAME_PLANE_U => FramePlane::U,
            ffi::otc_video_frame_plane_OTC_VIDEO_FRAME_PLANE_UV_INTERLEAVED => {
                FramePlane::UvInterleaved
            }
            ffi::otc_video_frame_plane_OTC_VIDEO_FRAME_PLANE_V => FramePlane::V,
            ffi::otc_video_frame_plane_OTC_VIDEO_FRAME_PLANE_VU_INTERLEAVED => {
                FramePlane::VuInterleaved
            }
            ffi::otc_video_frame_plane_OTC_VIDEO_FRAME_PLANE_Y => FramePlane::Y,
            _ => FramePlane::__Unknown,
        }
    }
}

impl From<FramePlane> for ffi::otc_video_frame_plane {
    fn from(plane: FramePlane) -> ffi::otc_video_frame_plane {
        match plane {
            FramePlane::Packed => ffi::otc_video_frame_plane_OTC_VIDEO_FRAME_PLANE_PACKED,
            FramePlane::U => ffi::otc_video_frame_plane_OTC_VIDEO_FRAME_PLANE_U,
            FramePlane::UvInterleaved => {
                ffi::otc_video_frame_plane_OTC_VIDEO_FRAME_PLANE_UV_INTERLEAVED
            }
            FramePlane::V => ffi::otc_video_frame_plane_OTC_VIDEO_FRAME_PLANE_V,
            FramePlane::VuInterleaved => {
                ffi::otc_video_frame_plane_OTC_VIDEO_FRAME_PLANE_VU_INTERLEAVED
            }
            FramePlane::Y => ffi::otc_video_frame_plane_OTC_VIDEO_FRAME_PLANE_Y,
            FramePlane::__Unknown => u32::MAX,
        }
    }
}

pub struct VideoFrame {
    ptr: AtomicPtr<*const ffi::otc_video_frame>,
}

impl VideoFrame {
    pub fn inner(&self) -> *const ffi::otc_video_frame {
        self.ptr.load(Ordering::Relaxed) as *const _
    }

    pub fn new(format: FrameFormat, width: i32, height: i32, buffer: Vec<u8>) -> Self {
        Self {
            ptr: AtomicPtr::new(unsafe {
                ffi::otc_video_frame_new(format.into(), width, height, buffer.as_ptr()) as *mut _
            }),
        }
    }

    pub fn new_mjpeg(width: i32, height: i32, buffer: Vec<u8>, size: usize) -> Self {
        Self {
            ptr: AtomicPtr::new(unsafe {
                ffi::otc_video_frame_new_MJPEG(
                    width,
                    height,
                    buffer.as_ptr(),
                    size.try_into().expect("usize to size_t cast"),
                ) as *mut _
            }),
        }
    }

    pub fn new_compressed(width: i32, height: i32, buffer: Vec<u8>, size: usize) -> Self {
        Self {
            ptr: AtomicPtr::new(unsafe {
                ffi::otc_video_frame_new_compressed(
                    width,
                    height,
                    buffer.as_ptr(),
                    size.try_into().expect("usize to size_t cast"),
                )
            } as *mut _),
        }
    }

    // FIXME: implement more constructors as needed.

    pub fn get_buffer(&self) -> Result<&[u8], OtcError> {
        let ptr = self.ptr.load(Ordering::Relaxed);
        if ptr.is_null() {
            return Err(OtcError::NullError);
        }
        let data = unsafe { ffi::otc_video_frame_get_buffer(ptr as *const _) };
        let size = unsafe { ffi::otc_video_frame_get_buffer_size(ptr as *const _) };
        Ok(unsafe { slice::from_raw_parts(data, size.try_into().expect("u64 to usize cast")) })
    }

    pub fn get_timestamp(&self) -> Result<i64, OtcError> {
        let ptr = self.ptr.load(Ordering::Relaxed);
        if ptr.is_null() {
            return Err(OtcError::NullError);
        }
        Ok(unsafe { ffi::otc_video_frame_get_timestamp(ptr as *const _) })
    }

    pub fn set_timestamp(&mut self, timestamp: i64) -> OtcResult {
        let ptr = self.ptr.load(Ordering::Relaxed);
        if ptr.is_null() {
            return Err(OtcError::NullError);
        }
        unsafe {
            ffi::otc_video_frame_set_timestamp(ptr as *mut _, timestamp);
        }
        Ok(())
    }

    pub fn get_width(&self) -> Result<i32, OtcError> {
        let ptr = self.ptr.load(Ordering::Relaxed);
        if ptr.is_null() {
            return Err(OtcError::NullError);
        }
        Ok(unsafe { ffi::otc_video_frame_get_width(ptr as *const _) })
    }

    pub fn get_height(&self) -> Result<i32, OtcError> {
        let ptr = self.ptr.load(Ordering::Relaxed);
        if ptr.is_null() {
            return Err(OtcError::NullError);
        }
        Ok(unsafe { ffi::otc_video_frame_get_height(ptr as *const _) })
    }

    pub fn get_number_of_planes(&self) -> Result<usize, OtcError> {
        let ptr = self.ptr.load(Ordering::Relaxed);
        if ptr.is_null() {
            return Err(OtcError::NullError);
        }
        Ok(unsafe {
            ffi::otc_video_frame_get_number_of_planes(ptr as *const _)
                .try_into()
                .expect("u64 to usize cast")
        })
    }

    pub fn get_format(&self) -> Result<FrameFormat, OtcError> {
        let ptr = self.ptr.load(Ordering::Relaxed);
        if ptr.is_null() {
            return Err(OtcError::NullError);
        }
        Ok(unsafe { ffi::otc_video_frame_get_format(ptr as *const _) }.into())
    }

    pub fn set_format(&mut self, format: FrameFormat) -> OtcResult {
        let ptr = self.ptr.load(Ordering::Relaxed);
        if ptr.is_null() {
            return Err(OtcError::NullError);
        }
        unsafe {
            ffi::otc_video_frame_set_format(ptr as *mut _, format.into());
        }
        Ok(())
    }

    pub fn convert(&mut self, format: FrameFormat) -> Result<VideoFrame, OtcError> {
        let ptr = self.ptr.load(Ordering::Relaxed);
        if ptr.is_null() {
            return Err(OtcError::NullError);
        }
        Ok(
            ((unsafe { ffi::otc_video_frame_convert(format.into(), ptr as *mut _) })
                as *const ffi::otc_video_frame)
                .into(),
        )
    }

    pub fn get_plane_size(&self, plane: FramePlane) -> Result<usize, OtcError> {
        let ptr = self.ptr.load(Ordering::Relaxed);
        if ptr.is_null() {
            return Err(OtcError::NullError);
        }
        Ok(unsafe { ffi::otc_video_frame_get_plane_size(ptr as *mut _, plane.into()) as usize })
    }

    pub fn get_plane_stride(&self, plane: FramePlane) -> Result<i32, OtcError> {
        let ptr = self.ptr.load(Ordering::Relaxed);
        if ptr.is_null() {
            return Err(OtcError::NullError);
        }
        Ok(unsafe { ffi::otc_video_frame_get_plane_stride(ptr as *mut _, plane.into()) })
    }
}

impl Clone for VideoFrame {
    fn clone(&self) -> Self {
        (self.ptr.load(Ordering::Relaxed) as *const ffi::otc_video_frame).into()
    }
}

impl Drop for VideoFrame {
    fn drop(&mut self) {
        let ptr = self.ptr.load(Ordering::Relaxed);

        if ptr.is_null() {
            return;
        }

        unsafe {
            ffi::otc_video_frame_delete(ptr as *mut _);
        }

        self.ptr.store(std::ptr::null_mut(), Ordering::Relaxed);
    }
}

impl From<*const ffi::otc_video_frame> for VideoFrame {
    #[allow(clippy::not_unsafe_ptr_arg_deref)]
    fn from(ptr: *const ffi::otc_video_frame) -> Self {
        let ptr = unsafe { ffi::otc_video_frame_copy(ptr) };
        Self {
            ptr: AtomicPtr::new(ptr as *mut _),
        }
    }
}