Skip to content

Commit

Permalink
Experiments
Browse files Browse the repository at this point in the history
- Added test code to try and handle Blocks between Rust & Objective-C land.
- Added "Media Path" & "Media Bookmark Data" parameters.
- Added ability to export a Gyroflow project.
- Resolve issue with sandbox issues when dragging and dropping a file into Final Cut Pro Inspector.
- Added `isOfficialLensLoaded` method to Rust. Currently not used.
  • Loading branch information
latenitefilms committed Jul 21, 2023
1 parent a3902e2 commit 14dc6d1
Show file tree
Hide file tree
Showing 8 changed files with 501 additions and 107 deletions.
1 change: 1 addition & 0 deletions Source/Frameworks/gyroflow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ lru = "0.10"
nalgebra = { version = "0.32", features = ["serde-serialize"] }
once_cell = "1.16.0"
metal = { version = "0.25.0", git = "https://github.com/gfx-rs/metal-rs.git", rev = "a6a0446" }
block2 = "0.2.0"
11 changes: 11 additions & 0 deletions Source/Frameworks/gyroflow/inc/gyroflow.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,18 @@ const char* doesGyroflowProjectContainStabilisationData(
const char* gyroflow_project_data
);

const char* isOfficialLensLoaded(
const char* gyroflow_project_data
);

const char* loadLensProfile(
const char* gyroflow_project_data,
const char* lens_profile_path
);

//
// Just a test:
//
int32_t run_block(int32_t (^block)(int32_t, int32_t));

//double set_keyframe_provider(const char *, double (^block)(int32_t, double));
96 changes: 96 additions & 0 deletions Source/Frameworks/gyroflow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ use std::os::raw::c_char; // Allows us to use `*const c_uchar`
use std::sync::Arc; // Adds Atomic Reference Count support
use std::sync::atomic::AtomicBool; // The AtomicBool type is a type of atomic variable that can be used in concurrent (multi-threaded) contexts.
use std::sync::Mutex; // A mutual exclusion primitive useful for protecting shared data
use block2::Block;

//---------------------------------------------------------
// Test Block:
//---------------------------------------------------------
#[no_mangle]
unsafe extern "C" fn run_block(block: &Block<(i32, i32), i32>) -> i32 {
block.call((5, 8))
}

//---------------------------------------------------------
// We only want to setup the Gyroflow Manager once for
Expand All @@ -30,6 +39,93 @@ lazy_static! {
static ref MANAGER_CACHE: Mutex<LruCache<String, Arc<StabilizationManager>>> = Mutex::new(LruCache::new(std::num::NonZeroUsize::new(8).unwrap()));
}

//---------------------------------------------------------
// Set Keyframe Provider:
//---------------------------------------------------------
/*
struct UnsafeBlock(Block<(*const c_char, f64), f64>);
unsafe impl Send for UnsafeBlock {}
unsafe impl Sync for UnsafeBlock {}
unsafe extern "C" fn set_keyframe_provider(cache_key: *const c_char, block: &UnsafeBlock) -> bool {
let block = block2::RcBlock::copy(block.0);
let cache_key = unsafe { CStr::from_ptr(cache_key) }.to_string_lossy().to_string();
let mut cache = MANAGER_CACHE.lock().unwrap();
if let Some(manager) = cache.get(&cache_key) {
log::error!("[Gyroflow Toolbox Rust] Got manager for key:: {cache_key}");
manager.keyframes.write().set_custom_provider(move |kf, typ, timestamp_ms| -> Option<f64> {
let use_gyroflow_internal_keyframes = false; // TODO: set from UI
if use_gyroflow_internal_keyframes && kf.is_keyframed_internally(typ) { return None; }
let keyframe_type_str = std::ffi::CString::new(format!("{typ:?}")).unwrap();
let value_from_fcpx = block.0.call((keyframe_type_str.as_ptr(), timestamp_ms));
log::error!("[Gyroflow Toolbox Rust] Got keyframe value from FCPX:: {value_from_fcpx:?}");
Some(value_from_fcpx)
});
true
} else {
log::error!("[Gyroflow Toolbox Rust] Didn't find cache key: {cache_key}, keyframe provider not set");
false
}
}
*/

//---------------------------------------------------------
// Is official lens loaded?
// Returns "YES" or "NO".
//---------------------------------------------------------
#[no_mangle]
pub extern "C" fn isOfficialLensLoaded(
gyroflow_project_data: *const c_char,
) -> *const c_char {
//---------------------------------------------------------
// Convert the Gyroflow Project data to a `&str`:
//---------------------------------------------------------
let gyroflow_project_data_pointer = unsafe { CStr::from_ptr(gyroflow_project_data) };
let gyroflow_project_data_string = gyroflow_project_data_pointer.to_string_lossy();

let stab = StabilizationManager::default();

//---------------------------------------------------------
// Import the `gyroflow_project_data_string`:
//---------------------------------------------------------
let blocking = true;
let cancel_flag = Arc::new(AtomicBool::new(false));
let mut is_preset = false;
match stab.import_gyroflow_data(
gyroflow_project_data_string.as_bytes(),
blocking,
None,
|_|(),
cancel_flag,
&mut is_preset
) {
Ok(_) => {
//---------------------------------------------------------
// Is official lens loaded?
//---------------------------------------------------------
let is_official_lens_loaded = stab.lens.read().official;
if is_official_lens_loaded {
let result = CString::new("YES").unwrap();
return result.into_raw()
} else {
let result = CString::new("NO").unwrap();
return result.into_raw()
}
},
Err(e) => {
// Handle the error case
log::error!("[Gyroflow Toolbox Rust] Error importing gyroflow data: {:?}", e);

let result = CString::new("NO").unwrap();
return result.into_raw()
},
}
}

