1/*
2 *  Copyright (c) 2013 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#if !defined(__has_feature) || !__has_feature(objc_arc)
12#error "This file requires ARC support."
13#endif
14
15#import <AVFoundation/AVFoundation.h>
16
17#import "webrtc/modules/video_capture/ios/device_info_ios_objc.h"
18#include "webrtc/modules/video_capture/video_capture_config.h"
19
20@implementation DeviceInfoIosObjC
21
22+ (int)captureDeviceCount {
23  return [[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo] count];
24}
25
26+ (AVCaptureDevice*)captureDeviceForIndex:(int)index {
27  return [[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]
28      objectAtIndex:index];
29}
30
31+ (AVCaptureDevice*)captureDeviceForUniqueId:(NSString*)uniqueId {
32  for (AVCaptureDevice* device in
33       [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
34    if ([uniqueId isEqual:device.uniqueID]) {
35      return device;
36    }
37  }
38
39  return nil;
40}
41
42+ (NSString*)deviceNameForIndex:(int)index {
43  return [DeviceInfoIosObjC captureDeviceForIndex:index].localizedName;
44}
45
46+ (NSString*)deviceUniqueIdForIndex:(int)index {
47  return [DeviceInfoIosObjC captureDeviceForIndex:index].uniqueID;
48}
49
50+ (NSString*)deviceNameForUniqueId:(NSString*)uniqueId {
51  return [[AVCaptureDevice deviceWithUniqueID:uniqueId] localizedName];
52}
53
54+ (webrtc::VideoCaptureCapability)capabilityForPreset:(NSString*)preset {
55  webrtc::VideoCaptureCapability capability;
56
57  // TODO(tkchin): Maybe query AVCaptureDevice for supported formats, and
58  // then get the dimensions / frame rate from each supported format
59  if ([preset isEqualToString:AVCaptureSessionPreset352x288]) {
60    capability.width = 352;
61    capability.height = 288;
62    capability.maxFPS = 30;
63    capability.expectedCaptureDelay =
64        webrtc::videocapturemodule::kDefaultCaptureDelay;
65    capability.rawType = webrtc::kVideoNV12;
66    capability.codecType = webrtc::kVideoCodecUnknown;
67    capability.interlaced = false;
68  } else if ([preset isEqualToString:AVCaptureSessionPreset640x480]) {
69    capability.width = 640;
70    capability.height = 480;
71    capability.maxFPS = 30;
72    capability.expectedCaptureDelay =
73        webrtc::videocapturemodule::kDefaultCaptureDelay;
74    capability.rawType = webrtc::kVideoNV12;
75    capability.codecType = webrtc::kVideoCodecUnknown;
76    capability.interlaced = false;
77  } else if ([preset isEqualToString:AVCaptureSessionPreset1280x720]) {
78    capability.width = 1280;
79    capability.height = 720;
80    capability.maxFPS = 30;
81    capability.expectedCaptureDelay =
82        webrtc::videocapturemodule::kDefaultCaptureDelay;
83    capability.rawType = webrtc::kVideoNV12;
84    capability.codecType = webrtc::kVideoCodecUnknown;
85    capability.interlaced = false;
86  } else if ([preset isEqualToString:AVCaptureSessionPreset1920x1080]) {
87    capability.width = 1920;
88    capability.height = 1080;
89    capability.maxFPS = 30;
90    capability.expectedCaptureDelay =
91        webrtc::videocapturemodule::kDefaultCaptureDelay;
92    capability.rawType = webrtc::kVideoNV12;
93    capability.codecType = webrtc::kVideoCodecUnknown;
94    capability.interlaced = false;
95  }
96
97  return capability;
98}
99
100@end
101