/
SyIIDCAppDelegate.m
230 lines (164 loc) · 7.26 KB
/
SyIIDCAppDelegate.m
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
//
// SyIIDCAppDelegate.m
// SyphonIIDCServer
//
// Created by Andrea Cremaschi on 16/10/11.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
#import "SyIIDCAppDelegate.h"
#import "StatusItemManager.h"
#import "IIDCContext.h"
#import "IIDCCamera.h"
#import "IIDCCaptureSession.h"
// Interface
#import "SettingsWindowController.h"
#import "NSStringToNSNumberValueTransformer.h"
@interface SyIIDCAppDelegate () <StatusItemManagerDatasource, IIDCCaptureSessionDelegate>
@property (strong ) IIDCCaptureSession *captureSession;
@property (nonatomic, strong) NSArray *orderedArrayOfDevicesGUIDs;
@property (nonatomic, strong) IIDCContext *iidcContext;
@property (nonatomic, strong) SettingsWindowController *settingsWindowPanel;
@property (nonatomic, strong) NSError *connectionError;
@end
@implementation SyIIDCAppDelegate
@synthesize mainWindowController;
+(void)initialize {
[super initialize];
// create an autoreleased instance of our value transformer
NSStringToNSNumberValueTransformer *transfomer = [[NSStringToNSNumberValueTransformer alloc] init];
// register it with the name that we refer to it with
[NSValueTransformer setValueTransformer:transfomer
forName:@"NSStringToNSNumberValueTransformer"];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
self.iidcContext = [IIDCContext new];
[[StatusItemManager sharedManager] updateStatusItem];
[StatusItemManager sharedManager].dataSource = self;
}
-(void)applicationWillTerminate:(NSNotification *)notification {
if (self.captureSession) {
[self.captureSession stopCapturing: nil];
self.captureSession = nil;
}
}
#pragma mark - StatusItemManagerDatasource
- (void) updateAvailableDevicesListIfNeeded {
[self willChangeValueForKey:@"orderedArrayOfDevicesGUIDs"];
_orderedArrayOfDevicesGUIDs = nil;
[self didChangeValueForKey:@"orderedArrayOfDevicesGUIDs"];
}
- (NSString *)activeCameraGUID {
return self.captureSession.camera.deviceIdentifier;
}
- (NSNumber *)currentResolutionID {
return @([self.captureSession.camera videomode]);
}
- (NSString *)currentResolutionDescription {
IIDCCamera *camera = self.captureSession.camera;
double framerate = [camera framerate];
NSInteger videomode = camera.videomode;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"dc1394_videomode == %d", videomode];
NSArray *item = [camera.videomodes filteredArrayUsingPredicate: predicate];
NSDictionary *videomodeDict = item.firstObject;
int width = [videomodeDict[@"width"] intValue];
int height = [videomodeDict[@"height"] intValue];
return [NSString stringWithFormat:@"%ix%i@%.ffps", width, height, framerate];
}
- (NSArray *)orderedArrayOfDevicesGUIDs {
if (_orderedArrayOfDevicesGUIDs) return _orderedArrayOfDevicesGUIDs;
NSDictionary *availableCameras = self.iidcContext.availableCameras;
_orderedArrayOfDevicesGUIDs = [availableCameras keysSortedByValueUsingComparator:^NSComparisonResult(IIDCCamera *camera1, IIDCCamera *camera2) {
return [camera1.deviceName compare: camera2.deviceName];
}];
return _orderedArrayOfDevicesGUIDs;
}
- (NSDictionary *)dictionaryRepresentingAvailableDevices {
NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
for (NSString *deviceGUID in [self orderedArrayOfDevicesGUIDs]) {
NSString *cameraName = self.iidcContext.availableCameras[deviceGUID];
[mDict setObject: cameraName forKey:deviceGUID];
}
return [mDict copy];
}
- (NSArray *)arrayOfDictionariesRepresentingAvailableVideoModesForDeviceWithGUID:(NSString *)guid {
IIDCCamera *camera = [self.iidcContext cameraWithGUID: guid];
NSArray *orderedVideoModes = [camera.videomodes sortedArrayUsingDescriptors: @[
[NSSortDescriptor sortDescriptorWithKey:@"dc1394_videomode" ascending:YES]]];
NSMutableArray *mArray = [NSMutableArray array];
for (NSDictionary *videomode in orderedVideoModes) {
// TODO: quale chiave usare per le risoluzioni ?
[mArray addObject:@{@"name" : videomode[@"description"],
@"id" : videomode[@"dc1394_videomode"]}];
}
return [mArray copy];
}
#pragma mark - UI actions
- (void) selectVideoModeOfCamera: (id)sender {
NSMenuItem *menuItem = (NSMenuItem*)sender;
NSMenuItem *parentItem = menuItem.parentItem;
NSString *videoModeId = menuItem.representedObject;
NSString *deviceGUID = parentItem.representedObject;
self.connectionError = nil;
IIDCCamera *camera = [self.iidcContext cameraWithGUID:deviceGUID];
if (!camera) {
// TODO: error handling (the camera has been disconnected)
}
if (camera == self.captureSession.camera) {
NSError *error;
[self.captureSession stopCapturing: &error];
[camera setVideomode: videoModeId.integerValue];
[self.captureSession startCapturing: &error];
[[self settingsWindowPanel] setCaptureSession: self.captureSession];
} else {
// TODO:
if (self.captureSession)
[self.captureSession stopCapturing: nil];
self.captureSession = nil;
[camera reset];
[camera setVideomode: videoModeId.integerValue];
IIDCCaptureSession *captureSession = [[IIDCCaptureSession alloc] initWithCamera: camera];
captureSession.delegate = self;
NSError *error;
BOOL result = [captureSession startCapturing: &error];
if (!result) {
// TODO: error handling
NSLog(@"%@", error.localizedDescription);
return;
}
self.settingsWindowPanel.captureSession = captureSession;
self.captureSession = captureSession;
}
[[StatusItemManager sharedManager] updateStatusItem];
}
- (IBAction) setupCameraSettings: (id)sender {
SettingsWindowController *settingsPanel = self.settingsWindowPanel;
if (!settingsPanel)
settingsPanel = [[SettingsWindowController alloc] initWithWindowNibName:@"SettingsWindow"];
settingsPanel.captureSession = self.captureSession;
[settingsPanel.window makeKeyAndOrderFront: self];
self.settingsWindowPanel = settingsPanel;
}
- (void) disconnectCamera: (id)sender {
IIDCCaptureSession *captureSession = self.captureSession;
[captureSession stopCapturing: nil];
self.captureSession = nil;
// se è aperto, chiudi il pannello delle impostazioni
self.settingsWindowPanel.captureSession = nil;
[self.settingsWindowPanel close];
self.settingsWindowPanel = nil;
[[StatusItemManager sharedManager] updateStatusItem];
}
- (IBAction) resetCameraBus: (id)sender {
/* IIDCCaptureSession *captureSession = self.captureSession;
IIDCCamera *camera = captureSession.camera;*/
[self disconnectCamera: sender];
}
#pragma mark -Capture session delegate
-(void)captureSession:(IIDCCaptureSession *)captureSession didCaptureFrame:(void *)capturedFrame {
}
-(void)captureSession:(IIDCCaptureSession *)captureSession didFailWithError:(NSError *)error {
self.connectionError = error;
[[StatusItemManager sharedManager] updateStatusItem];
}
@end