avplayer 0.7.0

Safe Rust bindings for Apple's AVPlayer + AVAssetReader — playback and frame-by-frame asset reading on macOS
Documentation
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#![allow(
    clippy::missing_errors_doc,
    clippy::must_use_candidate,
    clippy::struct_excessive_bools
)]

use core::ffi::c_char;
use core::ops::{BitOr, BitOrAssign};
use core::ptr;
use std::ffi::CString;

use serde::Deserialize;

use crate::asset::Size;
use crate::error::{from_swift, AVPlayerError};
use crate::ffi;
use crate::player::PlayerItem;
use crate::time::TimeRange;
use crate::util::parse_json_and_free;

#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ExtendedPlayerItemInfoPayload {
    automatically_loaded_asset_keys: Vec<String>,
    seekable_time_ranges: Vec<TimeRange>,
    loaded_time_ranges: Vec<TimeRange>,
    can_use_network_resources_for_live_streaming_while_paused: bool,
    preferred_forward_buffer_duration: f64,
    preferred_peak_bit_rate: f64,
    preferred_peak_bit_rate_for_expensive_networks: f64,
    preferred_maximum_resolution: Size,
    preferred_maximum_resolution_for_expensive_networks: Size,
    audio_time_pitch_algorithm: String,
    output_count: usize,
    track_count: usize,
    variant_preferences: Option<u64>,
    authorization_required_for_playback: bool,
    application_authorized_for_playback: bool,
    content_authorized_for_playback: bool,
    content_authorization_request_status: i32,
    custom_video_compositor: Option<PlayerItemVideoCompositorPayload>,
}

/// Mirrors the `AVPlayer` framework counterpart for `AudioTimePitchAlgorithm`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum AudioTimePitchAlgorithm {
    /// Mirrors the `AVPlayer` framework case `Spectral`.
    Spectral,
    /// Mirrors the `AVPlayer` framework case `TimeDomain`.
    TimeDomain,
    /// Mirrors the `AVPlayer` framework case `Varispeed`.
    Varispeed,
    /// Mirrors the `AVPlayer` framework case `LowQualityZeroLatency`.
    LowQualityZeroLatency,
    /// Mirrors the `AVPlayer` framework case `Unknown`.
    Unknown(String),
}

impl AudioTimePitchAlgorithm {
    /// Calls the `AVPlayer` framework counterpart for `from_raw`.
    #[must_use]
    pub fn from_raw(raw: &str) -> Self {
        match raw {
            "spectral" => Self::Spectral,
            "time_domain" => Self::TimeDomain,
            "varispeed" => Self::Varispeed,
            "low_quality_zero_latency" => Self::LowQualityZeroLatency,
            other => Self::Unknown(other.to_owned()),
        }
    }

    /// Calls the `AVPlayer` framework counterpart for `as_raw`.
    #[must_use]
    pub fn as_raw(&self) -> &str {
        match self {
            Self::Spectral => "spectral",
            Self::TimeDomain => "time_domain",
            Self::Varispeed => "varispeed",
            Self::LowQualityZeroLatency => "low_quality_zero_latency",
            Self::Unknown(raw) => raw,
        }
    }
}

/// Mirrors the `AVPlayer` framework counterpart for `VariantPreferences`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct VariantPreferences(u64);

impl VariantPreferences {
    /// Mirrors the `AVPlayer` framework constant `NONE`.
    pub const NONE: Self = Self(0);
    /// Mirrors the `AVPlayer` framework constant `SCALABILITY_TO_LOSSLESS_AUDIO`.
    pub const SCALABILITY_TO_LOSSLESS_AUDIO: Self = Self(1 << 0);

    /// Mirrors the `AVPlayer` framework constant `fn`.
    #[must_use]
    pub const fn bits(self) -> u64 {
        self.0
    }

    /// Mirrors the `AVPlayer` framework constant `fn`.
    #[must_use]
    pub const fn contains(self, other: Self) -> bool {
        (self.0 & other.0) == other.0
    }

    const fn from_bits(bits: u64) -> Self {
        Self(bits)
    }
}

impl BitOr for VariantPreferences {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self::Output {
        Self(self.0 | rhs.0)
    }
}

