1/*
2 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#pragma mark **** imports/includes
12
13#import "webrtc/modules/video_capture/mac/qtkit/video_capture_qtkit_info_objc.h"
14
15#include "webrtc/system_wrappers/include/trace.h"
16
17using namespace webrtc;
18
19#pragma mark **** hidden class interface
20
21@implementation VideoCaptureMacQTKitInfoObjC
22
23// ****************** over-written OS methods ***********************
24#pragma mark **** over-written OS methods
25
26/// ***** Objective-C. Similar to C++ constructor, although invoked manually
27/// ***** Potentially returns an instance of self
28-(id)init{
29    self = [super init];
30    if(nil != self){
31        [self checkOSSupported];
32        [self initializeVariables];
33    }
34    else
35    {
36        return nil;
37    }
38    return self;
39}
40
41/// ***** Objective-C. Similar to C++ destructor
42/// ***** Returns nothing
43- (void)dealloc {
44
45    [_captureDevicesInfo release];
46
47    [super dealloc];
48}
49
50// ****************** public methods ******************
51#pragma mark **** public method implementations
52
53/// ***** Creates a message box with Cocoa framework
54/// ***** Returns 0 on success, -1 otherwise.
55- (NSNumber*)displayCaptureSettingsDialogBoxWithDevice:(const char*)deviceUniqueIdUTF8
56                    AndTitle:(const char*)dialogTitleUTF8
57                    AndParentWindow:(void*) parentWindow
58                    AtX:(uint32_t)positionX
59                    AndY:(uint32_t) positionY
60{
61    NSString* strTitle = [NSString stringWithFormat:@"%s", dialogTitleUTF8];
62    NSString* strButton = @"Alright";
63    NSAlert* alert = [NSAlert alertWithMessageText:strTitle
64                      defaultButton:strButton
65                      alternateButton:nil otherButton:nil
66                      informativeTextWithFormat:@"Device %s is capturing", deviceUniqueIdUTF8];
67    [alert setAlertStyle:NSInformationalAlertStyle];
68    [alert runModal];
69    return [NSNumber numberWithInt:0];
70}
71
72- (NSNumber*)getCaptureDeviceCount{
73    [self getCaptureDevices];
74    return [NSNumber numberWithInt:_captureDeviceCountInfo];
75}
76
77
78- (NSNumber*)getDeviceNamesFromIndex:(uint32_t)index
79    DefaultName:(char*)deviceName
80    WithLength:(uint32_t)deviceNameLength
81    AndUniqueID:(char*)deviceUniqueID
82    WithLength:(uint32_t)deviceUniqueIDLength
83    AndProductID:(char*)deviceProductID
84    WithLength:(uint32_t)deviceProductIDLength
85{
86    if(NO == _OSSupportedInfo)
87    {
88        return [NSNumber numberWithInt:0];
89    }
90
91    if(index >= (uint32_t)_captureDeviceCountInfo)
92    {
93        return [NSNumber numberWithInt:-1];
94    }
95
96    QTCaptureDevice* tempCaptureDevice =
97        (QTCaptureDevice*)[_captureDevicesInfo objectAtIndex:index];
98    if(!tempCaptureDevice)
99    {
100        return [NSNumber numberWithInt:-1];
101    }
102
103    memset(deviceName, 0, deviceNameLength);
104    memset(deviceUniqueID, 0, deviceUniqueIDLength);
105
106    bool successful = NO;
107
108    NSString* tempString = [tempCaptureDevice localizedDisplayName];
109    successful = [tempString getCString:(char*)deviceName
110                  maxLength:deviceNameLength encoding:NSUTF8StringEncoding];
111    if(NO == successful)
112    {
113        memset(deviceName, 0, deviceNameLength);
114        return [NSNumber numberWithInt:-1];
115    }
116
117    tempString = [tempCaptureDevice uniqueID];
118    successful = [tempString getCString:(char*)deviceUniqueID
119                  maxLength:deviceUniqueIDLength encoding:NSUTF8StringEncoding];
120    if(NO == successful)
121    {
122        memset(deviceUniqueID, 0, deviceNameLength);
123        return [NSNumber numberWithInt:-1];
124    }
125
126    return [NSNumber numberWithInt:0];
127
128}
129
130// ****************** "private" category functions below here  ******************
131#pragma mark **** "private" method implementations
132
133- (NSNumber*)initializeVariables
134{
135    if(NO == _OSSupportedInfo)
136    {
137        return [NSNumber numberWithInt:0];
138    }
139
140    _poolInfo = [[NSAutoreleasePool alloc]init];
141    _captureDeviceCountInfo = 0;
142    [self getCaptureDevices];
143
144    return [NSNumber numberWithInt:0];
145}
146
147// ***** Checks to see if the QTCaptureSession framework is available in the OS
148// ***** If it is not, isOSSupprted = NO
149// ***** Throughout the rest of the class isOSSupprted is checked and functions
150// ***** are/aren't called depending
151// ***** The user can use weak linking to the QTKit framework and run on older
152// ***** versions of the OS
153// ***** I.E. Backwards compaitibility
154// ***** Returns nothing. Sets member variable
155- (void)checkOSSupported
156{
157    Class osSupportedTest = NSClassFromString(@"QTCaptureSession");
158    if(nil == osSupportedTest)
159    {
160      _OSSupportedInfo = NO;
161    }
162    else
163    {
164      _OSSupportedInfo = YES;
165    }
166}
167
168/// ***** Retrieves the number of capture devices currently available
169/// ***** Stores them in an NSArray instance
170/// ***** Returns 0 on success, -1 otherwise.
171- (NSNumber*)getCaptureDevices
172{
173    if(NO == _OSSupportedInfo)
174    {
175        return [NSNumber numberWithInt:0];
176    }
177
178    if(_captureDevicesInfo)
179    {
180        [_captureDevicesInfo release];
181    }
182    _captureDevicesInfo = [[NSArray alloc]
183                            initWithArray:[QTCaptureDevice
184                                           inputDevicesWithMediaType:QTMediaTypeVideo]];
185
186    _captureDeviceCountInfo = _captureDevicesInfo.count;
187
188    return [NSNumber numberWithInt:0];
189}
190
191@end
192