//---------------------------------------------------------
// Does the Gyroflow Project contain Stabilisation Data?
// Returns "PASS" or "FAIL".
Expand Down
42 changes: 28 additions & 14 deletions Source/Gyroflow/Plugin/CustomDropZoneView.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#import "CustomDropZoneView.h"
#import <FxPlug/FxPlugSDK.h>

static NSString *const kFinalCutProUTI = @"com.apple.flexo.proFFPasteboardUTI";

@interface CustomDropZoneView ()

@property (nonatomic) bool dragIsOver;
Expand Down Expand Up @@ -38,8 +36,7 @@ - (instancetype)initWithAPIManager:(id<PROAPIAccessing>)apiManager
{
_apiManager = apiManager;

//[self registerForDraggedTypes:@[kFinalCutProUTI]];
NSArray *sortedPasteboardTypes = @[@"com.apple.finalcutpro.xml.v1-11", @"com.apple.finalcutpro.xml.v1-10", @"com.apple.finalcutpro.xml.v1-9", @"com.apple.finalcutpro.xml", kFinalCutProUTI, NSPasteboardTypeFileURL];
NSArray *sortedPasteboardTypes = @[NSPasteboardTypeFileURL, @"com.apple.finalcutpro.xml.v1-11", @"com.apple.finalcutpro.xml.v1-10", @"com.apple.finalcutpro.xml.v1-9", @"com.apple.finalcutpro.xml", @"com.apple.flexo.proFFPasteboardUTI"];
[self registerForDraggedTypes:sortedPasteboardTypes];

self.wantsLayer = YES;
Expand All @@ -62,7 +59,7 @@ - (instancetype)initWithAPIManager:(id<PROAPIAccessing>)apiManager
//---------------------------------------------------------
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {

NSArray *sortedPasteboardTypes = @[@"com.apple.finalcutpro.xml.v1-11", @"com.apple.finalcutpro.xml.v1-10", @"com.apple.finalcutpro.xml.v1-9", @"com.apple.finalcutpro.xml", NSPasteboardTypeFileURL];
NSArray *sortedPasteboardTypes = @[NSPasteboardTypeFileURL, @"com.apple.finalcutpro.xml.v1-11", @"com.apple.finalcutpro.xml.v1-10", @"com.apple.finalcutpro.xml.v1-9", @"com.apple.finalcutpro.xml"];
for (NSPasteboardType pasteboardType in sortedPasteboardTypes) {
if ( [[[sender draggingPasteboard] types] containsObject:pasteboardType] ) {
_dragIsOver = true;
Expand Down Expand Up @@ -92,21 +89,38 @@ - (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:NSPasteboardURLReadingFileURLsOnlyKey];
NSArray *fileURLs = [pasteboard readObjectsForClasses:classArray options:options];
for (NSURL *fileURL in fileURLs) {
// Do something with fileURL
NSLog(@"[Gyroflow Toolbox Renderer] Dropped file: %@", [fileURL path]);
//---------------------------------------------------------
// Trigger Dropped File Method:
//---------------------------------------------------------

NSLog(@"[Gyroflow Toolbox Renderer] Dropped file path: %@", [fileURL path]);

//---------------------------------------------------------
// Create a new security-scoped bookmark:
//---------------------------------------------------------
NSError *bookmarkError = nil;
NSURLBookmarkCreationOptions bookmarkOptions = NSURLBookmarkCreationWithSecurityScope | NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess;
NSData *bookmarkData = [fileURL bookmarkDataWithOptions:bookmarkOptions
includingResourceValuesForKeys:nil
relativeToURL:nil
error:&bookmarkError];

if ([fileURL startAccessingSecurityScopedResource]) {
NSLog(@"[Gyroflow Toolbox Renderer] SUCCESSFUL startAccessingSecurityScopedResource");
} else {
NSLog(@"[Gyroflow Toolbox Renderer] FAILED to startAccessingSecurityScopedResource");
if (bookmarkError != nil) {
NSLog(@"[Gyroflow Toolbox Renderer] ERROR - Unable to create security-scoped bookmark for dragged file ('%@') due to: %@", fileURL, bookmarkError);
return NO;
}

if (bookmarkData == nil) {
NSLog(@"[Gyroflow Toolbox Renderer] ERROR - Unable to create security-scoped bookmark for dragged file ('%@') due to: Bookmark is nil.", fileURL);
return NO;
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-method-access"
[_parentPlugin importDroppedMedia:fileURL];
BOOL result = [_parentPlugin importDroppedMedia:bookmarkData];
#pragma clang diagnostic pop

return YES;
return result;
}

} else {
Expand Down
4 changes: 4 additions & 0 deletions Source/Gyroflow/Plugin/GyroflowConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,13 @@ enum {
// Hidden Metadata:
//---------------------------------------------------------
kCB_UniqueIdentifier = 500,

kCB_GyroflowProjectPath = 60,
kCB_GyroflowProjectBookmarkData = 70,
kCB_GyroflowProjectData = 80,

kCB_MediaPath = 510,
kCB_MediaBookmarkData = 520,
};

//---------------------------------------------------------
Expand Down
Loading

0 comments on commit 14dc6d1

Please sign in to comment.