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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
#![allow(
    clippy::missing_errors_doc,
    clippy::module_name_repetitions,
    clippy::must_use_candidate,
    clippy::struct_excessive_bools
)]

use core::ffi::{c_char, c_void, CStr};
use core::ptr;
use std::ffi::CString;
use std::panic::AssertUnwindSafe;
use std::path::Path;

use doom_fish_utils::stream::{BoundedAsyncStream, NextItem};
use serde::{Deserialize, Serialize};

use crate::asset::UrlAsset;
use crate::error::{from_swift, AVPlayerError};
use crate::ffi;
use crate::util::{json_cstring, maybe_json_cstring, parse_json_and_free, to_cstring};

#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ContentKeySystem {
    FairPlayStreaming,
    ClearKey,
    AuthorizationToken,
    Unknown(String),
}

impl ContentKeySystem {
    fn from_raw(raw: &str) -> Self {
        match raw {
            "fair_play_streaming" => Self::FairPlayStreaming,
            "clear_key" => Self::ClearKey,
            "authorization_token" => Self::AuthorizationToken,
            other => Self::Unknown(other.to_owned()),
        }
    }

    fn as_raw(&self) -> &str {
        match self {
            Self::FairPlayStreaming => "fair_play_streaming",
            Self::ClearKey => "clear_key",
            Self::AuthorizationToken => "authorization_token",
            Self::Unknown(raw) => raw,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ContentKeyIdentifier {
    String(String),
    Url(String),
    Data(Vec<u8>),
    Number(i64),
}

impl ContentKeyIdentifier {
    #[must_use]
    pub fn string(value: impl Into<String>) -> Self {
        Self::String(value.into())
    }

    #[must_use]
    pub fn url(value: impl Into<String>) -> Self {
        Self::Url(value.into())
    }

    #[must_use]
    pub fn data(bytes: impl Into<Vec<u8>>) -> Self {
        Self::Data(bytes.into())
    }

    #[must_use]
    pub const fn number(value: i64) -> Self {
        Self::Number(value)
    }

    fn to_payload(&self) -> ContentKeyIdentifierPayload {
        match self {
            Self::String(value) => ContentKeyIdentifierPayload {
                kind: "string".into(),
                value: Some(value.clone()),
                bytes: None,
                number_value: None,
            },
            Self::Url(value) => ContentKeyIdentifierPayload {
                kind: "url".into(),
                value: Some(value.clone()),
                bytes: None,
                number_value: None,
            },
            Self::Data(bytes) => ContentKeyIdentifierPayload {
                kind: "data".into(),
                value: None,
                bytes: Some(bytes.clone()),
                number_value: None,
            },
            Self::Number(value) => ContentKeyIdentifierPayload {
                kind: "number".into(),
                value: None,
                bytes: None,
                number_value: Some(*value),
            },
        }
    }

    fn from_payload(payload: ContentKeyIdentifierPayload) -> Self {
        match payload.kind.as_str() {
            "url" => Self::Url(payload.value.unwrap_or_default()),
            "data" => Self::Data(payload.bytes.unwrap_or_default()),
            "number" => Self::Number(payload.number_value.unwrap_or_default()),
            _ => Self::String(payload.value.unwrap_or_default()),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ContentKeyIdentifierPayload {
    kind: String,
    value: Option<String>,
    bytes: Option<Vec<u8>>,
    number_value: Option<i64>,
}

#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ContentKeyRequestOptions {
    #[serde(default)]
    pub protocol_versions: Vec<i32>,
    pub should_randomize_device_identifier: Option<bool>,
    pub random_device_identifier_seed: Option<Vec<u8>>,
}

impl ContentKeyRequestOptions {
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.protocol_versions.is_empty()
            && self.should_randomize_device_identifier.is_none()
            && self.random_device_identifier_seed.is_none()
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ContentKeyRequestRetryReason {
    TimedOut,
    ReceivedResponseWithExpiredLease,
    ReceivedObsoleteContentKey,
    Unknown(String),
}

impl ContentKeyRequestRetryReason {
    fn from_raw(raw: &str) -> Self {
        match raw {
            "timed_out" => Self::TimedOut,
            "received_response_with_expired_lease" => Self::ReceivedResponseWithExpiredLease,
            "received_obsolete_content_key" => Self::ReceivedObsoleteContentKey,
            other => Self::Unknown(other.to_owned()),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ContentKeyRequestStatus {
    RequestingResponse,
    ReceivedResponse,
    Renewed,
    Retried,
    Cancelled,
    Failed,
    Unknown(i32),
}

impl ContentKeyRequestStatus {
    const fn from_raw(raw: i32) -> Self {
        match raw {
            0 => Self::RequestingResponse,
            1 => Self::ReceivedResponse,
            2 => Self::Renewed,
            3 => Self::Retried,
            4 => Self::Cancelled,
            5 => Self::Failed,
            other => Self::Unknown(other),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ExternalContentProtectionStatus {
    Pending,
    Sufficient,
    Insufficient,
    Unknown(i32),
}

impl ExternalContentProtectionStatus {
    const fn from_raw(raw: i32) -> Self {
        match raw {
            0 => Self::Pending,
            1 => Self::Sufficient,
            2 => Self::Insufficient,
            other => Self::Unknown(other),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ContentKeySessionInfoPayload {
    key_system: String,
    storage_url: Option<String>,
    content_protection_session_identifier_base64: Option<String>,
    recipient_count: usize,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ContentKeyRequestInfoPayload {
    status: i32,
    error_message: Option<String>,
    identifier: Option<ContentKeyIdentifierPayload>,
    initialization_data: Option<Vec<u8>>,
    options: Option<ContentKeyRequestOptions>,
    can_provide_persistable_content_key: bool,
    renews_expiring_response_data: bool,
    has_content_key_specifier: bool,
    has_content_key: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ContentKeySpecifierInfoPayload {
    key_system: String,
    identifier: Option<ContentKeyIdentifierPayload>,
    options: Option<ContentKeyRequestOptions>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ContentKeyInfoPayload {
    external_content_protection_status: Option<i32>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ContentKeyBytesPayload {
    bytes: Option<Vec<u8>>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ContentKeySessionEventPayload {
    event: String,
    request_ptr: Option<u64>,
    key_request_ptrs: Option<Vec<u64>>,
    content_key_ptr: Option<u64>,
    initialization_data: Option<Vec<u8>>,
    persistable_content_key: Option<Vec<u8>>,
    key_identifier: Option<ContentKeyIdentifierPayload>,
    error_message: Option<String>,
    retry_reason: Option<String>,
}

#[derive(Debug)]
pub struct ContentKeySession {
    ptr: *mut c_void,
}

impl Drop for ContentKeySession {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe { ffi::av_ns_object_release(self.ptr) };
            self.ptr = ptr::null_mut();
        }
    }
}

#[derive(Debug)]
pub struct ContentKeyRequest {
    ptr: *mut c_void,
}

impl Drop for ContentKeyRequest {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe { ffi::av_ns_object_release(self.ptr) };
            self.ptr = ptr::null_mut();
        }
    }
}

#[derive(Debug)]
pub struct PersistableContentKeyRequest {
    ptr: *mut c_void,
}

impl Drop for PersistableContentKeyRequest {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe { ffi::av_ns_object_release(self.ptr) };
            self.ptr = ptr::null_mut();
        }
    }
}

#[derive(Debug)]
pub struct ContentKeyResponse {
    ptr: *mut c_void,
}

impl Drop for ContentKeyResponse {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe { ffi::av_ns_object_release(self.ptr) };
            self.ptr = ptr::null_mut();
        }
    }
}

#[derive(Debug)]
pub struct ContentKeySpecifier {
    ptr: *mut c_void,
}

impl Drop for ContentKeySpecifier {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe { ffi::av_ns_object_release(self.ptr) };
            self.ptr = ptr::null_mut();
        }
    }
}

#[derive(Debug)]
pub struct ContentKey {
    ptr: *mut c_void,
}

impl Drop for ContentKey {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe { ffi::av_ns_object_release(self.ptr) };
            self.ptr = ptr::null_mut();
        }
    }
}

#[derive(Debug)]
pub struct ContentKeySessionObserver {
    token: *mut c_void,
}

impl Drop for ContentKeySessionObserver {
    fn drop(&mut self) {
        if !self.token.is_null() {
            unsafe { ffi::av_content_key_session_observer_release(self.token) };
            self.token = ptr::null_mut();
        }
    }
}

#[derive(Debug)]
pub struct ContentKeySessionEventStream {
    inner: BoundedAsyncStream<ContentKeySessionEvent>,
    _observer: ContentKeySessionObserver,
}

impl ContentKeySessionEventStream {
    #[must_use]
    pub const fn next(&self) -> NextItem<'_, ContentKeySessionEvent> {
        self.inner.next()
    }

    #[must_use]
    pub fn try_next(&self) -> Option<ContentKeySessionEvent> {
        self.inner.try_next()
    }

    #[must_use]
    pub fn buffered_count(&self) -> usize {
        self.inner.buffered_count()
    }

    pub fn clear_buffer(&self) {
        self.inner.clear_buffer();
    }

    #[must_use]
    pub fn is_closed(&self) -> bool {
        self.inner.is_closed()
    }
}

#[derive(Debug)]
#[non_exhaustive]
pub enum ContentKeySessionEvent {
    Requested(ContentKeyRequest),
    Renewing(ContentKeyRequest),
    Persistable(PersistableContentKeyRequest),
    UpdatedPersistableContentKey {
        persistable_content_key: Vec<u8>,
        key_identifier: Option<ContentKeyIdentifier>,
    },
    Failed {
        request: ContentKeyRequest,
        error_message: String,
    },
    RetryRequested {
        request: ContentKeyRequest,
        reason: ContentKeyRequestRetryReason,
    },
    Succeeded(ContentKeyRequest),
    ContentProtectionSessionIdentifierDidChange,
    ExpiredSessionReportGenerated,
    ExternalProtectionStatusDidChange(ContentKey),
    RequestedCollection {
        requests: Vec<ContentKeyRequest>,
        initialization_data: Option<Vec<u8>>,
    },
}

pub type ContentKeyEvent = ContentKeySessionEvent;

struct ContentKeySessionObserverState {
    callback: Box<dyn Fn(ContentKeySessionEvent) -> bool + Send + 'static>,
}

impl UrlAsset {
    pub fn may_require_content_keys_for_media_data_processing(&self) -> bool {
        unsafe {
            ffi::av_url_asset_may_require_content_keys_for_media_data_processing(self.asset.ptr)
        }
    }
}

impl ContentKeySession {
    pub fn new(key_system: &ContentKeySystem) -> Result<Self, AVPlayerError> {
        let key_system = CString::new(key_system.as_raw()).map_err(|error| {
            AVPlayerError::InvalidArgument(format!("key system contains NUL byte: {error}"))
        })?;
        let mut err: *mut c_char = ptr::null_mut();
        let ptr = unsafe { ffi::av_content_key_session_create(key_system.as_ptr(), &mut err) };
        if ptr.is_null() {
            return Err(unsafe { from_swift(ffi::status::OPERATION_FAILED, err) });
        }
        Ok(Self { ptr })
    }

    pub fn with_storage_directory(
        key_system: &ContentKeySystem,
        path: impl AsRef<Path>,
    ) -> Result<Self, AVPlayerError> {
        let key_system = CString::new(key_system.as_raw()).map_err(|error| {
            AVPlayerError::InvalidArgument(format!("key system contains NUL byte: {error}"))
        })?;
        let path = path
            .as_ref()
            .to_str()
            .ok_or_else(|| AVPlayerError::InvalidArgument("path is not valid UTF-8".into()))?;
        let path = to_cstring(path, "content-key storage directory")?;
        let mut err: *mut c_char = ptr::null_mut();
        let ptr = unsafe {
            ffi::av_content_key_session_create_with_storage_directory(
                key_system.as_ptr(),
                path.as_ptr(),
                &mut err,
            )
        };
        if ptr.is_null() {
            return Err(unsafe { from_swift(ffi::status::OPERATION_FAILED, err) });
        }
        Ok(Self { ptr })
    }

    fn info(&self) -> Result<ContentKeySessionInfoPayload, AVPlayerError> {
        let mut err: *mut c_char = ptr::null_mut();
        let json_ptr = unsafe { ffi::av_content_key_session_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)
    }

    pub fn key_system(&self) -> Result<ContentKeySystem, AVPlayerError> {
        Ok(ContentKeySystem::from_raw(&self.info()?.key_system))
    }

    pub fn storage_url(&self) -> Result<Option<String>, AVPlayerError> {
        Ok(self.info()?.storage_url)
    }

    pub fn content_protection_session_identifier_base64(
        &self,
    ) -> Result<Option<String>, AVPlayerError> {
        Ok(self.info()?.content_protection_session_identifier_base64)
    }

    pub fn recipient_count(&self) -> Result<usize, AVPlayerError> {
        Ok(self.info()?.recipient_count)
    }

    pub fn add_content_key_recipient(&self, recipient: &UrlAsset) {
        unsafe {
            ffi::av_content_key_session_add_content_key_recipient(self.ptr, recipient.asset.ptr);
        }
    }

    pub fn remove_content_key_recipient(&self, recipient: &UrlAsset) {
        unsafe {
            ffi::av_content_key_session_remove_content_key_recipient(self.ptr, recipient.asset.ptr);
        }
    }

    pub fn process_content_key_request(
        &self,
        identifier: Option<&ContentKeyIdentifier>,
        initialization_data: Option<&[u8]>,
        options: Option<&ContentKeyRequestOptions>,
    ) -> Result<(), AVPlayerError> {
        if identifier.is_none() && initialization_data.is_none() {
            return Err(AVPlayerError::InvalidArgument(
                "content-key requests require an identifier, initialization data, or both".into(),
            ));
        }

        if let Some(identifier) = identifier {
            match (self.key_system()?, identifier) {
                (
                    ContentKeySystem::ClearKey | ContentKeySystem::AuthorizationToken,
                    ContentKeyIdentifier::String(_),
                ) => {}
                (ContentKeySystem::ClearKey | ContentKeySystem::AuthorizationToken, _) => {
                    return Err(AVPlayerError::InvalidArgument(
                        "clear-key and authorization-token content-key requests require string identifiers"
                            .into(),
                    ));
                }
                _ => {}
            }
        }

        let identifier_json = identifier
            .map(|identifier| json_cstring(&identifier.to_payload(), "content-key identifier"))
            .transpose()?;
        let options_json = maybe_json_cstring(options, "content-key request options")?;
        let initialization_data = initialization_data.unwrap_or_default();
        let mut err: *mut c_char = ptr::null_mut();
        let status = unsafe {
            ffi::av_content_key_session_process_content_key_request(
                self.ptr,
                identifier_json
                    .as_ref()
                    .map_or(ptr::null(), |json| json.as_ptr()),
                if initialization_data.is_empty() {
                    ptr::null()
                } else {
                    initialization_data.as_ptr()
                },
                initialization_data.len(),
                options_json
                    .as_ref()
                    .map_or(ptr::null(), |json| json.as_ptr()),
                &mut err,
            )
        };
        if status != ffi::status::OK {
            return Err(unsafe { from_swift(status, err) });
        }
        Ok(())
    }

    pub fn renew_expiring_response_data_for_content_key_request(
        &self,
        request: &ContentKeyRequest,
    ) -> Result<(), AVPlayerError> {
        if self.key_system()? != ContentKeySystem::FairPlayStreaming {
            return Err(AVPlayerError::InvalidArgument(
                "content-key renewal requires the FairPlay Streaming key system".into(),
            ));
        }
        let mut err: *mut c_char = ptr::null_mut();
        let status = unsafe {
            ffi::av_content_key_session_renew_expiring_response_data_for_request(
                self.ptr,
                request.ptr,
                &mut err,
            )
        };
        if status != ffi::status::OK {
            return Err(unsafe { from_swift(status, err) });
        }
        Ok(())
    }

    pub fn observe<F>(
        &self,
        queue_label: Option<&str>,
        callback: F,
    ) -> Result<ContentKeySessionObserver, AVPlayerError>
    where
        F: Fn(ContentKeySessionEvent) -> bool + Send + 'static,
    {
        let queue_label = to_cstring(
            queue_label.unwrap_or("avplayer.content-key-session"),
            "content-key session queue label",
        )?;
        let state = Box::new(ContentKeySessionObserverState {
            callback: Box::new(callback),
        });
        let userdata = Box::into_raw(state).cast::<c_void>();
        let mut err: *mut c_char = ptr::null_mut();
        let token = unsafe {
            ffi::av_content_key_session_add_observer(
                self.ptr,
                queue_label.as_ptr(),
                Some(content_key_session_event_trampoline),
                userdata,
                Some(content_key_session_observer_drop),
                &mut err,
            )
        };
        if token.is_null() {
            unsafe { content_key_session_observer_drop(userdata) };
            return Err(unsafe { from_swift(ffi::status::OBSERVER_FAILED, err) });
        }
        Ok(ContentKeySessionObserver { token })
    }

    pub fn observe_events(
        &self,
        queue_label: Option<&str>,
        capacity: usize,
    ) -> Result<ContentKeySessionEventStream, AVPlayerError> {
        let (inner, sender) = BoundedAsyncStream::new(capacity);
        let observer = self.observe(queue_label, move |event| {
            sender.push(event);
            false
        })?;
        Ok(ContentKeySessionEventStream {
            inner,
            _observer: observer,
        })
    }

    pub fn event_stream(
        &self,
        capacity: usize,
    ) -> Result<ContentKeySessionEventStream, AVPlayerError> {
        self.observe_events(Some("avplayer.content-key-session"), capacity)
    }

    pub fn expire(&self) {
        unsafe { ffi::av_content_key_session_expire(self.ptr) };
    }
}

impl ContentKeyResponse {
    pub fn fair_play_streaming_key_response_data(
        bytes: impl AsRef<[u8]>,
    ) -> Result<Self, AVPlayerError> {
        let bytes = bytes.as_ref();
        let mut err: *mut c_char = ptr::null_mut();
        let ptr = unsafe {
            ffi::av_content_key_response_create_fair_play_streaming(
                if bytes.is_empty() {
                    ptr::null()
                } else {
                    bytes.as_ptr()
                },
                bytes.len(),
                &mut err,
            )
        };
        if ptr.is_null() {
            return Err(unsafe { from_swift(ffi::status::OPERATION_FAILED, err) });
        }
        Ok(Self { ptr })
    }

    pub fn clear_key_data(
        key_data: impl AsRef<[u8]>,
        initialization_vector: Option<&[u8]>,
    ) -> Result<Self, AVPlayerError> {
        let key_data = key_data.as_ref();
        let initialization_vector = initialization_vector.unwrap_or_default();
        let mut err: *mut c_char = ptr::null_mut();
        let ptr = unsafe {
            ffi::av_content_key_response_create_clear_key(
                if key_data.is_empty() {
                    ptr::null()
                } else {
                    key_data.as_ptr()
                },
                key_data.len(),
                if initialization_vector.is_empty() {
                    ptr::null()
                } else {
                    initialization_vector.as_ptr()
                },
                initialization_vector.len(),
                &mut err,
            )
        };
        if ptr.is_null() {
            return Err(unsafe { from_swift(ffi::status::OPERATION_FAILED, err) });
        }
        Ok(Self { ptr })
    }

    pub fn authorization_token_data(bytes: impl AsRef<[u8]>) -> Result<Self, AVPlayerError> {
        let bytes = bytes.as_ref();
        let mut err: *mut c_char = ptr::null_mut();
        let ptr = unsafe {
            ffi::av_content_key_response_create_authorization_token(
                if bytes.is_empty() {
                    ptr::null()
                } else {
                    bytes.as_ptr()
                },
                bytes.len(),
                &mut err,
            )
        };
        if ptr.is_null() {
            return Err(unsafe { from_swift(ffi::status::OPERATION_FAILED, err) });
        }
        Ok(Self { ptr })
    }
}

impl ContentKeySpecifier {
    pub fn new(
        key_system: &ContentKeySystem,
        identifier: &ContentKeyIdentifier,
        options: Option<&ContentKeyRequestOptions>,
    ) -> Result<Self, AVPlayerError> {
        let key_system = to_cstring(key_system.as_raw(), "content-key system")?;
        let identifier = json_cstring(&identifier.to_payload(), "content-key identifier")?;
        let options = maybe_json_cstring(options, "content-key specifier options")?;
        let mut err: *mut c_char = ptr::null_mut();
        let ptr = unsafe {
            ffi::av_content_key_specifier_create(
                key_system.as_ptr(),
                identifier.as_ptr(),
                options.as_ref().map_or(ptr::null(), |value| value.as_ptr()),
                &mut err,
            )
        };
        if ptr.is_null() {
            return Err(unsafe { from_swift(ffi::status::OPERATION_FAILED, err) });
        }
        Ok(Self { ptr })
    }

    fn info(&self) -> Result<ContentKeySpecifierInfoPayload, AVPlayerError> {
        let mut err: *mut c_char = ptr::null_mut();
        let json_ptr = unsafe { ffi::av_content_key_specifier_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)
    }

    pub fn key_system(&self) -> Result<ContentKeySystem, AVPlayerError> {
        Ok(ContentKeySystem::from_raw(&self.info()?.key_system))
    }

    pub fn identifier(&self) -> Result<Option<ContentKeyIdentifier>, AVPlayerError> {
        Ok(self
            .info()?
            .identifier
            .map(ContentKeyIdentifier::from_payload))
    }

    pub fn options(&self) -> Result<ContentKeyRequestOptions, AVPlayerError> {
        Ok(self.info()?.options.unwrap_or_default())
    }
}

impl ContentKey {
    fn info(&self) -> Result<ContentKeyInfoPayload, AVPlayerError> {
        let mut err: *mut c_char = ptr::null_mut();
        let json_ptr = unsafe { ffi::av_content_key_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)
    }

    pub fn content_key_specifier(&self) -> Option<ContentKeySpecifier> {
        let ptr = unsafe { ffi::av_content_key_copy_content_key_specifier(self.ptr) };
        if ptr.is_null() {
            None
        } else {
            Some(ContentKeySpecifier { ptr })
        }
    }

    pub fn external_content_protection_status(
        &self,
    ) -> Result<Option<ExternalContentProtectionStatus>, AVPlayerError> {
        Ok(self
            .info()?
            .external_content_protection_status
            .map(ExternalContentProtectionStatus::from_raw))
    }

    pub fn revoke(&self) -> Result<(), AVPlayerError> {
        let mut err: *mut c_char = ptr::null_mut();
        let status = unsafe { ffi::av_content_key_revoke(self.ptr, &mut err) };
        if status != ffi::status::OK {
            return Err(unsafe { from_swift(status, err) });
        }
        Ok(())
    }
}

impl ContentKeyRequest {
    fn info(&self) -> Result<ContentKeyRequestInfoPayload, AVPlayerError> {
        request_info(self.ptr)
    }

    pub fn status(&self) -> Result<ContentKeyRequestStatus, AVPlayerError> {
        Ok(ContentKeyRequestStatus::from_raw(self.info()?.status))
    }

    pub fn error_message(&self) -> Result<Option<String>, AVPlayerError> {
        Ok(self.info()?.error_message)
    }

    pub fn identifier(&self) -> Result<Option<ContentKeyIdentifier>, AVPlayerError> {
        Ok(self
            .info()?
            .identifier
            .map(ContentKeyIdentifier::from_payload))
    }

    pub fn initialization_data(&self) -> Result<Option<Vec<u8>>, AVPlayerError> {
        Ok(self.info()?.initialization_data)
    }

    pub fn options(&self) -> Result<ContentKeyRequestOptions, AVPlayerError> {
        Ok(self.info()?.options.unwrap_or_default())
    }

    pub fn can_provide_persistable_content_key(&self) -> Result<bool, AVPlayerError> {
        Ok(self.info()?.can_provide_persistable_content_key)
    }

    pub fn renews_expiring_response_data(&self) -> Result<bool, AVPlayerError> {
        Ok(self.info()?.renews_expiring_response_data)
    }

    pub fn content_key_specifier(&self) -> Result<Option<ContentKeySpecifier>, AVPlayerError> {
        Ok(if self.info()?.has_content_key_specifier {
            copy_content_key_request_specifier(self.ptr)
        } else {
            None
        })
    }

    pub fn content_key(&self) -> Result<Option<ContentKey>, AVPlayerError> {
        Ok(if self.info()?.has_content_key {
            copy_content_key_request_content_key(self.ptr)
        } else {
            None
        })
    }

    pub fn make_streaming_content_key_request_data(
        &self,
        app_identifier: &[u8],
        content_identifier: Option<&[u8]>,
        options: Option<&ContentKeyRequestOptions>,
    ) -> Result<Option<Vec<u8>>, AVPlayerError> {
        request_make_streaming_content_key_request_data(
            self.ptr,
            app_identifier,
            content_identifier,
            options,
        )
    }

    pub fn process_response(&self, response: &ContentKeyResponse) -> Result<(), AVPlayerError> {
        request_process_response(self.ptr, response)
    }

    pub fn process_content_key_response(
        &self,
        fair_play_streaming_key_response_data: impl AsRef<[u8]>,
    ) -> Result<(), AVPlayerError> {
        let response = ContentKeyResponse::fair_play_streaming_key_response_data(
            fair_play_streaming_key_response_data,
        )?;
        self.process_response(&response)
    }

    pub fn process_clear_key_response(
        &self,
        key_data: impl AsRef<[u8]>,
        initialization_vector: Option<&[u8]>,
    ) -> Result<(), AVPlayerError> {
        let response = ContentKeyResponse::clear_key_data(key_data, initialization_vector)?;
        self.process_response(&response)
    }

    pub fn process_authorization_token_response(
        &self,
        authorization_token_data: impl AsRef<[u8]>,
    ) -> Result<(), AVPlayerError> {
        let response = ContentKeyResponse::authorization_token_data(authorization_token_data)?;
        self.process_response(&response)
    }

    pub fn process_content_key_response_error(
        &self,
        message: impl AsRef<str>,
    ) -> Result<(), AVPlayerError> {
        request_process_response_error(self.ptr, message.as_ref())
    }

    pub fn respond_by_requesting_persistable_content_key_request(
        &self,
    ) -> Result<(), AVPlayerError> {
        request_request_persistable_content_key(self.ptr)
    }
}

impl PersistableContentKeyRequest {
    fn info(&self) -> Result<ContentKeyRequestInfoPayload, AVPlayerError> {
        request_info(self.ptr)
    }

    pub fn status(&self) -> Result<ContentKeyRequestStatus, AVPlayerError> {
        Ok(ContentKeyRequestStatus::from_raw(self.info()?.status))
    }

    pub fn error_message(&self) -> Result<Option<String>, AVPlayerError> {
        Ok(self.info()?.error_message)
    }

    pub fn identifier(&self) -> Result<Option<ContentKeyIdentifier>, AVPlayerError> {
        Ok(self
            .info()?
            .identifier
            .map(ContentKeyIdentifier::from_payload))
    }

    pub fn initialization_data(&self) -> Result<Option<Vec<u8>>, AVPlayerError> {
        Ok(self.info()?.initialization_data)
    }

    pub fn options(&self) -> Result<ContentKeyRequestOptions, AVPlayerError> {
        Ok(self.info()?.options.unwrap_or_default())
    }

    pub fn can_provide_persistable_content_key(&self) -> Result<bool, AVPlayerError> {
        Ok(self.info()?.can_provide_persistable_content_key)
    }

    pub fn renews_expiring_response_data(&self) -> Result<bool, AVPlayerError> {
        Ok(self.info()?.renews_expiring_response_data)
    }

    pub fn content_key_specifier(&self) -> Result<Option<ContentKeySpecifier>, AVPlayerError> {
        Ok(if self.info()?.has_content_key_specifier {
            copy_content_key_request_specifier(self.ptr)
        } else {
            None
        })
    }

    pub fn content_key(&self) -> Result<Option<ContentKey>, AVPlayerError> {
        Ok(if self.info()?.has_content_key {
            copy_content_key_request_content_key(self.ptr)
        } else {
            None
        })
    }

    pub fn make_streaming_content_key_request_data(
        &self,
        app_identifier: &[u8],
        content_identifier: Option<&[u8]>,
        options: Option<&ContentKeyRequestOptions>,
    ) -> Result<Option<Vec<u8>>, AVPlayerError> {
        request_make_streaming_content_key_request_data(
            self.ptr,
            app_identifier,
            content_identifier,
            options,
        )
    }

    pub fn process_response(&self, response: &ContentKeyResponse) -> Result<(), AVPlayerError> {
        request_process_response(self.ptr, response)
    }

    pub fn process_content_key_response(
        &self,
        fair_play_streaming_key_response_data: impl AsRef<[u8]>,
    ) -> Result<(), AVPlayerError> {
        let response = ContentKeyResponse::fair_play_streaming_key_response_data(
            fair_play_streaming_key_response_data,
        )?;
        self.process_response(&response)
    }

    pub fn process_clear_key_response(
        &self,
        key_data: impl AsRef<[u8]>,
        initialization_vector: Option<&[u8]>,
    ) -> Result<(), AVPlayerError> {
        let response = ContentKeyResponse::clear_key_data(key_data, initialization_vector)?;
        self.process_response(&response)
    }

    pub fn process_authorization_token_response(
        &self,
        authorization_token_data: impl AsRef<[u8]>,
    ) -> Result<(), AVPlayerError> {
        let response = ContentKeyResponse::authorization_token_data(authorization_token_data)?;
        self.process_response(&response)
    }

    pub fn process_content_key_response_error(
        &self,
        message: impl AsRef<str>,
    ) -> Result<(), AVPlayerError> {
        request_process_response_error(self.ptr, message.as_ref())
    }

    pub fn persistable_content_key_from_key_vendor_response(
        &self,
        key_vendor_response: &[u8],
        options: Option<&ContentKeyRequestOptions>,
    ) -> Result<Vec<u8>, AVPlayerError> {
        let options = maybe_json_cstring(options, "persistable content-key options")?;
        let mut err: *mut c_char = ptr::null_mut();
        let json_ptr = unsafe {
            ffi::av_persistable_content_key_request_persistable_content_key_json(
                self.ptr,
                if key_vendor_response.is_empty() {
                    ptr::null()
                } else {
                    key_vendor_response.as_ptr()
                },
                key_vendor_response.len(),
                options.as_ref().map_or(ptr::null(), |value| value.as_ptr()),
                &mut err,
            )
        };
        if json_ptr.is_null() {
            return Err(unsafe { from_swift(ffi::status::OPERATION_FAILED, err) });
        }
        Ok(parse_json_and_free::<ContentKeyBytesPayload>(json_ptr)?
            .bytes
            .unwrap_or_default())
    }
}

fn request_info(ptr: *mut c_void) -> Result<ContentKeyRequestInfoPayload, AVPlayerError> {
    let mut err: *mut c_char = ptr::null_mut();
    let json_ptr = unsafe { ffi::av_content_key_request_info_json(ptr, &mut err) };
    if json_ptr.is_null() {
        return Err(unsafe { from_swift(ffi::status::OPERATION_FAILED, err) });
    }
    parse_json_and_free(json_ptr)
}

fn copy_content_key_request_specifier(ptr: *mut c_void) -> Option<ContentKeySpecifier> {
    let ptr = unsafe { ffi::av_content_key_request_copy_content_key_specifier(ptr) };
    if ptr.is_null() {
        None
    } else {
        Some(ContentKeySpecifier { ptr })
    }
}

fn copy_content_key_request_content_key(ptr: *mut c_void) -> Option<ContentKey> {
    let ptr = unsafe { ffi::av_content_key_request_copy_content_key(ptr) };
    if ptr.is_null() {
        None
    } else {
        Some(ContentKey { ptr })
    }
}

fn request_make_streaming_content_key_request_data(
    ptr: *mut c_void,
    app_identifier: &[u8],
    content_identifier: Option<&[u8]>,
    options: Option<&ContentKeyRequestOptions>,
) -> Result<Option<Vec<u8>>, AVPlayerError> {
    let options = maybe_json_cstring(options, "streaming content-key request options")?;
    let content_identifier = content_identifier.unwrap_or_default();
    let mut err: *mut c_char = ptr::null_mut();
    let json_ptr = unsafe {
        ffi::av_content_key_request_make_streaming_content_key_request_data_json(
            ptr,
            if app_identifier.is_empty() {
                ptr::null()
            } else {
                app_identifier.as_ptr()
            },
            app_identifier.len(),
            if content_identifier.is_empty() {
                ptr::null()
            } else {
                content_identifier.as_ptr()
            },
            content_identifier.len(),
            options.as_ref().map_or(ptr::null(), |value| value.as_ptr()),
            &mut err,
        )
    };
    if json_ptr.is_null() {
        return Err(unsafe { from_swift(ffi::status::OPERATION_FAILED, err) });
    }
    Ok(parse_json_and_free::<ContentKeyBytesPayload>(json_ptr)?.bytes)
}

fn request_process_response(
    request_ptr: *mut c_void,
    response: &ContentKeyResponse,
) -> Result<(), AVPlayerError> {
    let mut err: *mut c_char = ptr::null_mut();
    let status = unsafe {
        ffi::av_content_key_request_process_content_key_response(
            request_ptr,
            response.ptr,
            &mut err,
        )
    };
    if status != ffi::status::OK {
        return Err(unsafe { from_swift(status, err) });
    }
    Ok(())
}

fn request_process_response_error(
    request_ptr: *mut c_void,
    message: &str,
) -> Result<(), AVPlayerError> {
    let message = CString::new(message).map_err(|error| {
        AVPlayerError::InvalidArgument(format!(
            "content-key response error message contains NUL byte: {error}"
        ))
    })?;
    let mut err: *mut c_char = ptr::null_mut();
    let status = unsafe {
        ffi::av_content_key_request_process_content_key_response_error(
            request_ptr,
            message.as_ptr(),
            &mut err,
        )
    };
    if status != ffi::status::OK {
        return Err(unsafe { from_swift(status, err) });
    }
    Ok(())
}

fn request_request_persistable_content_key(request_ptr: *mut c_void) -> Result<(), AVPlayerError> {
    let mut err: *mut c_char = ptr::null_mut();
    let status = unsafe {
        ffi::av_content_key_request_request_persistable_content_key(request_ptr, &mut err)
    };
    if status != ffi::status::OK {
        return Err(unsafe { from_swift(status, err) });
    }
    Ok(())
}

fn catch_cb_panic_bool<F: FnOnce() -> bool>(site: &str, f: F) -> bool {
    match std::panic::catch_unwind(AssertUnwindSafe(f)) {
        Ok(value) => value,
        Err(payload) => {
            let msg = payload.downcast_ref::<&str>().copied().unwrap_or_else(|| {
                payload
                    .downcast_ref::<String>()
                    .map_or("<non-string panic>", String::as_str)
            });
            eprintln!("avplayer: panic in {site} caught at C ABI boundary: {msg}");
            false
        }
    }
}

fn content_key_session_event_from_payload(
    payload: ContentKeySessionEventPayload,
) -> Option<ContentKeySessionEvent> {
    match payload.event.as_str() {
        "requested" => Some(ContentKeySessionEvent::Requested(ContentKeyRequest {
            ptr: ptr_from_u64(payload.request_ptr?),
        })),
        "renewing" => Some(ContentKeySessionEvent::Renewing(ContentKeyRequest {
            ptr: ptr_from_u64(payload.request_ptr?),
        })),
        "persistable" => Some(ContentKeySessionEvent::Persistable(
            PersistableContentKeyRequest {
                ptr: ptr_from_u64(payload.request_ptr?),
            },
        )),
        "updated_persistable_content_key" => {
            Some(ContentKeySessionEvent::UpdatedPersistableContentKey {
                persistable_content_key: payload.persistable_content_key.unwrap_or_default(),
                key_identifier: payload
                    .key_identifier
                    .map(ContentKeyIdentifier::from_payload),
            })
        }
        "failed" => Some(ContentKeySessionEvent::Failed {
            request: ContentKeyRequest {
                ptr: ptr_from_u64(payload.request_ptr?),
            },
            error_message: payload.error_message.unwrap_or_default(),
        }),
        "retry_requested" => Some(ContentKeySessionEvent::RetryRequested {
            request: ContentKeyRequest {
                ptr: ptr_from_u64(payload.request_ptr?),
            },
            reason: ContentKeyRequestRetryReason::from_raw(
                payload.retry_reason.as_deref().unwrap_or_default(),
            ),
        }),
        "succeeded" => Some(ContentKeySessionEvent::Succeeded(ContentKeyRequest {
            ptr: ptr_from_u64(payload.request_ptr?),
        })),
        "content_protection_session_identifier_did_change" => {
            Some(ContentKeySessionEvent::ContentProtectionSessionIdentifierDidChange)
        }
        "expired_session_report_generated" => {
            Some(ContentKeySessionEvent::ExpiredSessionReportGenerated)
        }
        "external_protection_status_did_change" => Some(
            ContentKeySessionEvent::ExternalProtectionStatusDidChange(ContentKey {
                ptr: ptr_from_u64(payload.content_key_ptr?),
            }),
        ),
        "requested_collection" => {
            let mut requests = payload
                .key_request_ptrs?
                .into_iter()
                .map(|raw| ContentKeyRequest {
                    ptr: ptr_from_u64(raw),
                })
                .collect::<Vec<_>>();
            if requests.len() == 1 && payload.initialization_data.is_none() {
                return requests.pop().map(ContentKeySessionEvent::Requested);
            }
            Some(ContentKeySessionEvent::RequestedCollection {
                requests,
                initialization_data: payload.initialization_data,
            })
        }
        _ => None,
    }
}

fn ptr_from_u64(raw: u64) -> *mut c_void {
    usize::try_from(raw).expect("content-key event pointer fits in usize") as *mut c_void
}

unsafe extern "C" fn content_key_session_event_trampoline(
    userdata: *mut c_void,
    payload_json: *const c_char,
) -> bool {
    if userdata.is_null() || payload_json.is_null() {
        return false;
    }

    let callback = unsafe { &*userdata.cast::<ContentKeySessionObserverState>() };
    let Ok(payload) = unsafe { CStr::from_ptr(payload_json) }.to_str() else {
        return false;
    };
    let Ok(payload) = serde_json::from_str::<ContentKeySessionEventPayload>(payload) else {
        return false;
    };
    let Some(event) = content_key_session_event_from_payload(payload) else {
        return false;
    };

    catch_cb_panic_bool("content_key_session_event_trampoline", || {
        (callback.callback)(event)
    })
}

unsafe extern "C" fn content_key_session_observer_drop(userdata: *mut c_void) {
    if !userdata.is_null() {
        drop(unsafe { Box::from_raw(userdata.cast::<ContentKeySessionObserverState>()) });
    }
}

// SAFETY: AVFoundation content-key handles and observer tokens may be sent
// across thread boundaries; the underlying objects manage their own internal
// synchronization and lifetime once retained.
unsafe impl Send for ContentKeySession {}
unsafe impl Send for ContentKeyRequest {}
unsafe impl Send for PersistableContentKeyRequest {}
unsafe impl Send for ContentKeyResponse {}
unsafe impl Send for ContentKeySpecifier {}
unsafe impl Send for ContentKey {}
unsafe impl Send for ContentKeySessionObserver {}