|
|
|
@@ -1,7 +1,8 @@
|
|
|
|
|
// USB still cameras speak PTP, so macOS never mounts them as a disk. Image Capture (and this file)
|
|
|
|
|
// talks to them through ImageCaptureCore: open a session, list what is on the card (and each new
|
|
|
|
|
// shot as it appears), and download only the files the photographer picks. Nothing is copied on its
|
|
|
|
|
// own — that is deliberate: a full card would flood the ingest folder.
|
|
|
|
|
// talks to them through ImageCaptureCore: open a session briefly, list what is on the card, download
|
|
|
|
|
// what the photographer picked (or auto-ingest asked for), then close the session so the body can
|
|
|
|
|
// shoot again. Holding the session open (and especially requestEnableTethering) puts many Canons on
|
|
|
|
|
// the "computer / busy" viewfinder screen and blocks the shutter — that is why we pulse.
|
|
|
|
|
// One process can hold the PTP session; if EOS Utility has it, opening fails and we try again.
|
|
|
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
@@ -16,6 +17,11 @@ static NSString *g_dir = nil;
|
|
|
|
|
// session lists the whole card over USB; doing that at every launch made big cards feel hung.
|
|
|
|
|
static BOOL g_tether = NO;
|
|
|
|
|
|
|
|
|
|
/// How long the body stays free to shoot between card reads.
|
|
|
|
|
static const NSTimeInterval kPollSeconds = 2.5;
|
|
|
|
|
/// Close shortly after a catalog (or last download) so the viewfinder is not stuck on "computer".
|
|
|
|
|
static const NSTimeInterval kCloseAfterIdleSeconds = 0.4;
|
|
|
|
|
|
|
|
|
|
static void emit(NSDictionary *obj) {
|
|
|
|
|
if (!g_callback || !obj) return;
|
|
|
|
|
NSData *data = [NSJSONSerialization dataWithJSONObject:obj options:0 error:nil];
|
|
|
|
@@ -49,6 +55,12 @@ static BOOL is_still(ICCameraItem *item) {
|
|
|
|
|
@property (retain) NSMutableSet<NSString *> *fetched;
|
|
|
|
|
@property (retain) NSMutableSet<NSString *> *seenAtConnect;
|
|
|
|
|
@property (retain) NSMutableDictionary<NSString *, ICCameraFile *> *filesByName;
|
|
|
|
|
/// First full catalog of this tether-on cycle: names already on the card (not "new shots").
|
|
|
|
|
@property (assign) BOOL baselineTaken;
|
|
|
|
|
@property (assign) NSInteger pendingDownloads;
|
|
|
|
|
@property (retain) NSMutableArray<NSString *> *pendingImport;
|
|
|
|
|
/// Last file rows sent to the UI (kept when the session closes so the list does not vanish).
|
|
|
|
|
@property (retain) NSArray<NSDictionary *> *lastCatalog;
|
|
|
|
|
/// synced | unsupported. Nil until the session is ready.
|
|
|
|
|
@property (copy) NSString *clock;
|
|
|
|
|
@property (assign) double clockOffset;
|
|
|
|
@@ -65,6 +77,7 @@ static BOOL is_still(ICCameraItem *item) {
|
|
|
|
|
@property (copy) NSString *dir;
|
|
|
|
|
@property (assign) BOOL tether;
|
|
|
|
|
@property (assign) BOOL retryScheduled;
|
|
|
|
|
@property (assign) BOOL pollScheduled;
|
|
|
|
|
@property (retain) NSArray<NSDictionary *> *locked;
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
@@ -146,14 +159,17 @@ static BOOL is_still(ICCameraItem *item) {
|
|
|
|
|
cam.fetched = [NSMutableSet set];
|
|
|
|
|
cam.seenAtConnect = [NSMutableSet set];
|
|
|
|
|
cam.filesByName = [NSMutableDictionary dictionary];
|
|
|
|
|
cam.pendingImport = [NSMutableArray array];
|
|
|
|
|
cam.lastCatalog = @[];
|
|
|
|
|
cam.clockDone = NO;
|
|
|
|
|
cam.openRequested = NO;
|
|
|
|
|
cam.state = @"idle";
|
|
|
|
|
self.cams[uuid] = cam;
|
|
|
|
|
}
|
|
|
|
|
cam.device = (ICCameraDevice *)device;
|
|
|
|
|
cam.name = device.name ?: @"Câmara";
|
|
|
|
|
cam.vendor = device.usbVendorID;
|
|
|
|
|
cam.product = device.usbProductID;
|
|
|
|
|
cam.vendor = (int)device.usbVendorID;
|
|
|
|
|
cam.product = (int)device.usbProductID;
|
|
|
|
|
return cam;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -170,7 +186,8 @@ static BOOL is_still(ICCameraItem *item) {
|
|
|
|
|
@"error": cam.errorText ?: [NSNull null],
|
|
|
|
|
@"clock": cam.clock ?: [NSNull null],
|
|
|
|
|
@"clockOffset": cam.clockOffsetKnown ? @(cam.clockOffset) : [NSNull null],
|
|
|
|
|
@"catalogReady": @(cam.catalogReady),
|
|
|
|
|
// Keep the last list usable while the session is closed between pulses.
|
|
|
|
|
@"catalogReady": @(cam.catalogReady || cam.lastCatalog.count > 0),
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
if (self.locked) [list addObjectsFromArray:self.locked];
|
|
|
|
@@ -179,27 +196,41 @@ static BOOL is_still(ICCameraItem *item) {
|
|
|
|
|
|
|
|
|
|
- (void)publishCatalog:(PRCam *)cam {
|
|
|
|
|
NSMutableArray *files = [NSMutableArray array];
|
|
|
|
|
for (NSString *name in cam.filesByName) {
|
|
|
|
|
ICCameraFile *file = cam.filesByName[name];
|
|
|
|
|
BOOL isNew = ![cam.seenAtConnect containsObject:name];
|
|
|
|
|
NSDate *created = file.creationDate;
|
|
|
|
|
[files addObject:@{
|
|
|
|
|
@"name": name,
|
|
|
|
|
@"size": @(file.fileSize),
|
|
|
|
|
@"createdMs": created ? @((long long)(created.timeIntervalSince1970 * 1000.0)) : [NSNull null],
|
|
|
|
|
@"isNew": @(isNew),
|
|
|
|
|
@"imported": @([cam.fetched containsObject:name]),
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
[files sortUsingComparator:^NSComparisonResult(NSDictionary *a, NSDictionary *b) {
|
|
|
|
|
NSNumber *ca = a[@"createdMs"];
|
|
|
|
|
NSNumber *cb = b[@"createdMs"];
|
|
|
|
|
if ([ca isKindOfClass:[NSNumber class]] && [cb isKindOfClass:[NSNumber class]] && ![ca isEqual:[NSNull null]] && ![cb isEqual:[NSNull null]]) {
|
|
|
|
|
NSComparisonResult byDate = [cb compare:ca];
|
|
|
|
|
if (byDate != NSOrderedSame) return byDate;
|
|
|
|
|
if (cam.device.hasOpenSession && cam.filesByName.count) {
|
|
|
|
|
for (NSString *name in cam.filesByName) {
|
|
|
|
|
ICCameraFile *file = cam.filesByName[name];
|
|
|
|
|
BOOL isNew = ![cam.seenAtConnect containsObject:name];
|
|
|
|
|
NSDate *created = file.creationDate;
|
|
|
|
|
[files addObject:@{
|
|
|
|
|
@"name": name,
|
|
|
|
|
@"size": @(file.fileSize),
|
|
|
|
|
@"createdMs": created ? @((long long)(created.timeIntervalSince1970 * 1000.0)) : [NSNull null],
|
|
|
|
|
@"isNew": @(isNew),
|
|
|
|
|
@"imported": @([cam.fetched containsObject:name]),
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
return [((NSString *)a[@"name"]) compare:b[@"name"]];
|
|
|
|
|
}];
|
|
|
|
|
[files sortUsingComparator:^NSComparisonResult(NSDictionary *a, NSDictionary *b) {
|
|
|
|
|
NSNumber *ca = a[@"createdMs"];
|
|
|
|
|
NSNumber *cb = b[@"createdMs"];
|
|
|
|
|
if ([ca isKindOfClass:[NSNumber class]] && [cb isKindOfClass:[NSNumber class]] && ![ca isEqual:[NSNull null]] && ![cb isEqual:[NSNull null]]) {
|
|
|
|
|
NSComparisonResult byDate = [cb compare:ca];
|
|
|
|
|
if (byDate != NSOrderedSame) return byDate;
|
|
|
|
|
}
|
|
|
|
|
return [((NSString *)a[@"name"]) compare:b[@"name"]];
|
|
|
|
|
}];
|
|
|
|
|
cam.lastCatalog = files;
|
|
|
|
|
} else if (cam.lastCatalog.count) {
|
|
|
|
|
// Session closed: refresh only the imported flags on the last snapshot.
|
|
|
|
|
NSMutableArray *refreshed = [NSMutableArray arrayWithCapacity:cam.lastCatalog.count];
|
|
|
|
|
for (NSDictionary *row in cam.lastCatalog) {
|
|
|
|
|
NSMutableDictionary *copy = [row mutableCopy];
|
|
|
|
|
NSString *name = copy[@"name"];
|
|
|
|
|
if (name) copy[@"imported"] = @([cam.fetched containsObject:name]);
|
|
|
|
|
[refreshed addObject:copy];
|
|
|
|
|
}
|
|
|
|
|
files = refreshed;
|
|
|
|
|
cam.lastCatalog = files;
|
|
|
|
|
}
|
|
|
|
|
emit(@{@"kind": @"catalog", @"cameraId": cam.uuid ?: @"", @"files": files});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -273,6 +304,56 @@ static BOOL is_still(ICCameraItem *item) {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// After the card is free again, wait a beat then reopen briefly to catch new shots.
|
|
|
|
|
- (void)schedulePoll {
|
|
|
|
|
if (self.pollScheduled || !self.tether) return;
|
|
|
|
|
self.pollScheduled = YES;
|
|
|
|
|
__weak PRCameraHub *weak = self;
|
|
|
|
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kPollSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
|
|
|
|
PRCameraHub *hub = weak;
|
|
|
|
|
if (!hub) return;
|
|
|
|
|
hub.pollScheduled = NO;
|
|
|
|
|
if (!hub.tether) return;
|
|
|
|
|
for (NSString *key in hub.cams.allKeys) {
|
|
|
|
|
PRCam *cam = hub.cams[key];
|
|
|
|
|
if (cam.pendingDownloads > 0) continue;
|
|
|
|
|
cam.openRequested = NO;
|
|
|
|
|
[hub requestOpenIfReady:cam];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Free the shutter: close PTP once catalog (and downloads) are done.
|
|
|
|
|
- (void)closeWhenIdle:(PRCam *)cam {
|
|
|
|
|
if (!self.tether) return;
|
|
|
|
|
if (cam.pendingDownloads > 0 || cam.pendingImport.count > 0) return;
|
|
|
|
|
if (!cam.device.hasOpenSession) {
|
|
|
|
|
[self schedulePoll];
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
NSString *uuid = cam.uuid;
|
|
|
|
|
__weak PRCameraHub *weak = self;
|
|
|
|
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kCloseAfterIdleSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
|
|
|
|
PRCameraHub *hub = weak;
|
|
|
|
|
if (!hub || !hub.tether) return;
|
|
|
|
|
PRCam *still = hub.cams[uuid];
|
|
|
|
|
if (!still || still.pendingDownloads > 0 || still.pendingImport.count > 0) return;
|
|
|
|
|
if (still.device.hasOpenSession) [still.device requestCloseSession];
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)drainPendingImport:(PRCam *)cam {
|
|
|
|
|
if (!cam.device.hasOpenSession || cam.pendingImport.count == 0) return;
|
|
|
|
|
NSArray *names = [cam.pendingImport copy];
|
|
|
|
|
[cam.pendingImport removeAllObjects];
|
|
|
|
|
for (NSString *name in names) {
|
|
|
|
|
ICCameraFile *file = cam.filesByName[name];
|
|
|
|
|
if (file) [self download:file cam:cam];
|
|
|
|
|
}
|
|
|
|
|
[self publishCatalog:cam];
|
|
|
|
|
if (cam.pendingDownloads == 0) [self closeWhenIdle:cam];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString *)uniqueName:(NSString *)original {
|
|
|
|
|
NSString *base = original.lastPathComponent ?: @"photo.JPG";
|
|
|
|
|
NSString *stem = base.stringByDeletingPathExtension;
|
|
|
|
@@ -297,6 +378,7 @@ static BOOL is_still(ICCameraItem *item) {
|
|
|
|
|
NSURL *url = [NSURL fileURLWithPath:self.dir isDirectory:YES];
|
|
|
|
|
if (!url) return;
|
|
|
|
|
[cam.fetched addObject:file.name];
|
|
|
|
|
cam.pendingDownloads++;
|
|
|
|
|
NSDictionary *options = @{
|
|
|
|
|
ICDownloadsDirectoryURL: url,
|
|
|
|
|
ICSaveAsFilename: save,
|
|
|
|
@@ -317,12 +399,15 @@ static BOOL is_still(ICCameraItem *item) {
|
|
|
|
|
if (self.dir.length == 0 || names.count == 0) return;
|
|
|
|
|
for (NSString *key in self.cams) {
|
|
|
|
|
PRCam *cam = self.cams[key];
|
|
|
|
|
if (!cam.device.hasOpenSession) continue;
|
|
|
|
|
for (NSString *name in names) {
|
|
|
|
|
ICCameraFile *file = cam.filesByName[name];
|
|
|
|
|
if (file) [self download:file cam:cam];
|
|
|
|
|
if (![cam.pendingImport containsObject:name]) [cam.pendingImport addObject:name];
|
|
|
|
|
}
|
|
|
|
|
if (cam.device.hasOpenSession && cam.catalogReady) {
|
|
|
|
|
[self drainPendingImport:cam];
|
|
|
|
|
} else {
|
|
|
|
|
cam.openRequested = NO;
|
|
|
|
|
[self requestOpenIfReady:cam];
|
|
|
|
|
}
|
|
|
|
|
[self publishCatalog:cam];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -364,10 +449,8 @@ static BOOL is_still(ICCameraItem *item) {
|
|
|
|
|
}
|
|
|
|
|
cam.state = @"tethered";
|
|
|
|
|
cam.errorText = nil;
|
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
|
[cam.device requestEnableTethering];
|
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
|
// Do NOT call requestEnableTethering: that puts many bodies on the computer/busy screen and
|
|
|
|
|
// blocks the shutter. We only need a short PTP session to list and download files.
|
|
|
|
|
[self syncClock:cam];
|
|
|
|
|
[self publish];
|
|
|
|
|
}
|
|
|
|
@@ -378,13 +461,18 @@ static BOOL is_still(ICCameraItem *item) {
|
|
|
|
|
if (!cam) return;
|
|
|
|
|
cam.openRequested = NO;
|
|
|
|
|
cam.catalogReady = NO;
|
|
|
|
|
[cam.filesByName removeAllObjects];
|
|
|
|
|
if (self.tether) {
|
|
|
|
|
cam.state = @"busy";
|
|
|
|
|
[self scheduleRetry];
|
|
|
|
|
// Session closed on purpose: body is free to shoot until the next poll.
|
|
|
|
|
cam.state = @"tethered";
|
|
|
|
|
cam.errorText = nil;
|
|
|
|
|
[self publish];
|
|
|
|
|
[self publishCatalog:cam];
|
|
|
|
|
[self schedulePoll];
|
|
|
|
|
} else {
|
|
|
|
|
cam.state = @"idle";
|
|
|
|
|
[self publish];
|
|
|
|
|
}
|
|
|
|
|
[self publish];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)didRemoveDevice:(ICDevice *)device {
|
|
|
|
@@ -400,10 +488,13 @@ static BOOL is_still(ICCameraItem *item) {
|
|
|
|
|
|
|
|
|
|
- (void)deviceDidBecomeReadyWithCompleteContentCatalog:(ICCameraDevice *)device {
|
|
|
|
|
PRCam *cam = [self camFor:device];
|
|
|
|
|
// Snapshot what was already on the card so the UI can mark later shots as "new".
|
|
|
|
|
[cam.seenAtConnect removeAllObjects];
|
|
|
|
|
for (ICCameraItem *item in cam.device.mediaFiles) {
|
|
|
|
|
if (item.name && is_still(item)) [cam.seenAtConnect addObject:item.name];
|
|
|
|
|
// Baseline once per tether-on cycle so each poll does not re-mark the whole card as "old".
|
|
|
|
|
if (!cam.baselineTaken) {
|
|
|
|
|
[cam.seenAtConnect removeAllObjects];
|
|
|
|
|
for (ICCameraItem *item in cam.device.mediaFiles) {
|
|
|
|
|
if (item.name && is_still(item)) [cam.seenAtConnect addObject:item.name];
|
|
|
|
|
}
|
|
|
|
|
cam.baselineTaken = YES;
|
|
|
|
|
}
|
|
|
|
|
cam.catalogReady = YES;
|
|
|
|
|
if ([cam.state isEqualToString:@"opening"]) cam.state = @"tethered";
|
|
|
|
@@ -411,16 +502,17 @@ static BOOL is_still(ICCameraItem *item) {
|
|
|
|
|
[self rebuildCatalog:cam];
|
|
|
|
|
[self syncClock:cam];
|
|
|
|
|
[self publish];
|
|
|
|
|
[self drainPendingImport:cam];
|
|
|
|
|
[self closeWhenIdle:cam];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)cameraDevice:(ICCameraDevice *)camera didAddItems:(NSArray<ICCameraItem *> *)items {
|
|
|
|
|
PRCam *cam = [self camFor:camera];
|
|
|
|
|
// List only — never auto-download. The photographer picks what to pull.
|
|
|
|
|
// List only — never auto-download. The photographer picks what to pull (or auto-ingest via the UI).
|
|
|
|
|
for (ICCameraItem *item in items) [self indexItem:item cam:cam];
|
|
|
|
|
// During the first pass Image Capture calls this for every file on the card. Publishing each
|
|
|
|
|
// time sorts and ships thousands of JSON rows to the UI. Wait for the complete catalog (or a
|
|
|
|
|
// real tethered shot after catalogReady) before emitting. Items seen before catalogReady are
|
|
|
|
|
// also snapshotted as "already on the card", so they must not be treated as new shots.
|
|
|
|
|
// real tethered shot after catalogReady) before emitting.
|
|
|
|
|
if (cam.catalogReady) [self publishCatalog:cam];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -470,25 +562,33 @@ static BOOL is_still(ICCameraItem *item) {
|
|
|
|
|
|
|
|
|
|
- (void)didDownloadFile:(ICCameraFile *)file error:(NSError *)error options:(NSDictionary *)options contextInfo:(void *)contextInfo {
|
|
|
|
|
NSString *camId = contextInfo ? CFBridgingRelease(contextInfo) : nil;
|
|
|
|
|
PRCam *cam = camId ? self.cams[camId] : nil;
|
|
|
|
|
if (cam && cam.pendingDownloads > 0) cam.pendingDownloads--;
|
|
|
|
|
if (error || self.dir.length == 0) {
|
|
|
|
|
if (camId && file.name) {
|
|
|
|
|
PRCam *cam = self.cams[camId];
|
|
|
|
|
if (cam && file.name) {
|
|
|
|
|
[cam.fetched removeObject:file.name];
|
|
|
|
|
[self publishCatalog:cam];
|
|
|
|
|
}
|
|
|
|
|
if (cam) [self closeWhenIdle:cam];
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
NSString *saved = options[ICSavedFilename];
|
|
|
|
|
if (![saved isKindOfClass:[NSString class]] || saved.length == 0) saved = file.name;
|
|
|
|
|
if (saved.length == 0) return;
|
|
|
|
|
if (saved.length == 0) {
|
|
|
|
|
if (cam) [self closeWhenIdle:cam];
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
NSString *path = [self.dir stringByAppendingPathComponent:saved];
|
|
|
|
|
NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
|
|
|
|
|
unsigned long long size = [attrs fileSize];
|
|
|
|
|
if (size == 0) return;
|
|
|
|
|
if (size == 0) {
|
|
|
|
|
if (cam) [self closeWhenIdle:cam];
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
emit(@{@"kind": @"file", @"path": path, @"name": saved, @"size": @(size)});
|
|
|
|
|
if (camId) {
|
|
|
|
|
PRCam *cam = self.cams[camId];
|
|
|
|
|
if (cam) [self publishCatalog:cam];
|
|
|
|
|
if (cam) {
|
|
|
|
|
[self publishCatalog:cam];
|
|
|
|
|
[self closeWhenIdle:cam];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -534,9 +634,11 @@ void pr_camera_set_tether(int on) {
|
|
|
|
|
g_tether = on ? YES : NO;
|
|
|
|
|
if (!g_hub) return;
|
|
|
|
|
g_hub.tether = g_tether;
|
|
|
|
|
g_hub.pollScheduled = NO;
|
|
|
|
|
for (NSString *key in g_hub.cams.allKeys) {
|
|
|
|
|
PRCam *cam = g_hub.cams[key];
|
|
|
|
|
if (g_hub.tether) {
|
|
|
|
|
cam.baselineTaken = NO;
|
|
|
|
|
cam.openRequested = NO;
|
|
|
|
|
[g_hub requestOpenIfReady:cam];
|
|
|
|
|
} else if (cam.device.hasOpenSession) {
|
|
|
|
@@ -568,7 +670,11 @@ void pr_camera_refresh_catalog(void) {
|
|
|
|
|
if (!g_hub) return;
|
|
|
|
|
for (NSString *key in g_hub.cams.allKeys) {
|
|
|
|
|
PRCam *cam = g_hub.cams[key];
|
|
|
|
|
if (cam.catalogReady) [g_hub rebuildCatalog:cam];
|
|
|
|
|
if (cam.device.hasOpenSession && cam.catalogReady) [g_hub rebuildCatalog:cam];
|
|
|
|
|
else if (g_hub.tether) {
|
|
|
|
|
cam.openRequested = NO;
|
|
|
|
|
[g_hub requestOpenIfReady:cam];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|