impl BitOrAssign for VariantPreferences {
    fn bitor_assign(&mut self, rhs: Self) {
        self.0 |= rhs.0;
    }
}

/// Mirrors the `AVPlayer` framework counterpart for `ContentAuthorizationStatus`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ContentAuthorizationStatus {
    /// Mirrors the `AVPlayer` framework case `Unknown`.
    Unknown,
    /// Mirrors the `AVPlayer` framework case `Completed`.
    Completed,
    /// Mirrors the `AVPlayer` framework case `Cancelled`.
    Cancelled,
    /// Mirrors the `AVPlayer` framework case `TimedOut`.
    TimedOut,
    /// Mirrors the `AVPlayer` framework case `Busy`.
    Busy,
    /// Mirrors the `AVPlayer` framework case `NotAvailable`.
    NotAvailable,
    /// Mirrors the `AVPlayer` framework case `NotPossible`.
    NotPossible,
    /// Mirrors the `AVPlayer` framework case `Other`.
    Other(i32),
}

impl ContentAuthorizationStatus {
    const fn from_raw(raw: i32) -> Self {
        match raw {
            0 => Self::Unknown,
            1 => Self::Completed,
            2 => Self::Cancelled,
            3 => Self::TimedOut,
            4 => Self::Busy,
            5 => Self::NotAvailable,
            6 => Self::NotPossible,
            other => Self::Other(other),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
struct PlayerItemVideoCompositorPayload {
    class_name: String,
    supports_wide_color_source_frames: Option<bool>,
    supports_hdr_source_frames: Option<bool>,
    supports_source_tagged_buffers: Option<bool>,
    can_conform_color_of_source_frames: Option<bool>,
}

/// Mirrors the `AVPlayer` framework counterpart for `PlayerItemVideoCompositorInfo`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PlayerItemVideoCompositorInfo {
    /// Mirrors the `AVPlayer` framework property for `class_name`.
    pub class_name: String,
    /// Mirrors the `AVPlayer` framework property for `supports_wide_color_source_frames`.
    pub supports_wide_color_source_frames: Option<bool>,
    /// Mirrors the `AVPlayer` framework property for `supports_hdr_source_frames`.
    pub supports_hdr_source_frames: Option<bool>,
    /// Mirrors the `AVPlayer` framework property for `supports_source_tagged_buffers`.
    pub supports_source_tagged_buffers: Option<bool>,
    /// Mirrors the `AVPlayer` framework property for `can_conform_color_of_source_frames`.
    pub can_conform_color_of_source_frames: Option<bool>,
}

impl From<PlayerItemVideoCompositorPayload> for PlayerItemVideoCompositorInfo {
    fn from(payload: PlayerItemVideoCompositorPayload) -> Self {
        Self {
            class_name: payload.class_name,
            supports_wide_color_source_frames: payload.supports_wide_color_source_frames,
            supports_hdr_source_frames: payload.supports_hdr_source_frames,
            supports_source_tagged_buffers: payload.supports_source_tagged_buffers,
            can_conform_color_of_source_frames: payload.can_conform_color_of_source_frames,
        }
    }
}

impl PlayerItem {
    fn extended_info(&self) -> Result<ExtendedPlayerItemInfoPayload, AVPlayerError> {
        let mut err: *mut c_char = ptr::null_mut();
        let json_ptr = unsafe { ffi::av_player_item_info_json(self.ptr, &mut err) };
        if json_ptr.is_null() {
            return Err(unsafe { from_swift(ffi::status::OPERATION_FAILED, err) });
        }
        parse_json_and_free(json_ptr)
    }

    /// Calls the `AVPlayer` framework counterpart for `automatically_loaded_asset_keys`.
    pub fn automatically_loaded_asset_keys(&self) -> Result<Vec<String>, AVPlayerError> {
        Ok(self.extended_info()?.automatically_loaded_asset_keys)
    }

    /// Calls the `AVPlayer` framework counterpart for `seekable_time_ranges`.
    pub fn seekable_time_ranges(&self) -> Result<Vec<TimeRange>, AVPlayerError> {
        Ok(self.extended_info()?.seekable_time_ranges)
    }

    /// Calls the `AVPlayer` framework counterpart for `loaded_time_ranges`.
    pub fn loaded_time_ranges(&self) -> Result<Vec<TimeRange>, AVPlayerError> {
        Ok(self.extended_info()?.loaded_time_ranges)
    }

    /// Calls the `AVPlayer` framework counterpart for `can_use_network_resources_for_live_streaming_while_paused`.
    pub fn can_use_network_resources_for_live_streaming_while_paused(
        &self,
    ) -> Result<bool, AVPlayerError> {
        Ok(self
            .extended_info()?
            .can_use_network_resources_for_live_streaming_while_paused)
    }

    /// Calls the `AVPlayer` framework counterpart for `set_can_use_network_resources_for_live_streaming_while_paused`.
    pub fn set_can_use_network_resources_for_live_streaming_while_paused(&self, enabled: bool) {
        unsafe {
            ffi::av_player_item_set_can_use_network_resources_for_live_streaming_while_paused(
                self.ptr, enabled,
            );
        }
    }

    /// Calls the `AVPlayer` framework counterpart for `preferred_forward_buffer_duration`.
    pub fn preferred_forward_buffer_duration(&self) -> Result<f64, AVPlayerError> {
        Ok(self.extended_info()?.preferred_forward_buffer_duration)
    }

    /// Calls the `AVPlayer` framework counterpart for `set_preferred_forward_buffer_duration`.
    pub fn set_preferred_forward_buffer_duration(&self, duration: f64) {
        unsafe { ffi::av_player_item_set_preferred_forward_buffer_duration(self.ptr, duration) };
    }

    /// Calls the `AVPlayer` framework counterpart for `preferred_peak_bit_rate`.
    pub fn preferred_peak_bit_rate(&self) -> Result<f64, AVPlayerError> {
        Ok(self.extended_info()?.preferred_peak_bit_rate)
    }

    /// Calls the `AVPlayer` framework counterpart for `set_preferred_peak_bit_rate`.
    pub fn set_preferred_peak_bit_rate(&self, value: f64) {
        unsafe { ffi::av_player_item_set_preferred_peak_bit_rate(self.ptr, value) };
    }

    /// Calls the `AVPlayer` framework counterpart for `preferred_peak_bit_rate_for_expensive_networks`.
    pub fn preferred_peak_bit_rate_for_expensive_networks(&self) -> Result<f64, AVPlayerError> {
        Ok(self
            .extended_info()?
            .preferred_peak_bit_rate_for_expensive_networks)
    }

    /// Calls the `AVPlayer` framework counterpart for `set_preferred_peak_bit_rate_for_expensive_networks`.
    pub fn set_preferred_peak_bit_rate_for_expensive_networks(&self, value: f64) {
        unsafe {
            ffi::av_player_item_set_preferred_peak_bit_rate_for_expensive_networks(self.ptr, value);
        }
    }

    /// Calls the `AVPlayer` framework counterpart for `preferred_maximum_resolution`.
    pub fn preferred_maximum_resolution(&self) -> Result<Size, AVPlayerError> {
        Ok(self.extended_info()?.preferred_maximum_resolution)
    }

    /// Calls the `AVPlayer` framework counterpart for `set_preferred_maximum_resolution`.
    pub fn set_preferred_maximum_resolution(&self, value: Size) {
        unsafe {
            ffi::av_player_item_set_preferred_maximum_resolution(
                self.ptr,
                value.width,
                value.height,
            );
        }
    }

    /// Calls the `AVPlayer` framework counterpart for `preferred_maximum_resolution_for_expensive_networks`.
    pub fn preferred_maximum_resolution_for_expensive_networks(
        &self,
    ) -> Result<Size, AVPlayerError> {
        Ok(self
            .extended_info()?
            .preferred_maximum_resolution_for_expensive_networks)
    }

    /// Calls the `AVPlayer` framework counterpart for `set_preferred_maximum_resolution_for_expensive_networks`.
    pub fn set_preferred_maximum_resolution_for_expensive_networks(&self, value: Size) {
        unsafe {
            ffi::av_player_item_set_preferred_maximum_resolution_for_expensive_networks(
                self.ptr,
                value.width,
                value.height,
            );
        }
    }

    /// Calls the `AVPlayer` framework counterpart for `audio_time_pitch_algorithm`.
    pub fn audio_time_pitch_algorithm(&self) -> Result<AudioTimePitchAlgorithm, AVPlayerError> {
        Ok(AudioTimePitchAlgorithm::from_raw(
            &self.extended_info()?.audio_time_pitch_algorithm,
        ))
    }

    /// Calls the `AVPlayer` framework counterpart for `set_audio_time_pitch_algorithm`.
    pub fn set_audio_time_pitch_algorithm(
        &self,
        algorithm: &AudioTimePitchAlgorithm,
    ) -> Result<(), AVPlayerError> {
        let algorithm = CString::new(algorithm.as_raw()).map_err(|error| {
            AVPlayerError::InvalidArgument(format!(
                "audio time pitch algorithm contains NUL byte: {error}"
            ))
        })?;
        unsafe { ffi::av_player_item_set_audio_time_pitch_algorithm(self.ptr, algorithm.as_ptr()) };
        Ok(())
    }

    /// Calls the `AVPlayer` framework counterpart for `output_count`.
    pub fn output_count(&self) -> Result<usize, AVPlayerError> {
        Ok(self.extended_info()?.output_count)
    }

    /// Calls the `AVPlayer` framework counterpart for `track_count`.
    pub fn track_count(&self) -> Result<usize, AVPlayerError> {
        Ok(self.extended_info()?.track_count)
    }

    /// Calls the `AVPlayer` framework counterpart for `variant_preferences`.
    pub fn variant_preferences(&self) -> Result<VariantPreferences, AVPlayerError> {
        Ok(VariantPreferences::from_bits(
            self.extended_info()?
                .variant_preferences
                .ok_or_else(|| availability_error("AVPlayerItem.variantPreferences", "11.3"))?,
        ))
    }

    /// Calls the `AVPlayer` framework counterpart for `set_variant_preferences`.
    pub fn set_variant_preferences(
        &self,
        preferences: VariantPreferences,
    ) -> Result<(), AVPlayerError> {
        let mut err: *mut c_char = ptr::null_mut();
        let status = unsafe {
            ffi::av_player_item_set_variant_preferences(self.ptr, preferences.bits(), &mut err)
        };
        if status != ffi::status::OK {
            return Err(unsafe { from_swift(status, err) });
        }
        Ok(())
    }

    /// Calls the `AVPlayer` framework counterpart for `authorization_required_for_playback`.
    pub fn authorization_required_for_playback(&self) -> Result<bool, AVPlayerError> {
        Ok(self.extended_info()?.authorization_required_for_playback)
    }

    /// Calls the `AVPlayer` framework counterpart for `application_authorized_for_playback`.
    pub fn application_authorized_for_playback(&self) -> Result<bool, AVPlayerError> {
        Ok(self.extended_info()?.application_authorized_for_playback)
    }

    /// Calls the `AVPlayer` framework counterpart for `content_authorized_for_playback`.
    pub fn content_authorized_for_playback(&self) -> Result<bool, AVPlayerError> {
        Ok(self.extended_info()?.content_authorized_for_playback)
    }

    /// Calls the `AVPlayer` framework counterpart for `content_authorization_request_status`.
    pub fn content_authorization_request_status(
        &self,
    ) -> Result<ContentAuthorizationStatus, AVPlayerError> {
        Ok(ContentAuthorizationStatus::from_raw(
            self.extended_info()?.content_authorization_request_status,
        ))
    }

    /// Calls the `AVPlayer` framework counterpart for `custom_video_compositor`.
    pub fn custom_video_compositor(
        &self,
    ) -> Result<Option<PlayerItemVideoCompositorInfo>, AVPlayerError> {
        Ok(self
            .extended_info()?
            .custom_video_compositor
            .map(PlayerItemVideoCompositorInfo::from))
    }
}

fn availability_error(symbol: &str, macos_version: &str) -> AVPlayerError {
    AVPlayerError::OperationFailed(format!("{symbol} requires macOS {macos_version}+"))
}