-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLocalCamera.m
More file actions
531 lines (448 loc) · 14.2 KB
/
Copy pathLocalCamera.m
File metadata and controls
531 lines (448 loc) · 14.2 KB
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//
// LocalCamera.m
// Gawker
//
// Created by Phil Piwonka on 7/1/05.
// Copyright 2005 Phil Piwonka. All rights reserved.
//
#import "LocalCamera.h"
#import "CameraController.h"
#import "LocalCameraController.h"
#import "PreferenceController.h"
#import "SharePreferenceController.h"
#import "CameraPreferences.h"
#import "CameraFeaturesWindowController.h"
#import "CameraStatusView.h"
#import "ImageTransitionView.h"
#import "LapseMovie.h"
#import "ImageServer.h"
#import <QTKit/QTMovie.h>
#import <QTKit/QTKit.h>
@interface LocalCamera (PrivateMethods)
//
// Server Stuff
//
- (BOOL)startSharing;
- (void)stopSharing;
- (void)shareTimerFired:(NSTimer *)timer;
- (NSString *)connectMessage;
- (NSString *)connectErrorMessage;
- (NSString *)connectSuccessMessage;
//
// Record panel handlers
//
- (void)recordDidEnd:(NSSavePanel *)sheet returnCode:(int)code
contextInfo:(void *)contextInfo;
@end
@implementation LocalCamera
- (id)init
{
return [self initWithCameraName:[NSString stringWithString:@"Camera"]];
}
- (id)initWithCameraName:(NSString *)name
{
self = [super initWithWindowNibName:@"LocalCamera"];
if (self) {
releaseOnWindowClose = NO;
cameraName = [name retain];
camController = [[LocalCameraController alloc] initWithDelegate:self
cameraName:cameraName];
if (camController) {
sharePreferences =
[[CameraPreferences prefs] prefsForDevice:cameraName];
[self setSourceDescription:[sharePreferences objectForKey:WNKShareDescriptionKey]];
icon = [[NSImage imageNamed:@"iSight"] retain];
[[self window]
setTitle:[NSString stringWithFormat:@"%@ - %@",
[camController sourceSubDescription],
[camController sourceDescription]]];
}
else {
NSLog(@"Error allocating LocalCameraController!");
}
}
return self;
}
- (void)dealloc
{
NSLog(@"LocalCamera -dealloc");
if (timeToNextFrameTimer) {
[timeToNextFrameTimer invalidate];
[timeToNextFrameTimer release];
timeToNextFrameTimer = nil;
}
[cameraName release];
[self setSharingEnabled:NO];
[super dealloc];
}
- (void)setSourceEnabled:(BOOL)enable openWindow:(BOOL)open share:(BOOL)share
{
if (!enable) {
[self setSharingEnabled:NO];
}
shareOnConnect = share;
[super setSourceEnabled:enable openWindow:open];
}
- (void)setSourceEnabled:(BOOL)enable openWindow:(BOOL)open
{
[self setSourceEnabled:enable openWindow:open share:NO];
}
- (void)setSourceDescription:(NSString *)description
{
[imageServer setServerDescription:description];
[camController setSourceDescription:description];
[sharePreferences setObject:description
forKey:WNKShareDescriptionKey];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:@"UpdateCameraTable"
object:self
userInfo:nil];
NSString *windowHeader = nil;
if ([self isSharing]) {
windowHeader = [NSString stringWithFormat:@"My Shared %@",
cameraName];
}
else {
windowHeader = [NSString stringWithFormat:@"My %@",
cameraName];
}
[[self window]
setTitle:[NSString stringWithFormat:@"%@ - %@",
windowHeader,
[camController sourceDescription]]];
}
- (void)scheduledStart:(NSTimer *)timer
{
NSDictionary *scheduling = [[[startTimer userInfo] retain] autorelease];
[super scheduledStart:timer];
if ([self isRecording]) {
double interval = [[scheduling objectForKey:SCHIntervalTag] floatValue];
[(LocalCameraController *)camController captureFrameAtInterval:interval];
}
}
- (int)sharePort
{
return [[sharePreferences objectForKey:WNKSharePortKey] intValue];
}
- (void)setSharePort:(int)portNum
{
[sharePreferences setObject:[NSNumber numberWithInt:portNum]
forKey:WNKSharePortKey];
}
- (BOOL)isSharing
{
return (imageServer) ? YES : NO;
}
- (BOOL)setSharingEnabled:(BOOL)state
{
BOOL wasSuccessful = YES;
if (state) {
wasSuccessful = [self startSharing];
if (wasSuccessful) {
[[self window]
setTitle:[NSString stringWithFormat:@"My Shared %@ - %@",
cameraName,
[camController sourceDescription]]];
}
}
else {
[self stopSharing];
[[self window]
setTitle:[NSString stringWithFormat:@"My %@ - %@",
cameraName,
[camController sourceDescription]]];
}
if (wasSuccessful) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"SharingChanged" object:self];
// [camFeaturesWindow updateSharingFields:state];
}
return wasSuccessful;
}
- (BOOL)isBonjourEnabled
{
BOOL isEnabled =
[[sharePreferences objectForKey:WNKShareBonjourKey] boolValue];
if (imageServer) {
isEnabled = [imageServer isBonjourEnabled];
}
return isEnabled;
}
- (void)setBonjourEnabled:(BOOL)enable
{
[imageServer setBonjourEnabled:enable];
[sharePreferences setObject:[NSNumber numberWithBool:enable]
forKey:WNKShareBonjourKey];
}
- (int)numberOfConnectedClients
{
int numConnected = 0;
if ([self isSharing]) {
numConnected = [imageServer numConnected];
}
return numConnected;
}
- (NSArray *)connectedClients
{
NSArray *clients = nil;
if ([self isSharing]) {
clients = [imageServer clients];
}
return clients;
}
- (BOOL)limitUsers
{
BOOL isLimit =
[[sharePreferences objectForKey:WNKShareLimitKey] boolValue];
if (imageServer) {
isLimit = [imageServer limitUsers];
}
return isLimit;
}
- (void)setLimitUsers:(BOOL)newLimit
{
if (imageServer) {
[imageServer setLimitUsers:newLimit];
}
[sharePreferences setObject:[NSNumber numberWithBool:newLimit]
forKey:WNKShareLimitKey];
}
- (int)shareLimit
{
int limit = [[sharePreferences objectForKey:WNKShareLimitNumKey] intValue];
if (imageServer) {
limit = [imageServer userLimit];
}
return limit;
}
- (void)setShareLimit:(int)limit
{
if (imageServer) {
[imageServer setUserLimit:limit];
}
[sharePreferences setObject:[NSNumber numberWithInt:limit]
forKey:WNKShareLimitNumKey];
}
- (int)shareInterval
{
return [[sharePreferences objectForKey:WNKShareFrequencyKey] intValue];
}
- (void)setShareInterval:(NSTimeInterval)newInterval;
{
if ([self isSharing]) {
if (shareTimer && newInterval != [shareTimer timeInterval]) {
NSLog(@"Changing share timer to %.1f",
newInterval);
[shareTimer invalidate];
[shareTimer release];
shareTimer = [[NSTimer scheduledTimerWithTimeInterval:newInterval
target:self
selector:@selector(shareTimerFired:)
userInfo:nil
repeats:YES] retain];
if (!shareTimer) {
NSLog(@"Error with share timer alloc!");
}
}
}
// FIXME accepting double, storing int?
[sharePreferences setObject:[NSNumber numberWithInt:newInterval]
forKey:WNKShareFrequencyKey];
}
- (BOOL)sharePasswordRequired
{
return [[sharePreferences objectForKey:WNKShareUsePasswordKey] boolValue];
}
- (void)setSharePasswordRequired:(BOOL)newReq
{
if (imageServer) {
[imageServer setRequirePassword:newReq];
}
NSLog(@"Setting sharePasswordRequired");
[sharePreferences setObject:[NSNumber numberWithBool:newReq]
forKey:WNKShareUsePasswordKey];
}
- (NSString *)sharePassword
{
return [sharePreferences objectForKey:WNKSharePasswordKey];
}
- (void)setSharePassword:(NSString *)newPass
{
if (imageServer) {
[imageServer setPassword:newPass];
}
NSLog(@"Setting sharePassword");
[sharePreferences setObject:newPass
forKey:WNKSharePasswordKey];
}
- (NSString *)cameraName
{
return cameraName;
}
- (void)deviceDisconnected
{
[camFeaturesWindow close];
if ([self isSourceEnabled]) {
[imageTransitionView setAcceptUpdates:NO];
[moreButton setEnabled:NO];
NSBeep();
[self showWindow:nil];
[self retain];
[self setSourceEnabled:NO];
[recordButton setEnabled:NO];
[camStatusView showErrorMessage:@"This device has been unplugged and is no longer available."
showButton:NO];
releaseOnWindowClose = YES;
}
}
- (void)windowWillClose:(NSNotification *)aNotification
{
if (releaseOnWindowClose) {
releaseOnWindowClose = NO;
NSLog(@"Releasing bonjourCamera");
[self autorelease];
}
}
@end
@implementation LocalCamera (ImageServerDelegateMethods)
- (void)imageServerUpdatedStats:(ImageServer *)server
{
NSLog(@"In imageServerUpdatedStats");
if ([imageServer limitUsers] == 0) {
[camController setSourceSubDescription:
[NSString stringWithFormat:@"My Shared %@ [%d]",
cameraName,
[imageServer numConnected]]];
}
else {
[camController setSourceSubDescription:
[NSString stringWithFormat:@"My Shared %@ [%d/%d]",
cameraName,
[imageServer numConnected],
[imageServer userLimit]]];
}
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:@"UpdateCameraTable"
object:self];
[nc postNotificationName:@"SharingStatsChanged"
object:self];
}
@end
@implementation LocalCamera (DelegateMethods)
- (void)cameraControllerConnected:(CameraController *)controller
{
[super cameraControllerConnected:controller];
if (shareOnConnect) {
[self setSharingEnabled:YES];
shareOnConnect = NO;
}
}
- (void)cameraControllerDisconnected:(CameraController *)controller
{
[super cameraControllerDisconnected:controller];
if (shareOnConnect) {
shareOnConnect = NO;
}
}
@end
@implementation LocalCamera (PrivateMethods)
- (BOOL)startSharing
{
BOOL wasSuccessful = NO;
int port = [[sharePreferences objectForKey:WNKSharePortKey] intValue];
NSString *desc = [sharePreferences objectForKey:WNKShareDescriptionKey];
BOOL enableBonjour = [[sharePreferences objectForKey:WNKShareBonjourKey] boolValue];
int interval = [[sharePreferences objectForKey:WNKShareFrequencyKey] intValue];
int userLimit = [[sharePreferences objectForKey:WNKShareLimitNumKey] intValue];
BOOL limitUsers = [[sharePreferences objectForKey:WNKShareLimitKey] boolValue];
BOOL requirePassword = [[sharePreferences objectForKey:WNKShareUsePasswordKey] boolValue];
NSString *password = [sharePreferences objectForKey:WNKSharePasswordKey];
NSLog(@"Starting to share with description: %@\nport: %d interval: %d",
desc, port, interval);
//
// Create Image Server
imageServer = [[ImageServer alloc] initWithPortNumber:port
description:desc
delegate:self];
if ([imageServer isValid]) {
[imageServer setLimitUsers:limitUsers];
[imageServer setUserLimit:userLimit];
[imageServer setBonjourEnabled:enableBonjour];
[imageServer setRequirePassword:requirePassword];
[imageServer setPassword:password];
wasSuccessful = YES;
//
// Start timer for sharing
shareTimer = [[NSTimer scheduledTimerWithTimeInterval:interval
target:self
selector:@selector(shareTimerFired:)
userInfo:nil
repeats:YES] retain];
[imageServer setSharedImage:[camController recentImage]];
}
else {
[imageServer release];
imageServer = nil;
NSLog(@"Unable to initialize Image Server");
NSString *errString =
[NSString stringWithFormat:@"Could not share %@ on port %d",
desc, port];
NSRunAlertPanel(errString,
@"Is something else using this port?",
@"OK", nil, nil);
}
return wasSuccessful;
}
- (void)stopSharing
{
[shareTimer invalidate];
[shareTimer release];
shareTimer = nil;
[imageServer release];
imageServer = nil;
NSString *newSubDesc = [NSString stringWithFormat:@"My %@", cameraName];
[camController setSourceSubDescription:newSubDesc];
}
- (void)shareTimerFired:(NSTimer *)timer
{
[imageServer setSharedImage:[camController recentImage]];
}
- (NSString *)connectMessage
{
return [NSString stringWithFormat:@"Connecting to %@...",
[self cameraName]];
}
- (NSString *)connectErrorMessage
{
return [NSString stringWithFormat:@"Could not enable your %@! Ensure it's not already in use.",
[self cameraName]];
}
- (NSString *)connectSuccessMessage
{
return [NSString stringWithFormat:@"%@ Enabled",
[self cameraName]];
}
//
// Record panel handlers
//
- (void)recordDidEnd:(NSSavePanel *)sheet
returnCode:(int)code
contextInfo:(void *)contextInfo
{
[super recordDidEnd:sheet
returnCode:code
contextInfo:contextInfo];
if ([camController isRecording]) {
double interval = [saveFrameInterval floatValue];
[(LocalCameraController *)camController captureFrameAtInterval:interval];
}
}
- (void)windowDidLoad
{
[[self window] setFrameAutosaveName:@"LocalCamera"];
}
- (void)windowDidMove:(NSNotification *)note
{
[[self window] saveFrameUsingName:@"LocalCamera"];
[super windowDidMove:note];
}
@end