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
#![allow(unused)]
use std::{marker::PhantomData, time::{Duration, Instant}, fmt::Debug};

use crate::{platform::platform_impl::{ImplAudioFrame, ImplVideoFrame}, util::*};

/// The rate to capture audio samples
#[derive(Copy, Clone, Debug)]
pub enum AudioSampleRate {
    Hz8000,
    Hz16000,
    Hz24000,
    Hz48000,
}

/// The number of audio channels to capture
#[derive(Copy, Clone, Debug)]
pub enum AudioChannelCount {
    Mono,
    Stereo
}

/// Represents audio channel data in an audio frame
pub enum AudioChannelData<'data> {
    F32(AudioChannelDataSamples<'data, f32>),
    I32(AudioChannelDataSamples<'data, i32>),
    I16(AudioChannelDataSamples<'data, i16>),
}

/// Wraps a "slice" of audio data for one channel, handling data stride
pub struct AudioChannelDataSamples<'data, T> {
    pub(crate) data: *const u8,
    pub(crate) stride: usize,
    pub(crate) length: usize,
    pub(crate) phantom_lifetime: PhantomData<&'data T>,
}

impl<T: Copy> AudioChannelDataSamples<'_, T> {
    /// Get the nth sample for this channel data
    pub fn get(&self, n: usize) -> T {
        let ptr = self.data.wrapping_add(self.stride * n);
        unsafe { *(ptr as *const T) }
    }

    /// Get the length of this sample buffer
    pub fn length(&self) -> usize {
        self.length
    }
}

/// Represents an error getting the data for an audio channel
pub enum AudioBufferError {
    // The audio sample format was not supported
    UnsupportedFormat,
    // The requested channel number wasn't present
    InvalidChannel,
    Other(String)
}

pub(crate) trait AudioCaptureFrame {
    fn sample_rate(&self) -> AudioSampleRate;
    fn channel_count(&self) -> AudioChannelCount;
    fn audio_channel_buffer(&mut self, channel: usize) -> Result<AudioChannelData<'_>, AudioBufferError>;
    fn duration(&self) -> Duration;
    fn origin_time(&self) -> Duration;
    fn frame_id(&self) -> u64;
}

/// A frame of captured audio
pub struct AudioFrame {
    pub(crate) impl_audio_frame: ImplAudioFrame,
}

unsafe impl Send for AudioFrame {}
unsafe impl Sync for AudioFrame {}

impl Debug for AudioFrame {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AudioFrame").finish()
    }
}

impl AudioFrame {
    /// Get the sample rate of the captured audio
    pub fn sample_rate(&self) -> AudioSampleRate {
        self.impl_audio_frame.sample_rate()
    }

    /// Get the channel count of the captured audio
    pub fn channel_count(&self) -> AudioChannelCount {
        self.impl_audio_frame.channel_count()
    }

    /// Get the data buffer for the captured audio channel
    pub fn audio_channel_buffer(&mut self, channel: usize) -> Result<AudioChannelData<'_>, AudioBufferError> {
        self.impl_audio_frame.audio_channel_buffer(channel)
    }

    /// Get the duration of this audio frames
    pub fn duration(&self) -> Duration {
        self.impl_audio_frame.duration()
    }

    /// Get the time since the start of the stream that this audio frame begins at
    pub fn origin_time(&self) -> Duration {
        self.impl_audio_frame.duration()
    }

    /// Get the sequence id of this frame (monotonically increasing)
    /// 
    /// Note: This is separate from video frame ids
    pub fn frame_id(&self) -> u64 {
        self.impl_audio_frame.frame_id()
    }
}

pub(crate) trait VideoCaptureFrame {
    fn size(&self) -> Size;
    fn dpi(&self) -> f64;
    fn duration(&self) -> Duration;
    fn origin_time(&self) -> Duration;
    fn capture_time(&self) -> Instant;
    fn frame_id(&self) -> u64;
    fn content_rect(&self) -> Rect;
}

/// A frame of captured video
pub struct VideoFrame {
    pub(crate) impl_video_frame: ImplVideoFrame,
}

unsafe impl Send for VideoFrame {}
unsafe impl Sync for VideoFrame {}

impl VideoFrame {
    /// Get the sequence id of this video frame (monotonically increasing)
    /// 
    /// Note: This is separate from audio frame ids
    pub fn frame_id(&self) -> u64 {
        self.impl_video_frame.frame_id()
    }

    /// Get the Instant that this frame was delivered to the application
    pub fn capture_time(&self) -> Instant {
        self.impl_video_frame.capture_time()
    }

    /// Get the time since the start of the stream that this frame was generated
    pub fn origin_time(&self) -> Duration {
        self.impl_video_frame.origin_time()
    }

    /// Get the raw size of the frame
    /// 
    /// For planar image formats, this is the size of the largest plane
    pub fn size(&self) -> Size {
        self.impl_video_frame.size()
    }

    /// Get the dpi of the contents of the frame (accounting for capture scaling)
    pub fn dpi(&self) -> f64 {
        self.impl_video_frame.dpi()
    }

    /// Get the rectangle of the frame representing containing the captured contents
    pub fn content_rect(&self) -> Rect {
        self.impl_video_frame.content_rect()
    }
}

impl Debug for VideoFrame {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("VideoFrame").finish()
    }
}