blob: 6845fe08e2512d8096141d5dcf025b983348b9b5 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2010 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28// support GCC compiler
29#ifndef __has_feature
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000030#define __has_feature(x) 0
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031#endif
32
kjellandera96e2d72016-02-04 23:52:28 -080033#include "webrtc/media/devices/devicemanager.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034
35#import <assert.h>
buildbot@webrtc.orga42a3ad2014-09-13 01:09:18 +000036#ifdef __MAC_OS_X_VERSION_MAX_ALLOWED
37#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
38 #import <AVFoundation/AVFoundation.h>
39#endif
40#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041#import <QTKit/QTKit.h>
42
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000043#include "webrtc/base/logging.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
45@interface DeviceWatcherImpl : NSObject {
46 @private
47 cricket::DeviceManagerInterface* manager_;
48}
49- (id)init:(cricket::DeviceManagerInterface*)manager;
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000050- (void)onDevicesChanged:(NSNotification*)notification;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051@end
52
53@implementation DeviceWatcherImpl
54- (id)init:(cricket::DeviceManagerInterface*)manager {
55 if ((self = [super init])) {
56 assert(manager != NULL);
57 manager_ = manager;
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000058 [[NSNotificationCenter defaultCenter]
59 addObserver:self
60 selector:@selector(onDevicesChanged:)
61 name:QTCaptureDeviceWasConnectedNotification
62 object:nil];
63 [[NSNotificationCenter defaultCenter]
64 addObserver:self
65 selector:@selector(onDevicesChanged:)
66 name:QTCaptureDeviceWasDisconnectedNotification
67 object:nil];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 }
69 return self;
70}
71
72- (void)dealloc {
73 [[NSNotificationCenter defaultCenter] removeObserver:self];
74#if !__has_feature(objc_arc)
75 [super dealloc];
76#endif
77}
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000078- (void)onDevicesChanged:(NSNotification*)notification {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 manager_->SignalDevicesChange();
80}
81@end
82
83namespace cricket {
84
85DeviceWatcherImpl* CreateDeviceWatcherCallback(
86 DeviceManagerInterface* manager) {
87 DeviceWatcherImpl* impl;
88#if !__has_feature(objc_arc)
89 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
90#else
91 @autoreleasepool
92#endif
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000093 { impl = [[DeviceWatcherImpl alloc] init:manager]; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094#if !__has_feature(objc_arc)
95 [pool drain];
96#endif
97 return impl;
98}
99
100void ReleaseDeviceWatcherCallback(DeviceWatcherImpl* watcher) {
101#if !__has_feature(objc_arc)
102 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
103 [watcher release];
104 [pool drain];
105#endif
106}
107
108bool GetQTKitVideoDevices(std::vector<Device>* devices) {
109#if !__has_feature(objc_arc)
110 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
111#else
112 @autoreleasepool
113#endif
114 {
115 NSArray* qt_capture_devices =
116 [QTCaptureDevice inputDevicesWithMediaType:QTMediaTypeVideo];
117 NSUInteger count = [qt_capture_devices count];
118 LOG(LS_INFO) << count << " capture device(s) found:";
119 for (QTCaptureDevice* qt_capture_device in qt_capture_devices) {
120 static NSString* const kFormat = @"localizedDisplayName: \"%@\", "
121 @"modelUniqueID: \"%@\", uniqueID \"%@\", isConnected: %d, "
122 @"isOpen: %d, isInUseByAnotherApplication: %d";
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000123 NSString* info = [NSString
124 stringWithFormat:kFormat,
125 [qt_capture_device localizedDisplayName],
126 [qt_capture_device modelUniqueID],
127 [qt_capture_device uniqueID],
128 [qt_capture_device isConnected],
129 [qt_capture_device isOpen],
130 [qt_capture_device isInUseByAnotherApplication]];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 LOG(LS_INFO) << [info UTF8String];
132
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000133 std::string name([[qt_capture_device localizedDisplayName] UTF8String]);
134 devices->push_back(
135 Device(name, [[qt_capture_device uniqueID] UTF8String]));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 }
137 }
138#if !__has_feature(objc_arc)
139 [pool drain];
140#endif
141 return true;
142}
143
buildbot@webrtc.orga42a3ad2014-09-13 01:09:18 +0000144bool GetAVFoundationVideoDevices(std::vector<Device>* devices) {
145#ifdef __MAC_OS_X_VERSION_MAX_ALLOWED
146#if __MAC_OS_X_VERSION_MAX_ALLOWED >=1070
147 if (![AVCaptureDevice class]) {
148 // Fallback to using QTKit if AVFoundation is not available
149 return GetQTKitVideoDevices(devices);
150 }
151#if !__has_feature(objc_arc)
152 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
153#else
154 @autoreleasepool
155#endif
156 {
157 NSArray* capture_devices = [AVCaptureDevice devices];
158 LOG(LS_INFO) << [capture_devices count] << " capture device(s) found:";
159 for (AVCaptureDevice* capture_device in capture_devices) {
160 if ([capture_device hasMediaType:AVMediaTypeVideo] ||
161 [capture_device hasMediaType:AVMediaTypeMuxed]) {
162 static NSString* const kFormat = @"localizedName: \"%@\", "
163 @"modelID: \"%@\", uniqueID \"%@\", isConnected: %d, "
164 @"isInUseByAnotherApplication: %d";
165 NSString* info = [NSString
166 stringWithFormat:kFormat,
167 [capture_device localizedName],
168 [capture_device modelID],
169 [capture_device uniqueID],
170 [capture_device isConnected],
171 [capture_device isInUseByAnotherApplication]];
172 LOG(LS_INFO) << [info UTF8String];
173
174 std::string name([[capture_device localizedName] UTF8String]);
175 devices->push_back(
176 Device(name, [[capture_device uniqueID] UTF8String]));
177 }
178 }
179 }
180#if !__has_feature(objc_arc)
181 [pool drain];
182#endif
183 return true;
184#else // __MAC_OS_X_VERSION_MAX_ALLOWED >=1070
185 return GetQTKitVideoDevices(devices);
186#endif // __MAC_OS_X_VERSION_MAX_ALLOWED >=1070
187#else // __MAC_OS_X_VERSION_MAX_ALLOWED
188 return GetQTKitVideoDevices(devices);
189#endif // __MAC_OS_X_VERSION_MAX_ALLOWED
190}
191
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192} // namespace cricket