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