|
| 1 | +use std::ffi::{c_char, c_void}; |
| 2 | +use std::pin::Pin; |
| 3 | + |
| 4 | +use crate::ffi::cdm; |
| 5 | + |
| 6 | +/// Trait abstracting over different `Host_NN` interface versions. Note that we |
| 7 | +/// only define the methods OpenWV actually uses, for brevity and better |
| 8 | +/// compatibility across versions. |
| 9 | +#[allow(non_snake_case)] |
| 10 | +pub trait CommonHost { |
| 11 | + fn Allocate(self: Pin<&mut Self>, capacity: u32) -> *mut cdm::Buffer; |
| 12 | + fn OnInitialized(self: Pin<&mut Self>, success: bool); |
| 13 | + fn OnResolveKeyStatusPromise(self: Pin<&mut Self>, promise_id: u32, key_status: cdm::KeyStatus); |
| 14 | + unsafe fn OnResolveNewSessionPromise( |
| 15 | + self: Pin<&mut Self>, |
| 16 | + promise_id: u32, |
| 17 | + session_id: *const c_char, |
| 18 | + session_id_size: u32, |
| 19 | + ); |
| 20 | + fn OnResolvePromise(self: Pin<&mut Self>, promise_id: u32); |
| 21 | + unsafe fn OnRejectPromise( |
| 22 | + self: Pin<&mut Self>, |
| 23 | + promise_id: u32, |
| 24 | + exception: cdm::Exception, |
| 25 | + system_code: u32, |
| 26 | + error_message: *const c_char, |
| 27 | + error_message_size: u32, |
| 28 | + ); |
| 29 | + unsafe fn OnSessionMessage( |
| 30 | + self: Pin<&mut Self>, |
| 31 | + session_id: *const c_char, |
| 32 | + session_id_size: u32, |
| 33 | + message_type: cdm::MessageType, |
| 34 | + message: *const c_char, |
| 35 | + message_size: u32, |
| 36 | + ); |
| 37 | + unsafe fn OnSessionKeysChange( |
| 38 | + self: Pin<&mut Self>, |
| 39 | + session_id: *const c_char, |
| 40 | + session_id_size: u32, |
| 41 | + has_additional_usable_key: bool, |
| 42 | + keys_info: *const cdm::KeyInformation, |
| 43 | + keys_info_count: u32, |
| 44 | + ); |
| 45 | + unsafe fn OnSessionClosed( |
| 46 | + self: Pin<&mut Self>, |
| 47 | + session_id: *const c_char, |
| 48 | + session_id_size: u32, |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +pub unsafe fn downcast_host<T: CommonHost + 'static>( |
| 53 | + ptr: *mut c_void, |
| 54 | +) -> Option<&'static mut dyn CommonHost> { |
| 55 | + let typed_ptr: *mut T = ptr.cast(); |
| 56 | + let concrete_ref = unsafe { typed_ptr.as_mut() }; |
| 57 | + concrete_ref.map(|x| x as &mut dyn CommonHost) |
| 58 | +} |
| 59 | + |
| 60 | +macro_rules! impl_common_host { |
| 61 | + ($target:path) => { |
| 62 | + impl CommonHost for $target { |
| 63 | + fn Allocate(self: Pin<&mut Self>, capacity: u32) -> *mut cdm::Buffer { |
| 64 | + self.Allocate(capacity) |
| 65 | + } |
| 66 | + |
| 67 | + fn OnInitialized(self: Pin<&mut Self>, success: bool) { |
| 68 | + self.OnInitialized(success) |
| 69 | + } |
| 70 | + |
| 71 | + fn OnResolveKeyStatusPromise( |
| 72 | + self: Pin<&mut Self>, |
| 73 | + promise_id: u32, |
| 74 | + key_status: cdm::KeyStatus, |
| 75 | + ) { |
| 76 | + self.OnResolveKeyStatusPromise(promise_id, key_status) |
| 77 | + } |
| 78 | + |
| 79 | + unsafe fn OnResolveNewSessionPromise( |
| 80 | + self: Pin<&mut Self>, |
| 81 | + promise_id: u32, |
| 82 | + session_id: *const c_char, |
| 83 | + session_id_size: u32, |
| 84 | + ) { |
| 85 | + unsafe { self.OnResolveNewSessionPromise(promise_id, session_id, session_id_size) } |
| 86 | + } |
| 87 | + |
| 88 | + fn OnResolvePromise(self: Pin<&mut Self>, promise_id: u32) { |
| 89 | + self.OnResolvePromise(promise_id) |
| 90 | + } |
| 91 | + |
| 92 | + unsafe fn OnRejectPromise( |
| 93 | + self: Pin<&mut Self>, |
| 94 | + promise_id: u32, |
| 95 | + exception: cdm::Exception, |
| 96 | + system_code: u32, |
| 97 | + error_message: *const c_char, |
| 98 | + error_message_size: u32, |
| 99 | + ) { |
| 100 | + unsafe { |
| 101 | + self.OnRejectPromise( |
| 102 | + promise_id, |
| 103 | + exception, |
| 104 | + system_code, |
| 105 | + error_message, |
| 106 | + error_message_size, |
| 107 | + ) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + unsafe fn OnSessionMessage( |
| 112 | + self: Pin<&mut Self>, |
| 113 | + session_id: *const c_char, |
| 114 | + session_id_size: u32, |
| 115 | + message_type: cdm::MessageType, |
| 116 | + message: *const c_char, |
| 117 | + message_size: u32, |
| 118 | + ) { |
| 119 | + unsafe { |
| 120 | + self.OnSessionMessage( |
| 121 | + session_id, |
| 122 | + session_id_size, |
| 123 | + message_type, |
| 124 | + message, |
| 125 | + message_size, |
| 126 | + ) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + unsafe fn OnSessionKeysChange( |
| 131 | + self: Pin<&mut Self>, |
| 132 | + session_id: *const c_char, |
| 133 | + session_id_size: u32, |
| 134 | + has_additional_usable_key: bool, |
| 135 | + keys_info: *const cdm::KeyInformation, |
| 136 | + keys_info_count: u32, |
| 137 | + ) { |
| 138 | + unsafe { |
| 139 | + self.OnSessionKeysChange( |
| 140 | + session_id, |
| 141 | + session_id_size, |
| 142 | + has_additional_usable_key, |
| 143 | + keys_info, |
| 144 | + keys_info_count, |
| 145 | + ) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + unsafe fn OnSessionClosed( |
| 150 | + self: Pin<&mut Self>, |
| 151 | + session_id: *const c_char, |
| 152 | + session_id_size: u32, |
| 153 | + ) { |
| 154 | + unsafe { self.OnSessionClosed(session_id, session_id_size) } |
| 155 | + } |
| 156 | + } |
| 157 | + }; |
| 158 | +} |
| 159 | + |
| 160 | +impl_common_host!(cdm::Host_10); |
| 161 | +impl_common_host!(cdm::Host_11); |
0 commit comments