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#include "device_info_objc.h"
16#include "rtc_video_capture_objc.h"
17#include "webrtc/rtc_base/refcount.h"
18#include "webrtc/rtc_base/refcountedobject.h"
19#include "webrtc/rtc_base/scoped_ref_ptr.h"
20
21using namespace webrtc;
22using namespace videocapturemodule;
23
24rtc::scoped_refptr<VideoCaptureModule> VideoCaptureImpl::Create(const char* deviceUniqueIdUTF8) {
25  return VideoCaptureIos::Create(deviceUniqueIdUTF8);
26}
27
28VideoCaptureIos::VideoCaptureIos() : is_capturing_(false) {
29  capability_.width = kDefaultWidth;
30  capability_.height = kDefaultHeight;
31  capability_.maxFPS = kDefaultFrameRate;
32  capture_device_ = nil;
33}
34
35VideoCaptureIos::~VideoCaptureIos() {
36  if (is_capturing_) {
37    [capture_device_ stopCapture];
38    capture_device_ = nil;
39  }
40}
41
42rtc::scoped_refptr<VideoCaptureModule> VideoCaptureIos::Create(const char* deviceUniqueIdUTF8) {
43  if (!deviceUniqueIdUTF8[0]) {
44    return NULL;
45  }
46
47  rtc::scoped_refptr<VideoCaptureIos> capture_module(new rtc::RefCountedObject<VideoCaptureIos>());
48
49  const int32_t name_length = strlen(deviceUniqueIdUTF8);
50  if (name_length >= kVideoCaptureUniqueNameSize) return nullptr;
51
52  capture_module->_deviceUniqueId = new char[name_length + 1];
53  strncpy(capture_module->_deviceUniqueId, deviceUniqueIdUTF8, name_length + 1);
54  capture_module->_deviceUniqueId[name_length] = '\0';
55
56  capture_module->capture_device_ = [[RTCVideoCaptureIosObjC alloc] initWithOwner:capture_module];
57  if (!capture_module->capture_device_) {
58    return nullptr;
59  }
60
61  if (![capture_module->capture_device_
62          setCaptureDeviceByUniqueId:[[NSString alloc] initWithCString:deviceUniqueIdUTF8
63                                                              encoding:NSUTF8StringEncoding]]) {
64    return nullptr;
65  }
66  return capture_module;
67}
68
69int32_t VideoCaptureIos::StartCapture(const VideoCaptureCapability& capability) {
70  capability_ = capability;
71
72  if (![capture_device_ startCaptureWithCapability:capability]) {
73    return -1;
74  }
75
76  is_capturing_ = true;
77
78  return 0;
79}
80
81int32_t VideoCaptureIos::StopCapture() {
82  if (![capture_device_ stopCapture]) {
83    return -1;
84  }
85
86  is_capturing_ = false;
87  return 0;
88}
89
90bool VideoCaptureIos::CaptureStarted() { return is_capturing_; }
91
92int32_t VideoCaptureIos::CaptureSettings(VideoCaptureCapability& settings) {
93  settings = capability_;
94  settings.videoType = VideoType::kNV12;
95  return 0;
96}
97