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
//! Core types and traits to instantiate and interact with CLAP plugins.
use crate::prelude::*;
use clap_sys::plugin::clap_plugin;
use std::ffi::CStr;
use std::marker::PhantomData;
use std::mem::ManuallyDrop;
use std::sync::Arc;
mod error;
mod handle;
pub(crate) mod instance;
pub use error::PluginInstanceError;
pub use handle::*;
use instance::*;
pub use clack_common::plugin::*;
/// A plugin instance.
///
/// This type is generic over `H`, the type of the host callback handlers, which the plugin can
/// call use directly when its own methods are called by the host.
pub struct PluginInstance<H: HostHandlers> {
pub(crate) inner: ManuallyDrop<Arc<PluginInstanceInner<H>>>,
_no_send: PhantomData<*const ()>,
}
impl<H: HostHandlers> PluginInstance<H> {
/// Creates a new plugin instance from a given [`PluginEntry`].
///
/// The specific plugin to be instantiated is identified by the given `plugin_id`.
///
/// This method takes two closures, here named `FS` and `FH`, which will respectively create
/// the [`Shared`](HostHandlers::Shared) and [`MainThread`](HostHandlers::MainThread) callback
/// handler instances associated with this plugin instance.
///
/// The [`MainThread`](HostHandlers::MainThread)-producing callback takes a long-lived reference
/// to its [`Shared`](HostHandlers::Shared) counterpart, so that it can be accessed at any time
/// from the main thread if needed.
///
/// The [`Shared`](HostHandlers::Shared)-producing callback takes a unit reference `&()`. It is
/// not used for anything, but is necessary for proper lifetime binding.
///
/// This method also takes a reference to a [`HostInfo`], giving the plugin some metadata about
/// the host itself.
///
/// # Errors
///
/// If for some reason the given [`PluginEntry`] does not expose a [`PluginFactory`](crate::factory::plugin::PluginFactory),
/// then this will return [`PluginInstanceError::MissingPluginFactory`].
///
/// If the `PluginFactory`'s `create_plugin` function pointer is NULL,
/// this will return [`PluginInstanceError::NullFactoryCreatePluginFunction`].
///
/// If the given `plugin_id` does not exist, or if more generally the
/// `PluginFactory`'s `create_plugin` function returned NULL,
/// then this will return [`PluginInstanceError::PluginNotFound`].
///
/// Otherwise, if the plugin instantiation implementation failed, it will return
/// [`PluginInstanceError::InstantiationFailed`].
///
/// # Example
///
/// ```
/// use clack_host::prelude::*;
///
/// struct MyHost;
///
/// struct MyHostMainThread { /* ... */ }
///
/// impl HostHandlers for MyHost {
/// type Shared<'a> = /* ... */
/// # ();
/// type AudioProcessor<'a> = /* ... */
/// # ();
/// type MainThread<'a> = MyHostMainThread;
/// }
///
/// impl<'a> MainThreadHandler<'a> for MyHostMainThread {
/// fn initialized(&self, instance: InitializedPluginHandle<'a>) {
/// // Called whn the plugin has been fully initialized.
/// }
/// }
///
/// # mod diva { include!("./entry/diva_stub.rs"); }
///
/// let entry = /* ... */
/// # PluginEntry::load_from_clack::<diva::Entry>(c"").unwrap();
/// let host_info = HostInfo::new("Foo DAW", "Foo Inc.", "https://example.com", "0.0.1").unwrap();
///
/// let instance = PluginInstance::<MyHost>::new(
/// |_|(),
/// |_| MyHostMainThread {},
/// &entry,
/// c"com.audio.the-plugin",
/// &host_info
/// );
///
/// ```
pub fn new<FS, FH>(
shared: FS,
main_thread: FH,
entry: &PluginEntry,
plugin_id: &CStr,
host: &HostInfo,
) -> Result<Self, PluginInstanceError>
where
FS: for<'b> FnOnce(&'b ()) -> <H as HostHandlers>::Shared<'b>,
FH: for<'b> FnOnce(
&'b <H as HostHandlers>::Shared<'b>,
) -> <H as HostHandlers>::MainThread<'b>,
{
let inner = PluginInstanceInner::<H>::instantiate(
shared,
main_thread,
entry,
plugin_id,
host.clone(),
)?;
Ok(Self {
inner: ManuallyDrop::new(inner),
_no_send: PhantomData,
})
}
/// Activates the plugin instance, preparing it to process audio and events according to the given [`PluginAudioConfiguration`].
///
/// This method takes a closure `FA`, which will create
/// the [`AudioProcessor`](HostHandlers::AudioProcessor) callback
/// handler instance associated with this plugin instance.
///
/// This closure takes a long-lived reference to the [`Shared`](HostHandlers::Shared) handler
/// so that it can be accessed at any time from the audio thread / audio-processing context if needed.
///
/// This closure also provides a temporary, exclusive reference to the [`MainThread`](HostHandlers::MainThread) handler.
///
/// # Errors
///
/// This will return [`PluginInstanceError::AlreadyActivatedPlugin`] if the plugin has already
/// been activated.
///
/// If the plugin instance's `activate` function pointer is NULL,
/// this will return [`PluginInstanceError::NullActivateFunction`].
///
/// Otherwise, if the plugin instance's `activate` function implementation failed for any reason,
/// this will return [`PluginInstanceError::ActivationFailed`].
///
/// # Panics
///
/// This function will panic if either `min_frames_count` or `max_frames_count` is 0, or if
/// `min_frames_count` is greater than `max_frames_count`.
pub fn activate<FA>(
&mut self,
audio_processor: FA,
configuration: PluginAudioConfiguration,
) -> Result<StoppedPluginAudioProcessor<H>, PluginInstanceError>
where
FA: for<'a> FnOnce(
&'a <H as HostHandlers>::Shared<'a>,
&<H as HostHandlers>::MainThread<'a>,
) -> <H as HostHandlers>::AudioProcessor<'a>,
{
configuration.validate();
let wrapper =
Arc::get_mut(&mut self.inner).ok_or(PluginInstanceError::AlreadyActivatedPlugin)?;
wrapper.activate(audio_processor, configuration)?;
Ok(StoppedPluginAudioProcessor::new(Arc::clone(&self.inner)))
}
/// De-activates the plugin instance, freeing its processing-related resources, and allows it
/// to be re-[activated](Self::activate) with a different audio configuration.
///
/// This consumes the [`StoppedPluginAudioProcessor`] handle created by [`activate`](Self::activate),
/// in order to ensure it cannot be used anymore.
///
/// For a version of this method that does not require the [`StoppedPluginAudioProcessor`] handle,
/// see [`try_deactivate`](Self::try_deactivate).
///
/// # Panics
///
/// This will panic if the provided `processor` handle did not come from this plugin instance.
#[inline]
pub fn deactivate(&mut self, processor: StoppedPluginAudioProcessor<H>) {
self.deactivate_with(processor, |_, _| ())
}
/// Attempts to de-activate a plugin instance, which [`PluginAudioProcessor`] handle has been
/// dropped.
///
/// This is an alternative to [`deactivate`](Self::deactivate) which does not require the associated
/// [`PluginAudioProcessorHandle`] to be passed. However, this method is fallible, and will
/// return an error if the [`PluginAudioProcessorHandle`] has not been dropped.
///
/// This may be useful if it is more practical for you to drop the [`PluginAudioProcessorHandle`]
/// (e.g. in another thread) compared to sending it back to the main thread to use with
/// [`deactivate`](Self::deactivate).
///
/// However, if you can easily access or retrieve [`PluginAudioProcessorHandle`] back to the main
/// thread, you should use [`deactivate`](Self::deactivate) instead.
///
/// # Errors
///
/// This method will return [`PluginInstanceError::StillActivatedPlugin`] if the associated
/// [`PluginAudioProcessorHandle`] has not been dropped.
#[inline]
pub fn try_deactivate(&mut self) -> Result<(), PluginInstanceError> {
self.try_deactivate_with(|_, _| ())
}
/// De-activates the plugin instance, freeing its processing-related resources, and allows it
/// to be re-[activated](Self::activate) with a different audio configuration.
///
/// This consumes the [`StoppedPluginAudioProcessor`] handle created by [`activate`](Self::activate),
/// in order to ensure it cannot be used anymore.
///
/// This is equivalent to [`Self::deactivate`], except this method also takes a closure type `D`,
/// which allows to customize the destruction process of the [`AudioProcessor`](HostHandlers::AudioProcessor) callback
/// handler instance that was created during [`activate`](Self::activate).
///
/// This may be useful to e.g. reuse or reprocess components or allocations the [`AudioProcessor`](HostHandlers::AudioProcessor) callback
/// handler instance owned, either by giving it to the [`MainThread`](HostHandlers::MainThread) handler
/// (which is also provided by a temporary, exclusive reference), or by returning it.
///
/// This function will return whatever the destructor closure returned.
///
/// # Panics
///
/// This will panic if the provided `processor` handle did not come from this plugin instance.
pub fn deactivate_with<T, D>(
&mut self,
processor: StoppedPluginAudioProcessor<H>,
drop_with: D,
) -> T
where
D: for<'s> FnOnce(
<H as HostHandlers>::AudioProcessor<'s>,
&<H as HostHandlers>::MainThread<'s>,
) -> T,
{
if !Arc::ptr_eq(&self.inner, &processor.inner) {
panic!("Given plugin audio processor does not match the instance being deactivated")
}
drop(processor);
// PANIC: we dropped the only processor produced, and checked if it matched
self.try_deactivate_with(drop_with).unwrap()
}
/// Attempts to de-activate a plugin instance, which [`PluginAudioProcessor`] handle has been
/// dropped.
///
/// This is equivalent to [`Self::try_deactivate`], except this method also takes a closure type `D`,
/// which allows to customize the destruction process of the [`AudioProcessor`](HostHandlers::AudioProcessor) callback
/// handler instance that was created during [`activate`](Self::activate).
///
/// This is an alternative to [`deactivate_with`](Self::deactivate_with) which does not require the associated
/// [`PluginAudioProcessorHandle`] to be passed. However, this method is fallible, and will
/// return an error if the [`PluginAudioProcessorHandle`] has not been dropped.
///
/// This may be useful if it is more practical for you to drop the [`PluginAudioProcessorHandle`]
/// (e.g. in another thread) compared to sending it back to the main thread to use with
/// [`deactivate_with`](Self::deactivate_with).
///
/// However, if you can easily access or retrieve [`PluginAudioProcessorHandle`] back to the main
/// thread, you should use [`deactivate_with`](Self::deactivate_with) instead.
///
/// # Errors
///
/// This method will return [`PluginInstanceError::StillActivatedPlugin`] if the associated
/// [`PluginAudioProcessorHandle`] has not been dropped.
pub fn try_deactivate_with<T, D>(&mut self, drop_with: D) -> Result<T, PluginInstanceError>
where
D: for<'s> FnOnce(
<H as HostHandlers>::AudioProcessor<'s>,
&<H as HostHandlers>::MainThread<'s>,
) -> T,
{
let wrapper =
Arc::get_mut(&mut self.inner).ok_or(PluginInstanceError::StillActivatedPlugin)?;
wrapper.deactivate_with(drop_with)
}
/// Calls the plugin's `on_main_thread` callback.
///
/// This is usually done in response to the plugin calling [`SharedHandler::request_callback`]
/// earlier from a different thread, allowing it to "wake up" its main thread after e.g. another
/// of the plugin's threads sent a message to it.
#[inline]
pub fn call_on_main_thread_callback(&mut self) {
// SAFETY: this is done on the main thread, and the &mut reference guarantees no aliasing
unsafe { self.inner.on_main_thread() }
}
/// Returns a shared, read-only reference to the raw, C-FFI compatible representation
/// of thsi plugin instance.
#[inline]
pub fn raw_instance(&self) -> &clap_plugin {
self.inner.raw_instance()
}
/// Returns the [`PluginEntry`] this plugin instance was loaded from.
#[inline]
pub fn entry(&self) -> &PluginEntry {
self.inner.entry()
}
/// Returns `true` if the current plugin instance has been activated.
#[inline]
pub fn is_active(&self) -> bool {
self.inner.is_active()
}
/// Access the [`SharedHandler`] instance associated to this plugin instance using the given callback.
///
/// The callback's return value `R` is returned directly by this method.
///
/// Accessing the [`HostHandlers`] types can only be done with accessor callbacks because this
/// type is self-referential: both the [`HostHandlers`] and the plugin's instance data hold
/// references to each other, and both are owned by this type.
#[inline]
pub fn access_shared_handler<'s, R>(
&'s self,
access: impl for<'a> FnOnce(&'s <H as HostHandlers>::Shared<'a>) -> R,
) -> R {
access(self.inner.wrapper().shared())
}
/// Access a shared reference to [`MainThreadHandler`] instance associated to this plugin instance using the given callback.
///
/// The callback's return value `R` is returned directly by this method.
///
/// Accessing the [`HostHandlers`] types can only be done with accessor callbacks because this
/// type is self-referential: both the [`HostHandlers`] and the plugin's instance data hold
/// references to each other, and both are owned by this type.
#[inline]
pub fn access_handler<R>(
&self,
access: impl for<'a> FnOnce(&<H as HostHandlers>::MainThread<'a>) -> R,
) -> R {
// SAFETY: This type guarantees it can only be called on the main thread
let Some(result) = (unsafe { self.inner.wrapper().on_main_thread(access) }) else {
// PANIC: main_thread can only be None if called from within init()
unreachable!()
};
result
}
/// Returns a thread-safe handle to the plugin.
///
/// Unlike the [`PluginMainThreadHandle`] returned by [`plugin_handle`](Self::plugin_handle),
/// this handle can be cloned and sent to other threads.
///
/// However, it has fewer capabilities, and cannot be used for operations that have to be
/// performed on the main-thread (e.g. GUI).
#[inline]
pub fn plugin_shared_handle(&self) -> PluginSharedHandle<'_> {
self.inner.plugin_shared()
}
/// Returns a main-thread handle to the plugin.
#[inline]
pub fn plugin_handle(&mut self) -> PluginMainThreadHandle<'_> {
// SAFETY: this type can only exist on the main thread.
unsafe { PluginMainThreadHandle::new(self.inner.raw_instance().into()) }
}
/// Returns a main-thread handle to the plugin, that can only exist while the plugin is in the 'inactive' state.
///
/// Calling this method also prevents the plugin instance to be activated as long as the handle exists.
///
/// If the plugin is actually activated, this returns `None`.
#[inline]
pub fn inactive_plugin_handle(&mut self) -> Option<InactivePluginMainThreadHandle<'_>> {
if self.inner.is_active() {
return None;
}
// SAFETY: this type can only exist on the main thread.
// We also checked above that the plugin isn't in the active state, and the handle keeps
// this instance mutably borrowed, so it is not possible to activate the plugin while the
// handle still exists.
Some(unsafe { InactivePluginMainThreadHandle::new(self.inner.raw_instance().into()) })
}
}
impl<H: HostHandlers> Drop for PluginInstance<H> {
fn drop(&mut self) {
// Only drop our Arc if we are the sole owner.
// This leaks the plugin instance, but prevents accidentally transferring ownership to the
// audio thread if the audio processor handle is still around somewhere.
if Arc::get_mut(&mut self.inner).is_some() {
// SAFETY: We can only call this once (as we're in Drop), and we never use the inner
// value again afterward.
unsafe { ManuallyDrop::drop(&mut self.inner) }
};
}
}
#[cfg(test)]
mod test {
extern crate static_assertions as sa;
use super::*;
sa::assert_not_impl_any!(PluginInstance<()>: Send, Sync);
}