summaryrefslogtreecommitdiffstats
path: root/dom/media/systemservices/objc_video_capture/device_info_objc.mm
blob: 6e9435daff6661df328c884a4e9bf76bde1f5d4f (plain)
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
/*
 *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#if !defined(__has_feature) || !__has_feature(objc_arc)
#  error "This file requires ARC support."
#endif

#import <AVFoundation/AVFoundation.h>

#import "device_info_objc.h"

@implementation DeviceInfoIosObjC

- (id)init {
  self = [super init];
  if (nil != self) {
    _lock = [[NSLock alloc] init];
  }
  return self;
}

- (void)dealloc {
}

- (void)registerOwner:(webrtc::VideoCaptureModule::DeviceInfo*)owner {
  [_lock lock];
  if (!_owner && owner) {
    [self configureObservers];
  } else if (_owner && !owner) {
    NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
    for (id observer in _observers) {
      [notificationCenter removeObserver:observer];
    }
    _observers = nil;
  }
  _owner = owner;
  [_lock unlock];
}

+ (int)captureDeviceCount {
  int cnt = 0;
  @try {
    for (AVCaptureDevice* device in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
      if ([device isSuspended]) {
        continue;
      }
      cnt++;
    }
  } @catch (NSException* exception) {
    cnt = 0;
  }
  return cnt;
}

+ (AVCaptureDevice*)captureDeviceForIndex:(int)index {
  int cnt = 0;
  @try {
    for (AVCaptureDevice* device in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
      if ([device isSuspended]) {
        continue;
      }
      if (cnt == index) {
        return device;
      }
      cnt++;
    }
  } @catch (NSException* exception) {
    cnt = 0;
  }

  return nil;
}

+ (AVCaptureDevice*)captureDeviceForUniqueId:(NSString*)uniqueId {
  for (AVCaptureDevice* device in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
    if ([device isSuspended]) {
      continue;
    }
    if ([uniqueId isEqual:device.uniqueID]) {
      return device;
    }
  }

  return nil;
}

+ (NSString*)deviceNameForIndex:(int)index {
  return [DeviceInfoIosObjC captureDeviceForIndex:index].localizedName;
}

+ (NSString*)deviceUniqueIdForIndex:(int)index {
  return [DeviceInfoIosObjC captureDeviceForIndex:index].uniqueID;
}

+ (NSString*)deviceNameForUniqueId:(NSString*)uniqueId {
  return [[AVCaptureDevice deviceWithUniqueID:uniqueId] localizedName];
}

+ (webrtc::VideoCaptureCapability)capabilityForPreset:(NSString*)preset {
  webrtc::VideoCaptureCapability capability;

  // TODO(tkchin): Maybe query AVCaptureDevice for supported formats, and
  // then get the dimensions / frame rate from each supported format
  if ([preset isEqualToString:AVCaptureSessionPreset352x288]) {
    capability.width = 352;
    capability.height = 288;
    capability.maxFPS = 30;
    capability.videoType = webrtc::VideoType::kNV12;
    capability.interlaced = false;
  } else if ([preset isEqualToString:AVCaptureSessionPreset640x480]) {
    capability.width = 640;
    capability.height = 480;
    capability.maxFPS = 30;
    capability.videoType = webrtc::VideoType::kNV12;
    capability.interlaced = false;
  } else if ([preset isEqualToString:AVCaptureSessionPreset1280x720]) {
    capability.width = 1280;
    capability.height = 720;
    capability.maxFPS = 30;
    capability.videoType = webrtc::VideoType::kNV12;
    capability.interlaced = false;
  }

  return capability;
}

- (void)configureObservers {
  // register device connected / disconnected event
  NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];

  id deviceWasConnectedObserver =
      [notificationCenter addObserverForName:AVCaptureDeviceWasConnectedNotification
                                      object:nil
                                       queue:[NSOperationQueue mainQueue]
                                  usingBlock:^(NSNotification* note) {
                                    [_lock lock];
                                    AVCaptureDevice* device = [note object];
                                    BOOL isVideoDevice = [device hasMediaType:AVMediaTypeVideo];
                                    if (isVideoDevice && _owner) _owner->DeviceChange();
                                    [_lock unlock];
                                  }];

  id deviceWasDisconnectedObserver =
      [notificationCenter addObserverForName:AVCaptureDeviceWasDisconnectedNotification
                                      object:nil
                                       queue:[NSOperationQueue mainQueue]
                                  usingBlock:^(NSNotification* note) {
                                    [_lock lock];
                                    AVCaptureDevice* device = [note object];
                                    BOOL isVideoDevice = [device hasMediaType:AVMediaTypeVideo];
                                    if (isVideoDevice && _owner) _owner->DeviceChange();
                                    [_lock unlock];
                                  }];

  _observers = [[NSArray alloc]
      initWithObjects:deviceWasConnectedObserver, deviceWasDisconnectedObserver, nil];
}

@end