-
Notifications
You must be signed in to change notification settings - Fork 43
/
ECVConfigController.m
executable file
·231 lines (198 loc) · 8.39 KB
/
ECVConfigController.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
231
/* Copyright (c) 2009, Ben Trask
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY BEN TRASK ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL BEN TRASK BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
#import "ECVConfigController.h"
// Models
#import "ECVCaptureDocument.h"
#import "ECVCaptureDevice.h"
#import "ECVVideoFormat.h"
#import "ECVDeinterlacingMode.h"
#import "ECVAudioTarget.h"
// Other Sources
#if defined(ECV_ENABLE_AUDIO)
#import "ECVAudioDevice.h"
#endif
#import "ECVFoundationAdditions.h"
@interface ECVConfigController(Private)
- (void)_snapSlider:(NSSlider *)slider;
@end
@implementation ECVConfigController
#pragma mark +ECVConfigController
+ (id)sharedConfigController
{
static ECVConfigController *c;
if(!c) c = [[self alloc] init];
return [[c retain] autorelease];
}
#pragma mark -ECVConfigController
- (IBAction)changeSource:(id)sender
{
[[_captureDocument videoDevice] setVideoSource:[[sender selectedItem] representedObject]];
}
- (IBAction)changeFormat:(id)sender
{
[[_captureDocument videoDevice] setVideoFormat:[[sender selectedItem] representedObject]];
}
- (IBAction)changeDeinterlacing:(id)sender
{
[[_captureDocument videoDevice] setDeinterlacingMode:[ECVDeinterlacingMode deinterlacingModeWithType:[sender selectedTag]]];
}
- (IBAction)changeBrightness:(id)sender
{
[self _snapSlider:sender];
[[_captureDocument videoDevice] setBrightness:[sender doubleValue]];
}
- (IBAction)changeContrast:(id)sender
{
[self _snapSlider:sender];
[[_captureDocument videoDevice] setContrast:[sender doubleValue]];
}
- (IBAction)changeSaturation:(id)sender
{
[self _snapSlider:sender];
[[_captureDocument videoDevice] setSaturation:[sender doubleValue]];
}
- (IBAction)changeHue:(id)sender
{
[self _snapSlider:sender];
[[_captureDocument videoDevice] setHue:[sender doubleValue]];
}
#pragma mark -
- (IBAction)changeAudioInput:(id)sender
{
[_captureDocument setAudioDevice:[[sender selectedItem] representedObject]];
}
- (IBAction)changeUpconvertsFromMono:(id)sender
{
[[_captureDocument audioTarget] setUpconvertsFromMono:NSOnState == [sender state]];
}
- (IBAction)changeVolume:(id)sender
{
[[_captureDocument audioTarget] setVolume:[sender doubleValue]];
[[_captureDocument audioTarget] setMuted:NO];
}
#pragma mark -
- (ECVCaptureDocument *)captureDocument
{
return _captureDocument;
}
- (void)setCaptureDocument:(ECVCaptureDocument *const)c
{
[_captureDocument ECV_removeObserver:self name:ECVCaptureDeviceVolumeDidChangeNotification];
_captureDocument = c;
[_captureDocument ECV_addObserver:self selector:@selector(volumeDidChange:) name:ECVCaptureDeviceVolumeDidChangeNotification];
[self volumeDidChange:nil];
if(![self isWindowLoaded]) return;
ECVCaptureDevice *const captureDevice = [_captureDocument videoDevice];
[sourcePopUp removeAllItems];
for(ECVVideoSource *const source in [captureDevice supportedVideoSources]) {
NSMenuItem *const item = [[[NSMenuItem alloc] initWithTitle:[source localizedName] action:NULL keyEquivalent:@""] autorelease];
[item setRepresentedObject:source];
[[sourcePopUp menu] addItem:item];
}
[sourcePopUp setEnabled:[[sourcePopUp menu] numberOfItems] > 0];
if([sourcePopUp isEnabled]) [sourcePopUp selectItemAtIndex:[sourcePopUp indexOfItemWithRepresentedObject:[captureDevice videoSource]]];
NSSet *const formats = [captureDevice supportedVideoFormats];
[formatPopUp setMenu:[ECVVideoFormat menuWithVideoFormats:formats]];
[formatPopUp setEnabled:[formats count] > 0];
if([formatPopUp isEnabled]) [formatPopUp selectItemAtIndex:[formatPopUp indexOfItemWithRepresentedObject:[captureDevice videoFormat]]];
[deinterlacePopUp selectItemWithTag:[[captureDevice deinterlacingMode] deinterlacingModeType]];
[deinterlacePopUp setEnabled:!!captureDevice];
[brightnessSlider setEnabled:[captureDevice respondsToSelector:@selector(brightness)]];
[contrastSlider setEnabled:[captureDevice respondsToSelector:@selector(contrast)]];
[saturationSlider setEnabled:[captureDevice respondsToSelector:@selector(saturation)]];
[hueSlider setEnabled:[captureDevice respondsToSelector:@selector(hue)]];
[brightnessSlider setDoubleValue:[brightnessSlider isEnabled] ? [captureDevice brightness] : 0.5f];
[contrastSlider setDoubleValue:[contrastSlider isEnabled] ? [captureDevice contrast] : 0.5f];
[saturationSlider setDoubleValue:[saturationSlider isEnabled] ? [captureDevice saturation] : 0.5f];
[hueSlider setDoubleValue:[hueSlider isEnabled] ? [captureDevice hue] : 0.5f];
[self _snapSlider:brightnessSlider];
[self _snapSlider:contrastSlider];
[self _snapSlider:saturationSlider];
[self _snapSlider:hueSlider];
ECVAudioTarget *const audioTarget = [_captureDocument audioTarget];
[upconvertsFromMonoSwitch setEnabled:!!audioTarget];
[upconvertsFromMonoSwitch setState:audioTarget && [audioTarget upconvertsFromMono]];
[self audioHardwareDevicesDidChange:nil];
[audioSourcePopUp setEnabled:!!_captureDocument];
}
#pragma mark -
- (void)audioHardwareDevicesDidChange:(NSNotification *)aNotif
{
[audioSourcePopUp removeAllItems];
NSMenuItem *const nilItem = [[[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"No Input", nil) action:NULL keyEquivalent:@""] autorelease];
[[audioSourcePopUp menu] addItem:nilItem];
ECVAudioInput *const preferredInput = [[_captureDocument videoDevice] builtInAudioInput];
if(preferredInput) {
NSMenuItem *const item = [[[NSMenuItem alloc] initWithTitle:[preferredInput name] action:NULL keyEquivalent:@""] autorelease];
[item setRepresentedObject:preferredInput];
[[audioSourcePopUp menu] addItem:item];
}
NSMenuItem *const separator = [NSMenuItem separatorItem];
[[audioSourcePopUp menu] addItem:separator];
BOOL hasAdditionalItems = NO;
for(ECVAudioInput *const input in [ECVAudioInput allDevices]) {
if(BTEqualObjects(input, preferredInput)) continue;
NSMenuItem *const item = [[[NSMenuItem alloc] initWithTitle:[input name] action:NULL keyEquivalent:@""] autorelease];
[item setRepresentedObject:input];
[[audioSourcePopUp menu] addItem:item];
hasAdditionalItems = YES;
}
if(!hasAdditionalItems) [[audioSourcePopUp menu] removeItem:separator];
ECVAudioInput *const input = [_captureDocument audioDevice];
[audioSourcePopUp selectItemAtIndex:input ? [audioSourcePopUp indexOfItemWithRepresentedObject:input] : 0];
}
- (void)volumeDidChange:(NSNotification *)aNotif
{
if(![self isWindowLoaded]) return;
ECVAudioTarget *const audioTarget = [_captureDocument audioTarget];
[volumeSlider setEnabled:!!audioTarget];
if(audioTarget) [volumeSlider setDoubleValue:[audioTarget isMuted] ? 0.0f : [audioTarget volume]];
else [volumeSlider setDoubleValue:0.5f];
}
#pragma mark -ECVConfigController(Private)
- (void)_snapSlider:(NSSlider *)slider
{
if(ABS([slider doubleValue] - 0.5f) < 0.03f) [slider setDoubleValue:0.5f];
}
#pragma mark -NSWindowController
- (void)windowDidLoad
{
[super windowDidLoad];
NSPanel *const w = (NSPanel *)[self window];
[w setBecomesKeyOnlyIfNeeded:YES];
[w setCollectionBehavior:NSWindowCollectionBehaviorFullScreenAuxiliary];
[[ECVAudioDevice class] ECV_addObserver:self selector:@selector(audioHardwareDevicesDidChange:) name:ECVAudioHardwareDevicesDidChangeNotification];
[self setCaptureDocument:_captureDocument];
}
- (NSString *)windowFrameAutosaveName
{
return NSStringFromClass([self class]);
}
#pragma mark -NSObject
- (id)init
{
return [super initWithWindowNibName:@"ECVConfig"];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
@end