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