2014-04-28 14:58:08 -07:00
|
|
|
#import <AVFoundation/AVFoundation.h>
|
|
|
|
#import <CoreFoundation/CoreFoundation.h>
|
|
|
|
#import <CoreMedia/CoreMedia.h>
|
|
|
|
#import <CoreVideo/CoreVideo.h>
|
|
|
|
|
2014-07-09 22:12:57 -07:00
|
|
|
#include <obs-module.h>
|
2014-04-28 14:58:08 -07:00
|
|
|
#include <media-io/video-io.h>
|
|
|
|
|
2015-08-18 11:00:55 -07:00
|
|
|
#include <util/dstr.hpp>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <initializer_list>
|
|
|
|
#include <cinttypes>
|
|
|
|
#include <limits>
|
2015-08-11 11:12:37 -07:00
|
|
|
#include <memory>
|
2015-08-18 11:00:55 -07:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "scope-guard.hpp"
|
|
|
|
|
|
|
|
#define NBSP "\xC2\xA0"
|
2015-08-11 11:12:37 -07:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2015-08-18 11:00:55 -07:00
|
|
|
namespace std {
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct default_delete<obs_data_t> {
|
|
|
|
void operator()(obs_data_t *data)
|
|
|
|
{
|
|
|
|
obs_data_release(data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct default_delete<obs_data_item_t> {
|
|
|
|
void operator()(obs_data_item_t *item)
|
|
|
|
{
|
|
|
|
obs_data_item_release(&item);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-09 22:12:57 -07:00
|
|
|
#define TEXT_AVCAPTURE obs_module_text("AVCapture")
|
|
|
|
#define TEXT_DEVICE obs_module_text("Device")
|
|
|
|
#define TEXT_USE_PRESET obs_module_text("UsePreset")
|
|
|
|
#define TEXT_PRESET obs_module_text("Preset")
|
2015-08-18 11:00:55 -07:00
|
|
|
#define TEXT_RESOLUTION obs_module_text("Resolution")
|
|
|
|
#define TEXT_FRAME_RATE obs_module_text("FrameRate")
|
|
|
|
#define TEXT_MATCH_OBS obs_module_text("MatchOBS")
|
|
|
|
#define TEXT_INPUT_FORMAT obs_module_text("InputFormat")
|
|
|
|
#define TEXT_AUTO obs_module_text("Auto")
|
|
|
|
|
|
|
|
static const FourCharCode INPUT_FORMAT_AUTO = -1;
|
2014-07-09 22:12:57 -07:00
|
|
|
|
2014-05-05 11:31:24 -07:00
|
|
|
#define MILLI_TIMESCALE 1000
|
|
|
|
#define MICRO_TIMESCALE (MILLI_TIMESCALE * 1000)
|
|
|
|
#define NANO_TIMESCALE (MICRO_TIMESCALE * 1000)
|
|
|
|
|
2014-07-08 05:27:46 -07:00
|
|
|
#define AV_FOURCC_STR(code) \
|
|
|
|
(char[5]) { \
|
2015-08-11 11:12:37 -07:00
|
|
|
static_cast<char>((code >> 24) & 0xFF), \
|
|
|
|
static_cast<char>((code >> 16) & 0xFF), \
|
|
|
|
static_cast<char>((code >> 8) & 0xFF), \
|
|
|
|
static_cast<char>( code & 0xFF), \
|
|
|
|
0 \
|
2014-07-08 05:27:46 -07:00
|
|
|
}
|
|
|
|
|
2014-04-28 14:58:08 -07:00
|
|
|
struct av_capture;
|
|
|
|
|
2014-06-04 07:29:00 -07:00
|
|
|
#define AVLOG(level, format, ...) \
|
|
|
|
blog(level, "%s: " format, \
|
2014-08-04 08:41:15 -07:00
|
|
|
obs_source_get_name(capture->source), ##__VA_ARGS__)
|
2014-06-04 07:29:00 -07:00
|
|
|
|
2014-04-28 14:58:08 -07:00
|
|
|
@interface OBSAVCaptureDelegate :
|
|
|
|
NSObject<AVCaptureVideoDataOutputSampleBufferDelegate>
|
|
|
|
{
|
|
|
|
@public
|
|
|
|
struct av_capture *capture;
|
|
|
|
}
|
|
|
|
- (void)captureOutput:(AVCaptureOutput *)out
|
|
|
|
didDropSampleBuffer:(CMSampleBufferRef)sampleBuffer
|
|
|
|
fromConnection:(AVCaptureConnection *)connection;
|
|
|
|
- (void)captureOutput:(AVCaptureOutput *)captureOutput
|
|
|
|
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
|
|
|
|
fromConnection:(AVCaptureConnection *)connection;
|
|
|
|
@end
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
static auto remove_observer = [](id observer)
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:observer];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct observer_handle :
|
|
|
|
unique_ptr<remove_pointer<id>::type, decltype(remove_observer)> {
|
2014-04-28 14:58:08 -07:00
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
using base = unique_ptr<remove_pointer<id>::type,
|
|
|
|
decltype(remove_observer)>;
|
|
|
|
|
|
|
|
explicit observer_handle(id observer = nullptr)
|
|
|
|
: base(observer, remove_observer)
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
struct av_capture {
|
2014-04-28 14:58:08 -07:00
|
|
|
OBSAVCaptureDelegate *delegate;
|
|
|
|
dispatch_queue_t queue;
|
2014-05-05 11:31:24 -07:00
|
|
|
bool has_clock;
|
2014-04-28 14:58:08 -07:00
|
|
|
|
2015-08-18 11:00:55 -07:00
|
|
|
bool device_locked;
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
AVCaptureVideoDataOutput *out;
|
|
|
|
AVCaptureDevice *device;
|
|
|
|
AVCaptureDeviceInput *device_input;
|
|
|
|
AVCaptureSession *session;
|
|
|
|
|
2014-06-04 10:57:32 -07:00
|
|
|
NSString *uid;
|
2015-08-11 11:12:37 -07:00
|
|
|
observer_handle connect_observer;
|
|
|
|
observer_handle disconnect_observer;
|
2014-06-04 10:57:32 -07:00
|
|
|
|
2014-07-08 05:27:46 -07:00
|
|
|
FourCharCode fourcc;
|
2015-08-11 11:12:37 -07:00
|
|
|
video_format video_format;
|
|
|
|
video_colorspace colorspace;
|
|
|
|
video_range_type video_range;
|
2014-04-28 14:58:08 -07:00
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_source_t *source;
|
2014-04-28 14:58:08 -07:00
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
obs_source_frame frame;
|
2014-04-28 14:58:08 -07:00
|
|
|
};
|
|
|
|
|
2015-08-18 11:00:55 -07:00
|
|
|
static NSString *get_string(obs_data_t *data, char const *name)
|
|
|
|
{
|
|
|
|
return @(obs_data_get_string(data, name));
|
|
|
|
}
|
|
|
|
|
|
|
|
static AVCaptureDevice *get_device(obs_data_t *settings)
|
|
|
|
{
|
|
|
|
auto uid = get_string(settings, "device");
|
|
|
|
return [AVCaptureDevice deviceWithUniqueID:uid];
|
|
|
|
}
|
|
|
|
|
2015-08-18 11:00:55 -07:00
|
|
|
template <typename T, typename U>
|
|
|
|
static void clamp(T& store, U val, T low = numeric_limits<T>::min(),
|
|
|
|
T high = numeric_limits<T>::max())
|
|
|
|
{
|
|
|
|
store = static_cast<intmax_t>(val) < static_cast<intmax_t>(low) ? low :
|
|
|
|
(static_cast<intmax_t>(val) > static_cast<intmax_t>(high) ?
|
|
|
|
high : static_cast<T>(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool get_resolution(obs_data_t *settings, CMVideoDimensions &dims)
|
|
|
|
{
|
|
|
|
using item_ptr = unique_ptr<obs_data_item_t>;
|
|
|
|
item_ptr item{obs_data_item_byname(settings, "resolution")};
|
|
|
|
if (!item)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto res_str = obs_data_item_get_string(item.get());
|
|
|
|
unique_ptr<obs_data_t> res{obs_data_create_from_json(res_str)};
|
|
|
|
if (!res)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
item_ptr width{obs_data_item_byname(res.get(), "width")};
|
|
|
|
item_ptr height{obs_data_item_byname(res.get(), "height")};
|
|
|
|
|
|
|
|
if (!width || !height)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
clamp(dims.width, obs_data_item_get_int(width.get()), 0);
|
|
|
|
clamp(dims.height, obs_data_item_get_int(height.get()), 0);
|
|
|
|
|
|
|
|
if (!dims.width || !dims.height)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool get_input_format(obs_data_t *settings, FourCharCode &fourcc)
|
|
|
|
{
|
|
|
|
auto item = unique_ptr<obs_data_item_t>{
|
|
|
|
obs_data_item_byname(settings, "input_format")};
|
|
|
|
if (!item)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
fourcc = obs_data_item_get_int(item.get());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct config_helper {
|
|
|
|
obs_data_t *settings = nullptr;
|
|
|
|
|
|
|
|
AVCaptureDevice *dev_ = nullptr;
|
|
|
|
|
|
|
|
bool dims_valid : 1;
|
|
|
|
bool fr_valid : 1;
|
|
|
|
bool fps_valid : 1;
|
|
|
|
bool if_valid : 1;
|
|
|
|
|
|
|
|
CMVideoDimensions dims_{};
|
|
|
|
|
|
|
|
const char *frame_rate_ = nullptr;
|
|
|
|
media_frames_per_second fps_{};
|
|
|
|
|
|
|
|
FourCharCode input_format_ = INPUT_FORMAT_AUTO;
|
|
|
|
|
|
|
|
explicit config_helper(obs_data_t *settings)
|
|
|
|
: settings(settings)
|
|
|
|
{
|
|
|
|
dev_ = get_device(settings);
|
|
|
|
|
|
|
|
dims_valid = get_resolution(settings, dims_);
|
|
|
|
|
|
|
|
fr_valid = obs_data_get_frames_per_second(settings,
|
|
|
|
"frame_rate", nullptr, &frame_rate_);
|
|
|
|
fps_valid = obs_data_get_frames_per_second(settings,
|
|
|
|
"frame_rate", &fps_, nullptr);
|
|
|
|
|
|
|
|
if_valid = get_input_format(settings, input_format_);
|
|
|
|
}
|
|
|
|
|
|
|
|
AVCaptureDevice *dev() const
|
|
|
|
{
|
|
|
|
return dev_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CMVideoDimensions *dims() const
|
|
|
|
{
|
|
|
|
return dims_valid ? &dims_ : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *frame_rate() const
|
|
|
|
{
|
|
|
|
return fr_valid ? frame_rate_ : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const media_frames_per_second *fps() const
|
|
|
|
{
|
|
|
|
return fps_valid ? &fps_ : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const FourCharCode *input_format() const
|
|
|
|
{
|
|
|
|
return if_valid ? &input_format_ : nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
static inline video_format format_from_subtype(FourCharCode subtype)
|
2014-07-08 05:27:46 -07:00
|
|
|
{
|
|
|
|
//TODO: uncomment VIDEO_FORMAT_NV12 and VIDEO_FORMAT_ARGB once libobs
|
|
|
|
// gains matching GPU conversions or a CPU fallback is implemented
|
|
|
|
switch (subtype) {
|
|
|
|
case kCVPixelFormatType_422YpCbCr8:
|
|
|
|
return VIDEO_FORMAT_UYVY;
|
|
|
|
case kCVPixelFormatType_422YpCbCr8_yuvs:
|
|
|
|
return VIDEO_FORMAT_YUY2;
|
|
|
|
/*case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange:
|
|
|
|
case kCVPixelFormatType_420YpCbCr8BiPlanarFullRange:
|
|
|
|
return VIDEO_FORMAT_NV12;*/
|
|
|
|
/*case kCVPixelFormatType_32ARGB:
|
|
|
|
return VIDEO_FORMAT_ARGB;*/
|
|
|
|
case kCVPixelFormatType_32BGRA:
|
|
|
|
return VIDEO_FORMAT_BGRA;
|
|
|
|
default:
|
|
|
|
return VIDEO_FORMAT_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 11:00:55 -07:00
|
|
|
static const char *fourcc_subtype_name(FourCharCode fourcc);
|
|
|
|
|
|
|
|
static const char *format_description_subtype_name(CMFormatDescriptionRef desc,
|
|
|
|
FourCharCode *fourcc_=nullptr)
|
|
|
|
{
|
|
|
|
FourCharCode fourcc = CMFormatDescriptionGetMediaSubType(desc);
|
|
|
|
if (fourcc_)
|
|
|
|
*fourcc_ = fourcc;
|
|
|
|
|
|
|
|
return fourcc_subtype_name(fourcc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *fourcc_subtype_name(FourCharCode fourcc)
|
|
|
|
{
|
|
|
|
switch (fourcc) {
|
|
|
|
case kCVPixelFormatType_422YpCbCr8:
|
|
|
|
return "UYVY - 422YpCbCr8"; //VIDEO_FORMAT_UYVY;
|
|
|
|
case kCVPixelFormatType_422YpCbCr8_yuvs:
|
|
|
|
return "YUY2 - 422YpCbCr8_yuvs"; //VIDEO_FORMAT_YUY2;
|
|
|
|
case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange:
|
|
|
|
case kCVPixelFormatType_420YpCbCr8BiPlanarFullRange:
|
|
|
|
return "NV12 - 420YpCbCr8BiPlanar"; //VIDEO_FORMAT_NV12;
|
|
|
|
case kCVPixelFormatType_32ARGB:
|
|
|
|
return "ARGB - 32ARGB"; //VIDEO_FORMAT_ARGB;*/
|
|
|
|
case kCVPixelFormatType_32BGRA:
|
|
|
|
return "BGRA - 32BGRA"; //VIDEO_FORMAT_BGRA;
|
|
|
|
|
|
|
|
case kCMVideoCodecType_Animation: return "Apple Animation";
|
|
|
|
case kCMVideoCodecType_Cinepak: return "Cinepak";
|
|
|
|
case kCMVideoCodecType_JPEG: return "JPEG";
|
|
|
|
case kCMVideoCodecType_JPEG_OpenDML: return "MJPEG - JPEG OpenDML";
|
|
|
|
case kCMVideoCodecType_SorensonVideo: return "Sorenson Video";
|
|
|
|
case kCMVideoCodecType_SorensonVideo3: return "Sorenson Video 3";
|
|
|
|
case kCMVideoCodecType_H263: return "H.263";
|
|
|
|
case kCMVideoCodecType_H264: return "H.264";
|
|
|
|
case kCMVideoCodecType_MPEG4Video: return "MPEG-4";
|
|
|
|
case kCMVideoCodecType_MPEG2Video: return "MPEG-2";
|
|
|
|
case kCMVideoCodecType_MPEG1Video: return "MPEG-1";
|
|
|
|
|
|
|
|
case kCMVideoCodecType_DVCNTSC:
|
|
|
|
return "DV NTSC";
|
|
|
|
case kCMVideoCodecType_DVCPAL:
|
|
|
|
return "DV PAL";
|
|
|
|
case kCMVideoCodecType_DVCProPAL:
|
|
|
|
return "Panasonic DVCPro PAL";
|
|
|
|
case kCMVideoCodecType_DVCPro50NTSC:
|
|
|
|
return "Panasonic DVCPro-50 NTSC";
|
|
|
|
case kCMVideoCodecType_DVCPro50PAL:
|
|
|
|
return "Panasonic DVCPro-50 PAL";
|
|
|
|
case kCMVideoCodecType_DVCPROHD720p60:
|
|
|
|
return "Panasonic DVCPro-HD 720p60";
|
|
|
|
case kCMVideoCodecType_DVCPROHD720p50:
|
|
|
|
return "Panasonic DVCPro-HD 720p50";
|
|
|
|
case kCMVideoCodecType_DVCPROHD1080i60:
|
|
|
|
return "Panasonic DVCPro-HD 1080i60";
|
|
|
|
case kCMVideoCodecType_DVCPROHD1080i50:
|
|
|
|
return "Panasonic DVCPro-HD 1080i50";
|
|
|
|
case kCMVideoCodecType_DVCPROHD1080p30:
|
|
|
|
return "Panasonic DVCPro-HD 1080p30";
|
|
|
|
case kCMVideoCodecType_DVCPROHD1080p25:
|
|
|
|
return "Panasonic DVCPro-HD 1080p25";
|
|
|
|
|
|
|
|
case kCMVideoCodecType_AppleProRes4444:
|
|
|
|
return "Apple ProRes 4444";
|
|
|
|
case kCMVideoCodecType_AppleProRes422HQ:
|
|
|
|
return "Apple ProRes 422 HQ";
|
|
|
|
case kCMVideoCodecType_AppleProRes422:
|
|
|
|
return "Apple ProRes 422";
|
|
|
|
case kCMVideoCodecType_AppleProRes422LT:
|
|
|
|
return "Apple ProRes 422 LT";
|
|
|
|
case kCMVideoCodecType_AppleProRes422Proxy:
|
|
|
|
return "Apple ProRes 422 Proxy";
|
|
|
|
|
|
|
|
default:
|
|
|
|
blog(LOG_INFO, "Unknown format %s", AV_FOURCC_STR(fourcc));
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 05:27:46 -07:00
|
|
|
static inline bool is_fullrange_yuv(FourCharCode pixel_format)
|
|
|
|
{
|
|
|
|
switch (pixel_format) {
|
|
|
|
case kCVPixelFormatType_420YpCbCr8PlanarFullRange:
|
|
|
|
case kCVPixelFormatType_420YpCbCr8BiPlanarFullRange:
|
|
|
|
case kCVPixelFormatType_422YpCbCr8FullRange:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
static inline video_colorspace get_colorspace(CMFormatDescriptionRef desc)
|
2014-07-08 05:27:46 -07:00
|
|
|
{
|
|
|
|
CFPropertyListRef matrix = CMFormatDescriptionGetExtension(desc,
|
|
|
|
kCMFormatDescriptionExtension_YCbCrMatrix);
|
|
|
|
|
|
|
|
if (!matrix)
|
|
|
|
return VIDEO_CS_DEFAULT;
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
if (CFStringCompare(static_cast<CFStringRef>(matrix),
|
|
|
|
kCVImageBufferYCbCrMatrix_ITU_R_709_2, 0)
|
2014-07-08 05:27:46 -07:00
|
|
|
== kCFCompareEqualTo)
|
|
|
|
return VIDEO_CS_709;
|
|
|
|
|
|
|
|
return VIDEO_CS_601;
|
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
static inline bool update_colorspace(av_capture *capture,
|
|
|
|
obs_source_frame *frame, CMFormatDescriptionRef desc,
|
2014-07-08 05:27:46 -07:00
|
|
|
bool full_range)
|
2014-05-05 11:19:33 -07:00
|
|
|
{
|
2015-08-11 11:12:37 -07:00
|
|
|
video_colorspace colorspace = get_colorspace(desc);
|
|
|
|
video_range_type range = full_range ?
|
2014-07-08 05:27:46 -07:00
|
|
|
VIDEO_RANGE_FULL : VIDEO_RANGE_PARTIAL;
|
|
|
|
if (colorspace == capture->colorspace && range == capture->video_range)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
frame->full_range = full_range;
|
|
|
|
|
|
|
|
if (!video_format_get_parameters(colorspace, range,
|
|
|
|
frame->color_matrix,
|
|
|
|
frame->color_range_min,
|
|
|
|
frame->color_range_max)) {
|
|
|
|
AVLOG(LOG_ERROR, "Failed to get colorspace parameters for "
|
|
|
|
"colorspace %u range %u", colorspace, range);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
capture->colorspace = colorspace;
|
|
|
|
capture->video_range = range;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
static inline bool update_frame(av_capture *capture,
|
|
|
|
obs_source_frame *frame, CMSampleBufferRef sample_buffer)
|
2014-07-08 05:27:46 -07:00
|
|
|
{
|
|
|
|
CMFormatDescriptionRef desc =
|
|
|
|
CMSampleBufferGetFormatDescription(sample_buffer);
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
FourCharCode fourcc = CMFormatDescriptionGetMediaSubType(desc);
|
|
|
|
video_format format = format_from_subtype(fourcc);
|
|
|
|
CMVideoDimensions dims = CMVideoFormatDescriptionGetDimensions(desc);
|
2014-07-08 05:27:46 -07:00
|
|
|
|
|
|
|
CVImageBufferRef img = CMSampleBufferGetImageBuffer(sample_buffer);
|
|
|
|
|
|
|
|
if (format == VIDEO_FORMAT_NONE) {
|
|
|
|
if (capture->fourcc == fourcc)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
capture->fourcc = fourcc;
|
|
|
|
AVLOG(LOG_ERROR, "Unhandled fourcc: %s (0x%x) (%zu planes)",
|
|
|
|
AV_FOURCC_STR(fourcc), fourcc,
|
|
|
|
CVPixelBufferGetPlaneCount(img));
|
|
|
|
return false;
|
2014-05-05 11:19:33 -07:00
|
|
|
}
|
|
|
|
|
2014-07-08 05:27:46 -07:00
|
|
|
if (frame->format != format)
|
|
|
|
AVLOG(LOG_DEBUG, "Switching fourcc: "
|
|
|
|
"'%s' (0x%x) -> '%s' (0x%x)",
|
|
|
|
AV_FOURCC_STR(capture->fourcc), capture->fourcc,
|
|
|
|
AV_FOURCC_STR(fourcc), fourcc);
|
|
|
|
|
|
|
|
capture->fourcc = fourcc;
|
|
|
|
frame->format = format;
|
|
|
|
frame->width = dims.width;
|
|
|
|
frame->height = dims.height;
|
|
|
|
|
|
|
|
if (format_is_yuv(format) && !update_colorspace(capture, frame, desc,
|
|
|
|
is_fullrange_yuv(fourcc)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CVPixelBufferLockBaseAddress(img, kCVPixelBufferLock_ReadOnly);
|
|
|
|
|
|
|
|
if (!CVPixelBufferIsPlanar(img)) {
|
|
|
|
frame->linesize[0] = CVPixelBufferGetBytesPerRow(img);
|
2015-08-11 11:12:37 -07:00
|
|
|
frame->data[0] = static_cast<uint8_t*>(
|
|
|
|
CVPixelBufferGetBaseAddress(img));
|
2014-07-08 05:27:46 -07:00
|
|
|
return true;
|
2014-05-05 11:19:33 -07:00
|
|
|
}
|
2014-07-08 05:27:46 -07:00
|
|
|
|
|
|
|
size_t count = CVPixelBufferGetPlaneCount(img);
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
frame->linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(img, i);
|
2015-08-11 11:12:37 -07:00
|
|
|
frame->data[i] = static_cast<uint8_t*>(
|
|
|
|
CVPixelBufferGetBaseAddressOfPlane(img, i));
|
2014-07-08 05:27:46 -07:00
|
|
|
}
|
|
|
|
return true;
|
2014-05-05 11:19:33 -07:00
|
|
|
}
|
|
|
|
|
2014-04-28 14:58:08 -07:00
|
|
|
@implementation OBSAVCaptureDelegate
|
|
|
|
- (void)captureOutput:(AVCaptureOutput *)out
|
|
|
|
didDropSampleBuffer:(CMSampleBufferRef)sampleBuffer
|
|
|
|
fromConnection:(AVCaptureConnection *)connection
|
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(out);
|
|
|
|
UNUSED_PARAMETER(sampleBuffer);
|
|
|
|
UNUSED_PARAMETER(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)captureOutput:(AVCaptureOutput *)captureOutput
|
|
|
|
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
|
|
|
|
fromConnection:(AVCaptureConnection *)connection
|
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(captureOutput);
|
|
|
|
UNUSED_PARAMETER(connection);
|
|
|
|
|
|
|
|
CMItemCount count = CMSampleBufferGetNumSamples(sampleBuffer);
|
|
|
|
if (count < 1 || !capture)
|
|
|
|
return;
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
obs_source_frame *frame = &capture->frame;
|
2014-04-28 14:58:08 -07:00
|
|
|
|
2014-07-08 05:12:42 -07:00
|
|
|
CMTime target_pts =
|
|
|
|
CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer);
|
2014-05-12 15:10:57 -07:00
|
|
|
CMTime target_pts_nano = CMTimeConvertScale(target_pts, NANO_TIMESCALE,
|
|
|
|
kCMTimeRoundingMethod_Default);
|
2014-05-05 11:31:24 -07:00
|
|
|
frame->timestamp = target_pts_nano.value;
|
2014-04-28 14:58:08 -07:00
|
|
|
|
2014-07-08 05:27:46 -07:00
|
|
|
if (!update_frame(capture, frame, sampleBuffer))
|
|
|
|
return;
|
2014-05-05 11:19:33 -07:00
|
|
|
|
2014-04-28 14:58:08 -07:00
|
|
|
obs_source_output_video(capture->source, frame);
|
|
|
|
|
2014-07-08 05:27:46 -07:00
|
|
|
CVImageBufferRef img = CMSampleBufferGetImageBuffer(sampleBuffer);
|
2014-05-05 11:19:33 -07:00
|
|
|
CVPixelBufferUnlockBaseAddress(img, kCVPixelBufferLock_ReadOnly);
|
2014-04-28 14:58:08 -07:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
static void av_capture_enable_buffering(av_capture *capture, bool enabled)
|
2015-02-23 23:43:49 -08:00
|
|
|
{
|
|
|
|
obs_source_t *source = capture->source;
|
|
|
|
uint32_t flags = obs_source_get_flags(source);
|
|
|
|
if (enabled)
|
|
|
|
flags &= ~OBS_SOURCE_FLAG_UNBUFFERED;
|
|
|
|
else
|
|
|
|
flags |= OBS_SOURCE_FLAG_UNBUFFERED;
|
|
|
|
obs_source_set_flags(source, flags);
|
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
static const char *av_capture_getname(void*)
|
2014-04-28 14:58:08 -07:00
|
|
|
{
|
2014-07-09 22:12:57 -07:00
|
|
|
return TEXT_AVCAPTURE;
|
2014-04-28 14:58:08 -07:00
|
|
|
}
|
|
|
|
|
2015-08-18 11:00:55 -07:00
|
|
|
static void unlock_device(av_capture *capture, AVCaptureDevice *dev=nullptr)
|
|
|
|
{
|
|
|
|
if (!dev)
|
|
|
|
dev = capture->device;
|
|
|
|
|
|
|
|
if (dev && capture->device_locked)
|
|
|
|
[dev unlockForConfiguration];
|
|
|
|
|
|
|
|
capture->device_locked = false;
|
|
|
|
}
|
|
|
|
|
2015-08-18 11:00:55 -07:00
|
|
|
static void start_capture(av_capture *capture)
|
|
|
|
{
|
|
|
|
if (capture->session && !capture->session.running)
|
|
|
|
[capture->session startRunning];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clear_capture(av_capture *capture)
|
|
|
|
{
|
|
|
|
if (capture->session && capture->session.running)
|
|
|
|
[capture->session stopRunning];
|
|
|
|
|
|
|
|
obs_source_output_video(capture->source, nullptr);
|
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
static void remove_device(av_capture *capture)
|
2014-06-04 08:02:42 -07:00
|
|
|
{
|
2015-08-18 11:00:55 -07:00
|
|
|
clear_capture(capture);
|
|
|
|
|
2014-06-04 08:02:42 -07:00
|
|
|
[capture->session removeInput:capture->device_input];
|
|
|
|
|
2015-08-18 11:00:55 -07:00
|
|
|
unlock_device(capture);
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
capture->device_input = nullptr;
|
|
|
|
capture->device = nullptr;
|
2014-06-04 08:02:42 -07:00
|
|
|
}
|
|
|
|
|
2014-04-28 14:58:08 -07:00
|
|
|
static void av_capture_destroy(void *data)
|
|
|
|
{
|
2015-08-11 11:12:37 -07:00
|
|
|
auto capture = static_cast<av_capture*>(data);
|
2014-04-28 14:58:08 -07:00
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
delete capture;
|
2014-04-28 14:58:08 -07:00
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
static bool init_session(av_capture *capture)
|
2014-04-28 14:58:08 -07:00
|
|
|
{
|
2015-08-11 11:12:37 -07:00
|
|
|
auto session = [[AVCaptureSession alloc] init];
|
|
|
|
if (!session) {
|
2014-06-04 07:29:00 -07:00
|
|
|
AVLOG(LOG_ERROR, "Could not create AVCaptureSession");
|
2015-08-11 11:12:37 -07:00
|
|
|
return false;
|
2014-04-28 14:58:08 -07:00
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
auto delegate = [[OBSAVCaptureDelegate alloc] init];
|
|
|
|
if (!delegate) {
|
2014-06-04 07:29:00 -07:00
|
|
|
AVLOG(LOG_ERROR, "Could not create OBSAVCaptureDelegate");
|
2015-08-11 11:12:37 -07:00
|
|
|
return false;
|
2014-04-28 14:58:08 -07:00
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
delegate->capture = capture;
|
2014-04-28 14:58:08 -07:00
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
auto out = [[AVCaptureVideoDataOutput alloc] init];
|
|
|
|
if (!out) {
|
2014-06-04 07:29:00 -07:00
|
|
|
AVLOG(LOG_ERROR, "Could not create AVCaptureVideoDataOutput");
|
2015-08-11 11:12:37 -07:00
|
|
|
return false;
|
2014-04-28 14:58:08 -07:00
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
auto queue = dispatch_queue_create(NULL, NULL);
|
|
|
|
if (!queue) {
|
2014-06-04 07:29:00 -07:00
|
|
|
AVLOG(LOG_ERROR, "Could not create dispatch queue");
|
2015-08-11 11:12:37 -07:00
|
|
|
return false;
|
2014-04-28 14:58:08 -07:00
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
capture->session = session;
|
|
|
|
capture->delegate = delegate;
|
|
|
|
capture->out = out;
|
|
|
|
capture->queue = queue;
|
|
|
|
|
2014-04-28 14:58:08 -07:00
|
|
|
[capture->session addOutput:capture->out];
|
|
|
|
[capture->out
|
|
|
|
setSampleBufferDelegate:capture->delegate
|
|
|
|
queue:capture->queue];
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
static bool init_format(av_capture *capture, AVCaptureDevice *dev);
|
|
|
|
|
|
|
|
static bool init_device_input(av_capture *capture, AVCaptureDevice *dev)
|
2014-04-28 14:58:08 -07:00
|
|
|
{
|
|
|
|
NSError *err = nil;
|
2015-08-11 11:12:37 -07:00
|
|
|
AVCaptureDeviceInput *device_input = [AVCaptureDeviceInput
|
|
|
|
deviceInputWithDevice:dev error:&err];
|
|
|
|
if (!device_input) {
|
2014-06-04 07:29:00 -07:00
|
|
|
AVLOG(LOG_ERROR, "Error while initializing device input: %s",
|
2014-04-28 14:58:08 -07:00
|
|
|
err.localizedFailureReason.UTF8String);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
[capture->session addInput:device_input];
|
|
|
|
|
|
|
|
if (!init_format(capture, dev)) {
|
|
|
|
[capture->session removeInput:device_input];
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
capture->device_input = device_input;
|
2014-04-28 14:58:08 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t uint_from_dict(NSDictionary *dict, CFStringRef key)
|
|
|
|
{
|
|
|
|
return ((NSNumber*)dict[(__bridge NSString*)key]).unsignedIntValue;
|
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
static bool init_format(av_capture *capture, AVCaptureDevice *dev)
|
2014-04-28 14:58:08 -07:00
|
|
|
{
|
2015-08-11 11:12:37 -07:00
|
|
|
AVCaptureDeviceFormat *format = dev.activeFormat;
|
2014-04-28 14:58:08 -07:00
|
|
|
|
|
|
|
CMMediaType mtype = CMFormatDescriptionGetMediaType(
|
|
|
|
format.formatDescription);
|
|
|
|
// TODO: support other media types
|
|
|
|
if (mtype != kCMMediaType_Video) {
|
2014-07-08 05:30:27 -07:00
|
|
|
AVLOG(LOG_ERROR, "CMMediaType '%s' is unsupported",
|
|
|
|
AV_FOURCC_STR(mtype));
|
2014-04-28 14:58:08 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
capture->out.videoSettings = nil;
|
2014-07-08 05:27:46 -07:00
|
|
|
FourCharCode subtype = uint_from_dict(capture->out.videoSettings,
|
|
|
|
kCVPixelBufferPixelFormatTypeKey);
|
|
|
|
if (format_from_subtype(subtype) != VIDEO_FORMAT_NONE) {
|
|
|
|
AVLOG(LOG_DEBUG, "Using native fourcc '%s'",
|
|
|
|
AV_FOURCC_STR(subtype));
|
|
|
|
return true;
|
2014-04-28 14:58:08 -07:00
|
|
|
}
|
|
|
|
|
2014-07-08 05:27:46 -07:00
|
|
|
AVLOG(LOG_DEBUG, "Using fallback fourcc '%s' ('%s' 0x%08x unsupported)",
|
|
|
|
AV_FOURCC_STR(kCVPixelFormatType_32BGRA),
|
|
|
|
AV_FOURCC_STR(subtype), subtype);
|
2014-04-28 14:58:08 -07:00
|
|
|
|
2014-07-08 05:27:46 -07:00
|
|
|
capture->out.videoSettings = @{
|
|
|
|
(__bridge NSString*)kCVPixelBufferPixelFormatTypeKey:
|
|
|
|
@(kCVPixelFormatType_32BGRA)
|
|
|
|
};
|
2014-04-28 14:58:08 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-04 10:47:25 -07:00
|
|
|
static NSArray *presets(void);
|
|
|
|
static NSString *preset_names(NSString *preset);
|
|
|
|
|
|
|
|
static NSString *select_preset(AVCaptureDevice *dev, NSString *cur_preset)
|
|
|
|
{
|
|
|
|
NSString *new_preset = nil;
|
|
|
|
bool found_previous_preset = false;
|
2014-06-25 11:25:44 -07:00
|
|
|
for (NSString *preset in presets().reverseObjectEnumerator) {
|
2014-06-04 10:47:25 -07:00
|
|
|
if (!found_previous_preset)
|
|
|
|
found_previous_preset =
|
|
|
|
[cur_preset isEqualToString:preset];
|
|
|
|
|
|
|
|
if (![dev supportsAVCaptureSessionPreset:preset])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!new_preset || !found_previous_preset)
|
|
|
|
new_preset = preset;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_preset;
|
|
|
|
}
|
|
|
|
|
2015-08-18 11:00:55 -07:00
|
|
|
static bool init_preset(av_capture *capture, AVCaptureDevice *dev,
|
|
|
|
obs_data_t *settings)
|
|
|
|
{
|
|
|
|
clear_capture(capture);
|
|
|
|
|
2015-08-18 11:00:55 -07:00
|
|
|
unlock_device(capture, dev);
|
|
|
|
|
2015-08-18 11:00:55 -07:00
|
|
|
NSString *preset = get_string(settings, "preset");
|
|
|
|
if (![dev supportsAVCaptureSessionPreset:preset]) {
|
|
|
|
AVLOG(LOG_WARNING, "Preset %s not available",
|
|
|
|
preset_names(preset).UTF8String);
|
|
|
|
preset = select_preset(dev, preset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!preset) {
|
|
|
|
AVLOG(LOG_WARNING, "Could not select a preset, "
|
|
|
|
"initialization failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
capture->session.sessionPreset = preset;
|
|
|
|
AVLOG(LOG_INFO, "Using preset %s",
|
|
|
|
preset_names(preset).UTF8String);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-18 11:00:55 -07:00
|
|
|
static bool operator==(const CMVideoDimensions &a, const CMVideoDimensions &b);
|
|
|
|
static CMVideoDimensions get_dimensions(AVCaptureDeviceFormat *format);
|
|
|
|
|
|
|
|
static AVCaptureDeviceFormat *find_format(AVCaptureDevice *dev,
|
|
|
|
CMVideoDimensions dims)
|
|
|
|
{
|
|
|
|
for (AVCaptureDeviceFormat *format in dev.formats) {
|
|
|
|
if (get_dimensions(format) == dims)
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CMTime convert(media_frames_per_second fps)
|
|
|
|
{
|
|
|
|
CMTime time{};
|
|
|
|
time.value = fps.denominator;
|
|
|
|
time.timescale = fps.numerator;
|
|
|
|
time.flags = 1;
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool lock_device(av_capture *capture, AVCaptureDevice *dev)
|
|
|
|
{
|
|
|
|
if (!dev)
|
|
|
|
dev = capture->device;
|
|
|
|
|
|
|
|
NSError *err;
|
|
|
|
if (![dev lockForConfiguration:&err]) {
|
|
|
|
AVLOG(LOG_WARNING, "Could not lock device for configuration: "
|
|
|
|
"%s", err.localizedDescription.UTF8String);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
capture->device_locked = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Func>
|
|
|
|
static void find_formats(media_frames_per_second fps, AVCaptureDevice *dev,
|
|
|
|
const CMVideoDimensions *dims, Func &&f)
|
|
|
|
{
|
|
|
|
auto time = convert(fps);
|
|
|
|
|
|
|
|
for (AVCaptureDeviceFormat *format in dev.formats) {
|
|
|
|
if (!(get_dimensions(format) == *dims))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (AVFrameRateRange *range in
|
|
|
|
format.videoSupportedFrameRateRanges) {
|
|
|
|
if (CMTimeCompare(range.maxFrameDuration, time) >= 0 &&
|
|
|
|
CMTimeCompare(range.minFrameDuration,
|
|
|
|
time) <= 0)
|
|
|
|
if (f(format))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool init_manual(av_capture *capture, AVCaptureDevice *dev,
|
|
|
|
obs_data_t *settings)
|
|
|
|
{
|
|
|
|
clear_capture(capture);
|
|
|
|
|
|
|
|
auto input_format = obs_data_get_int(settings, "input_format");
|
|
|
|
FourCharCode actual_format = input_format;
|
|
|
|
|
|
|
|
SCOPE_EXIT
|
|
|
|
{
|
|
|
|
bool refresh = false;
|
|
|
|
if (input_format != actual_format) {
|
|
|
|
refresh = obs_data_get_autoselect_int(settings,
|
|
|
|
"input_format") != actual_format;
|
|
|
|
obs_data_set_autoselect_int(settings, "input_format",
|
|
|
|
actual_format);
|
|
|
|
} else {
|
|
|
|
refresh = obs_data_has_autoselect_value(settings,
|
|
|
|
"input_format");
|
|
|
|
obs_data_unset_autoselect_value(settings,
|
|
|
|
"input_format");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (refresh)
|
|
|
|
obs_source_update_properties(capture->source);
|
|
|
|
};
|
|
|
|
|
|
|
|
CMVideoDimensions dims{};
|
|
|
|
if (!get_resolution(settings, dims)) {
|
|
|
|
AVLOG(LOG_WARNING, "Could not load resolution");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
media_frames_per_second fps{};
|
|
|
|
if (!obs_data_get_frames_per_second(settings, "frame_rate", &fps,
|
|
|
|
nullptr)) {
|
|
|
|
AVLOG(LOG_WARNING, "Could not load frame rate");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVCaptureDeviceFormat *format = nullptr;
|
|
|
|
find_formats(fps, dev, &dims, [&](AVCaptureDeviceFormat *format_)
|
|
|
|
{
|
|
|
|
auto desc = format_.formatDescription;
|
|
|
|
auto fourcc = CMFormatDescriptionGetMediaSubType(desc);
|
|
|
|
if (input_format != INPUT_FORMAT_AUTO && fourcc != input_format)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
actual_format = fourcc;
|
|
|
|
format = format_;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!format) {
|
|
|
|
AVLOG(LOG_WARNING, "Frame rate is not supported: %g FPS "
|
|
|
|
"(%u/%u)",
|
|
|
|
media_frames_per_second_to_fps(fps),
|
|
|
|
fps.numerator, fps.denominator);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lock_device(capture, dev))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const char *if_name = input_format == INPUT_FORMAT_AUTO ?
|
|
|
|
"Auto" : fourcc_subtype_name(input_format);
|
|
|
|
|
|
|
|
#define IF_AUTO(x) (input_format != INPUT_FORMAT_AUTO ? "" : x)
|
|
|
|
AVLOG(LOG_INFO, "Capturing '%s' (%s):\n"
|
|
|
|
" Resolution: %ux%u\n"
|
|
|
|
" FPS: %g (%" PRIu32 "/%" PRIu32 ")\n"
|
|
|
|
" Frame interval: %g" NBSP "s\n"
|
|
|
|
" Input format: %s%s%s (%s)%s\n"
|
|
|
|
" Using format: %s",
|
|
|
|
dev.localizedName.UTF8String, dev.uniqueID.UTF8String,
|
|
|
|
dims.width, dims.height,
|
|
|
|
media_frames_per_second_to_fps(fps),
|
|
|
|
fps.numerator, fps.denominator,
|
|
|
|
media_frames_per_second_to_frame_interval(fps),
|
|
|
|
if_name, IF_AUTO(" (actual: "),
|
|
|
|
IF_AUTO(fourcc_subtype_name(actual_format)),
|
|
|
|
AV_FOURCC_STR(actual_format), IF_AUTO(")"),
|
|
|
|
format.description.UTF8String);
|
|
|
|
#undef IF_AUTO
|
|
|
|
|
|
|
|
dev.activeFormat = format;
|
|
|
|
dev.activeVideoMinFrameDuration = convert(fps);
|
|
|
|
dev.activeVideoMaxFrameDuration = convert(fps);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
static void capture_device(av_capture *capture, AVCaptureDevice *dev,
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_data_t *settings)
|
2014-04-28 14:58:08 -07:00
|
|
|
{
|
2015-08-11 11:12:37 -07:00
|
|
|
const char *name = dev.localizedName.UTF8String;
|
2014-08-05 11:09:29 -07:00
|
|
|
obs_data_set_string(settings, "device_name", name);
|
2015-08-11 11:12:37 -07:00
|
|
|
obs_data_set_string(settings, "device", dev.uniqueID.UTF8String);
|
2014-06-11 18:05:00 -07:00
|
|
|
AVLOG(LOG_INFO, "Selected device '%s'", name);
|
2014-04-28 14:58:08 -07:00
|
|
|
|
2014-08-05 11:09:29 -07:00
|
|
|
if (obs_data_get_bool(settings, "use_preset")) {
|
2015-08-18 11:00:55 -07:00
|
|
|
if (!init_preset(capture, dev, settings))
|
2014-06-04 10:47:25 -07:00
|
|
|
return;
|
2015-08-18 11:00:55 -07:00
|
|
|
|
|
|
|
} else {
|
|
|
|
if (!init_manual(capture, dev, settings))
|
|
|
|
return;
|
2014-06-04 10:47:25 -07:00
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
if (!init_device_input(capture, dev))
|
|
|
|
return;
|
2014-04-28 14:58:08 -07:00
|
|
|
|
2014-05-05 11:31:24 -07:00
|
|
|
AVCaptureInputPort *port = capture->device_input.ports[0];
|
|
|
|
capture->has_clock = [port respondsToSelector:@selector(clock)];
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
capture->device = dev;
|
2015-08-18 11:00:55 -07:00
|
|
|
start_capture(capture);
|
2014-06-04 07:50:58 -07:00
|
|
|
return;
|
2014-04-28 14:58:08 -07:00
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
static inline void handle_disconnect_capture(av_capture *capture,
|
2014-06-04 10:57:32 -07:00
|
|
|
AVCaptureDevice *dev)
|
|
|
|
{
|
|
|
|
if (![dev.uniqueID isEqualTo:capture->uid])
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!capture->device) {
|
2015-08-14 08:04:41 -07:00
|
|
|
AVLOG(LOG_INFO, "Received disconnect for inactive device '%s'",
|
2014-06-04 10:57:32 -07:00
|
|
|
capture->uid.UTF8String);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-14 08:04:41 -07:00
|
|
|
AVLOG(LOG_WARNING, "Device with unique ID '%s' disconnected",
|
2014-06-04 10:57:32 -07:00
|
|
|
dev.uniqueID.UTF8String);
|
|
|
|
|
|
|
|
remove_device(capture);
|
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
static inline void handle_disconnect(av_capture *capture,
|
2014-09-30 08:25:43 -07:00
|
|
|
AVCaptureDevice *dev)
|
2014-06-04 10:57:32 -07:00
|
|
|
{
|
|
|
|
if (!dev)
|
|
|
|
return;
|
|
|
|
|
2014-09-30 08:25:43 -07:00
|
|
|
handle_disconnect_capture(capture, dev);
|
|
|
|
obs_source_update_properties(capture->source);
|
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
static inline void handle_connect_capture(av_capture *capture,
|
2014-09-30 08:25:43 -07:00
|
|
|
AVCaptureDevice *dev, obs_data_t *settings)
|
|
|
|
{
|
2014-06-04 10:57:32 -07:00
|
|
|
if (![dev.uniqueID isEqualTo:capture->uid])
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (capture->device) {
|
|
|
|
AVLOG(LOG_ERROR, "Received connect for in-use device '%s'",
|
|
|
|
capture->uid.UTF8String);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVLOG(LOG_INFO, "Device with unique ID '%s' connected, "
|
|
|
|
"resuming capture", dev.uniqueID.UTF8String);
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
capture_device(capture, dev, settings);
|
2014-06-04 10:57:32 -07:00
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
static inline void handle_connect(av_capture *capture,
|
2014-09-30 08:25:43 -07:00
|
|
|
AVCaptureDevice *dev, obs_data_t *settings)
|
|
|
|
{
|
|
|
|
if (!dev)
|
|
|
|
return;
|
|
|
|
|
|
|
|
handle_connect_capture(capture, dev, settings);
|
|
|
|
obs_source_update_properties(capture->source);
|
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
static bool av_capture_init(av_capture *capture, obs_data_t *settings)
|
2014-06-04 08:02:42 -07:00
|
|
|
{
|
|
|
|
if (!init_session(capture))
|
2015-08-11 11:12:37 -07:00
|
|
|
return false;
|
2014-06-04 08:02:42 -07:00
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
capture->uid = get_string(settings, "device");
|
2014-06-04 10:57:32 -07:00
|
|
|
|
|
|
|
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
|
2015-08-11 11:12:37 -07:00
|
|
|
capture->disconnect_observer.reset([nc
|
2014-06-04 10:57:32 -07:00
|
|
|
addObserverForName:AVCaptureDeviceWasDisconnectedNotification
|
|
|
|
object:nil
|
|
|
|
queue:[NSOperationQueue mainQueue]
|
|
|
|
usingBlock:^(NSNotification *note)
|
|
|
|
{
|
|
|
|
handle_disconnect(capture, note.object);
|
|
|
|
}
|
2015-08-11 11:12:37 -07:00
|
|
|
]);
|
2014-06-04 10:57:32 -07:00
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
capture->connect_observer.reset([nc
|
2014-06-04 10:57:32 -07:00
|
|
|
addObserverForName:AVCaptureDeviceWasConnectedNotification
|
|
|
|
object:nil
|
|
|
|
queue:[NSOperationQueue mainQueue]
|
|
|
|
usingBlock:^(NSNotification *note)
|
|
|
|
{
|
|
|
|
handle_connect(capture, note.object, settings);
|
|
|
|
}
|
2015-08-11 11:12:37 -07:00
|
|
|
]);
|
2014-06-04 08:02:42 -07:00
|
|
|
|
|
|
|
AVCaptureDevice *dev =
|
2015-08-11 11:12:37 -07:00
|
|
|
[AVCaptureDevice deviceWithUniqueID:capture->uid];
|
2014-06-04 08:02:42 -07:00
|
|
|
|
|
|
|
if (!dev) {
|
2014-06-04 10:57:32 -07:00
|
|
|
if (capture->uid.length < 1)
|
2015-08-14 08:04:41 -07:00
|
|
|
AVLOG(LOG_INFO, "No device selected");
|
2014-06-04 08:02:42 -07:00
|
|
|
else
|
2015-08-14 08:04:41 -07:00
|
|
|
AVLOG(LOG_WARNING, "Could not initialize device " \
|
2014-06-04 08:02:42 -07:00
|
|
|
"with unique ID '%s'",
|
2014-06-04 10:57:32 -07:00
|
|
|
capture->uid.UTF8String);
|
2015-08-11 11:12:37 -07:00
|
|
|
return true;
|
2014-06-04 08:02:42 -07:00
|
|
|
}
|
|
|
|
|
2014-06-04 10:57:32 -07:00
|
|
|
capture_device(capture, dev, settings);
|
2015-08-11 11:12:37 -07:00
|
|
|
|
|
|
|
return true;
|
2014-06-04 08:02:42 -07:00
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
static void *av_capture_create(obs_data_t *settings, obs_source_t *source)
|
2014-04-28 14:58:08 -07:00
|
|
|
{
|
2015-08-11 11:12:37 -07:00
|
|
|
unique_ptr<av_capture> capture;
|
2014-04-28 14:58:08 -07:00
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
try {
|
|
|
|
capture.reset(new av_capture());
|
|
|
|
} catch (...) {
|
|
|
|
return capture.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
capture->source = source;
|
2014-04-28 14:58:08 -07:00
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
if (!av_capture_init(capture.get(), settings)) {
|
|
|
|
AVLOG(LOG_ERROR, "av_capture_init failed");
|
|
|
|
return nullptr;
|
2014-06-04 07:50:58 -07:00
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
av_capture_enable_buffering(capture.get(),
|
2015-02-23 23:43:49 -08:00
|
|
|
obs_data_get_bool(settings, "buffering"));
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
return capture.release();
|
2014-04-28 14:58:08 -07:00
|
|
|
}
|
|
|
|
|
2014-06-04 10:48:31 -07:00
|
|
|
static NSArray *presets(void)
|
|
|
|
{
|
2014-04-28 14:58:08 -07:00
|
|
|
return @[
|
|
|
|
//AVCaptureSessionPresetiFrame1280x720,
|
2014-06-25 11:25:44 -07:00
|
|
|
//AVCaptureSessionPresetiFrame960x540,
|
|
|
|
AVCaptureSessionPreset1280x720,
|
|
|
|
AVCaptureSessionPreset960x540,
|
|
|
|
AVCaptureSessionPreset640x480,
|
|
|
|
AVCaptureSessionPreset352x288,
|
|
|
|
AVCaptureSessionPreset320x240,
|
|
|
|
//AVCaptureSessionPresetHigh,
|
|
|
|
//AVCaptureSessionPresetMedium,
|
|
|
|
//AVCaptureSessionPresetLow,
|
|
|
|
//AVCaptureSessionPresetPhoto,
|
2014-04-28 14:58:08 -07:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
static NSString *preset_names(NSString *preset)
|
|
|
|
{
|
|
|
|
NSDictionary *preset_names = @{
|
|
|
|
AVCaptureSessionPresetLow:@"Low",
|
|
|
|
AVCaptureSessionPresetMedium:@"Medium",
|
|
|
|
AVCaptureSessionPresetHigh:@"High",
|
|
|
|
AVCaptureSessionPreset320x240:@"320x240",
|
|
|
|
AVCaptureSessionPreset352x288:@"352x288",
|
|
|
|
AVCaptureSessionPreset640x480:@"640x480",
|
|
|
|
AVCaptureSessionPreset960x540:@"960x540",
|
|
|
|
AVCaptureSessionPreset1280x720:@"1280x720",
|
|
|
|
};
|
2014-06-11 17:30:06 -07:00
|
|
|
NSString *name = preset_names[preset];
|
|
|
|
if (name)
|
|
|
|
return name;
|
|
|
|
return [NSString stringWithFormat:@"Unknown (%@)", preset];
|
2014-04-28 14:58:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
static void av_capture_defaults(obs_data_t *settings)
|
2014-04-28 14:58:08 -07:00
|
|
|
{
|
2015-08-14 08:21:34 -07:00
|
|
|
obs_data_set_default_string(settings, "uid", "");
|
2014-04-28 14:58:08 -07:00
|
|
|
obs_data_set_default_bool(settings, "use_preset", true);
|
2015-08-18 11:00:55 -07:00
|
|
|
|
2014-06-25 13:56:10 -07:00
|
|
|
obs_data_set_default_string(settings, "preset",
|
|
|
|
AVCaptureSessionPreset1280x720.UTF8String);
|
2015-08-18 11:00:55 -07:00
|
|
|
|
|
|
|
obs_data_set_default_int(settings, "input_format", INPUT_FORMAT_AUTO);
|
2014-04-28 14:58:08 -07:00
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
static bool update_device_list(obs_property_t *list,
|
2014-06-11 18:05:00 -07:00
|
|
|
NSString *uid, NSString *name, bool disconnected)
|
|
|
|
{
|
|
|
|
bool dev_found = false;
|
|
|
|
bool list_modified = false;
|
|
|
|
|
|
|
|
size_t size = obs_property_list_item_count(list);
|
|
|
|
for (size_t i = 0; i < size;) {
|
|
|
|
const char *uid_ = obs_property_list_item_string(list, i);
|
2015-05-30 18:34:29 -07:00
|
|
|
bool found = [uid isEqualToString:@(uid_ ? uid_ : "")];
|
2014-06-11 18:05:00 -07:00
|
|
|
bool disabled = obs_property_list_item_disabled(list, i);
|
|
|
|
if (!found && !disabled) {
|
|
|
|
i += 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disabled && !found) {
|
|
|
|
list_modified = true;
|
|
|
|
obs_property_list_item_remove(list, i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disabled != disconnected)
|
|
|
|
list_modified = true;
|
|
|
|
|
|
|
|
dev_found = true;
|
|
|
|
obs_property_list_item_disable(list, i, disconnected);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev_found)
|
|
|
|
return list_modified;
|
|
|
|
|
|
|
|
size_t idx = obs_property_list_add_string(list, name.UTF8String,
|
|
|
|
uid.UTF8String);
|
|
|
|
obs_property_list_item_disable(list, idx, disconnected);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
static void fill_presets(AVCaptureDevice *dev, obs_property_t *list,
|
2014-06-11 17:30:06 -07:00
|
|
|
NSString *current_preset)
|
|
|
|
{
|
|
|
|
obs_property_list_clear(list);
|
|
|
|
|
|
|
|
bool preset_found = false;
|
|
|
|
for (NSString *preset in presets()) {
|
|
|
|
bool is_current = [preset isEqualToString:current_preset];
|
2014-07-11 06:49:31 -07:00
|
|
|
bool supported = dev &&
|
2014-06-11 18:05:00 -07:00
|
|
|
[dev supportsAVCaptureSessionPreset:preset];
|
2014-06-11 17:30:06 -07:00
|
|
|
|
|
|
|
if (is_current)
|
|
|
|
preset_found = true;
|
|
|
|
|
|
|
|
if (!supported && !is_current)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
size_t idx = obs_property_list_add_string(list,
|
|
|
|
preset_names(preset).UTF8String,
|
|
|
|
preset.UTF8String);
|
|
|
|
obs_property_list_item_disable(list, idx, !supported);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preset_found)
|
|
|
|
return;
|
|
|
|
|
|
|
|
size_t idx = obs_property_list_add_string(list,
|
|
|
|
preset_names(current_preset).UTF8String,
|
|
|
|
current_preset.UTF8String);
|
|
|
|
obs_property_list_item_disable(list, idx, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool check_preset(AVCaptureDevice *dev,
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_property_t *list, obs_data_t *settings)
|
2014-06-11 17:30:06 -07:00
|
|
|
{
|
|
|
|
NSString *current_preset = get_string(settings, "preset");
|
|
|
|
|
|
|
|
size_t size = obs_property_list_item_count(list);
|
|
|
|
NSMutableSet *listed = [NSMutableSet setWithCapacity:size];
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; i++)
|
|
|
|
[listed addObject:@(obs_property_list_item_string(list, i))];
|
|
|
|
|
|
|
|
bool presets_changed = false;
|
|
|
|
for (NSString *preset in presets()) {
|
|
|
|
bool is_listed = [listed member:preset] != nil;
|
2014-07-11 06:49:31 -07:00
|
|
|
bool supported = dev &&
|
2014-06-11 18:05:00 -07:00
|
|
|
[dev supportsAVCaptureSessionPreset:preset];
|
2014-06-11 17:30:06 -07:00
|
|
|
|
|
|
|
if (supported == is_listed)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
presets_changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!presets_changed && [listed member:current_preset] != nil)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
fill_presets(dev, list, current_preset);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
static bool autoselect_preset(AVCaptureDevice *dev, obs_data_t *settings)
|
2014-06-25 09:17:02 -07:00
|
|
|
{
|
|
|
|
NSString *preset = get_string(settings, "preset");
|
|
|
|
if (!dev || [dev supportsAVCaptureSessionPreset:preset]) {
|
2014-08-09 12:38:56 -07:00
|
|
|
if (obs_data_has_autoselect_value(settings, "preset")) {
|
2014-06-25 09:17:02 -07:00
|
|
|
obs_data_unset_autoselect_value(settings, "preset");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
preset = select_preset(dev, preset);
|
|
|
|
const char *autoselect =
|
|
|
|
obs_data_get_autoselect_string(settings, "preset");
|
|
|
|
if (![preset isEqualToString:@(autoselect)]) {
|
|
|
|
obs_data_set_autoselect_string(settings, "preset",
|
|
|
|
preset.UTF8String);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-18 11:00:55 -07:00
|
|
|
static CMVideoDimensions get_dimensions(AVCaptureDeviceFormat *format)
|
|
|
|
{
|
|
|
|
auto desc = format.formatDescription;
|
|
|
|
return CMVideoFormatDescriptionGetDimensions(desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
using resolutions_t = vector<CMVideoDimensions>;
|
|
|
|
|
|
|
|
static resolutions_t enumerate_resolutions(AVCaptureDevice *dev)
|
|
|
|
{
|
|
|
|
resolutions_t res;
|
|
|
|
|
|
|
|
if (!dev)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
res.reserve(dev.formats.count + 1);
|
|
|
|
|
|
|
|
for (AVCaptureDeviceFormat *format in dev.formats) {
|
|
|
|
auto dims = get_dimensions(format);
|
|
|
|
|
|
|
|
if (find(begin(res), end(res), dims) == end(res))
|
|
|
|
res.push_back(dims);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sort_resolutions(vector<CMVideoDimensions> &resolutions)
|
|
|
|
{
|
|
|
|
auto cmp = [](const CMVideoDimensions &a, const CMVideoDimensions &b)
|
|
|
|
{
|
|
|
|
return a.width * a.height > b.width * b.height;
|
|
|
|
};
|
|
|
|
|
|
|
|
sort(begin(resolutions), end(resolutions), cmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void data_set_resolution(obs_data_t *data, const CMVideoDimensions &dims)
|
|
|
|
{
|
|
|
|
obs_data_set_int(data, "width", dims.width);
|
|
|
|
obs_data_set_int(data, "height", dims.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void data_set_resolution(const unique_ptr<obs_data_t> &data,
|
|
|
|
const CMVideoDimensions &dims)
|
|
|
|
{
|
|
|
|
data_set_resolution(data.get(), dims);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool add_resolution_to_list(vector<CMVideoDimensions> &res,
|
|
|
|
const CMVideoDimensions &dims)
|
|
|
|
{
|
|
|
|
if (find(begin(res), end(res), dims) != end(res))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
res.push_back(dims);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *obs_data_get_json(const unique_ptr<obs_data_t> &data)
|
|
|
|
{
|
|
|
|
return obs_data_get_json(data.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool operator==(const CMVideoDimensions &a, const CMVideoDimensions &b)
|
|
|
|
{
|
|
|
|
return a.width == b.width && a.height == b.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool resolution_property_needs_update(obs_property_t *p,
|
|
|
|
const resolutions_t &resolutions)
|
|
|
|
{
|
|
|
|
vector<bool> res_found(resolutions.size());
|
|
|
|
|
|
|
|
auto num = obs_property_list_item_count(p);
|
|
|
|
for (size_t i = 1; i < num; i++) { // skip empty entry
|
|
|
|
const char *json = obs_property_list_item_string(p, i);
|
|
|
|
unique_ptr<obs_data_t> buffer{obs_data_create_from_json(json)};
|
|
|
|
|
|
|
|
CMVideoDimensions dims{};
|
|
|
|
if (!get_resolution(buffer.get(), dims))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto pos = find(begin(resolutions), end(resolutions), dims);
|
|
|
|
if (pos == end(resolutions))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
res_found[pos - begin(resolutions)] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return any_of(begin(res_found), end(res_found),
|
|
|
|
[](bool b) { return !b; });
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool update_resolution_property(obs_properties_t *props,
|
|
|
|
const config_helper &conf, obs_property_t *p=nullptr)
|
|
|
|
{
|
|
|
|
if (!p)
|
|
|
|
p = obs_properties_get(props, "resolution");
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto valid_dims = conf.dims();
|
|
|
|
auto resolutions = enumerate_resolutions(conf.dev());
|
|
|
|
|
|
|
|
bool unsupported = true;
|
|
|
|
if (valid_dims)
|
|
|
|
unsupported = add_resolution_to_list(resolutions, *valid_dims);
|
|
|
|
|
|
|
|
bool was_enabled = obs_property_enabled(p);
|
|
|
|
obs_property_set_enabled(p, !!conf.dev());
|
|
|
|
|
|
|
|
if (!resolution_property_needs_update(p, resolutions))
|
|
|
|
return was_enabled != obs_property_enabled(p);
|
|
|
|
|
|
|
|
sort_resolutions(resolutions);
|
|
|
|
|
|
|
|
obs_property_list_clear(p);
|
|
|
|
obs_property_list_add_string(p, "", "{}");
|
|
|
|
|
|
|
|
DStr name;
|
|
|
|
unique_ptr<obs_data_t> buffer{obs_data_create()};
|
|
|
|
for (const CMVideoDimensions &dims : resolutions) {
|
|
|
|
data_set_resolution(buffer, dims);
|
|
|
|
auto json = obs_data_get_json(buffer);
|
|
|
|
dstr_printf(name, "%dx%d", dims.width, dims.height);
|
|
|
|
size_t idx = obs_property_list_add_string(p, name->array, json);
|
|
|
|
|
|
|
|
if (unsupported && valid_dims && dims == *valid_dims)
|
|
|
|
obs_property_list_item_disable(p, idx, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static media_frames_per_second convert(CMTime time_)
|
|
|
|
{
|
|
|
|
media_frames_per_second res{};
|
|
|
|
clamp(res.numerator, time_.timescale);
|
|
|
|
clamp(res.denominator, time_.value);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
using frame_rates_t = vector<pair<media_frames_per_second,
|
|
|
|
media_frames_per_second>>;
|
|
|
|
static frame_rates_t enumerate_frame_rates(AVCaptureDevice *dev,
|
|
|
|
const CMVideoDimensions *dims = nullptr)
|
|
|
|
{
|
|
|
|
frame_rates_t res;
|
|
|
|
|
|
|
|
if (!dev || !dims)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
auto add_unique_frame_rate_range = [&](AVFrameRateRange *range)
|
|
|
|
{
|
|
|
|
auto min = convert(range.maxFrameDuration);
|
|
|
|
auto max = convert(range.minFrameDuration);
|
|
|
|
|
|
|
|
auto pair = make_pair(min, max);
|
|
|
|
|
|
|
|
if (find(begin(res), end(res), pair) != end(res))
|
|
|
|
return;
|
|
|
|
|
|
|
|
res.push_back(pair);
|
|
|
|
};
|
|
|
|
|
|
|
|
for (AVCaptureDeviceFormat *format in dev.formats) {
|
|
|
|
if (!(get_dimensions(format) == *dims))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (AVFrameRateRange *range in
|
|
|
|
format.videoSupportedFrameRateRanges) {
|
|
|
|
add_unique_frame_rate_range(range);
|
|
|
|
|
|
|
|
//FIXME remove debug true
|
|
|
|
if (true || CMTimeCompare(range.minFrameDuration,
|
|
|
|
range.maxFrameDuration) != 0) {
|
|
|
|
blog(LOG_WARNING, "Got actual frame rate range:"
|
|
|
|
" %g - %g "
|
|
|
|
"({%lld, %d} - {%lld, %d})",
|
|
|
|
range.minFrameRate,
|
|
|
|
range.maxFrameRate,
|
|
|
|
range.maxFrameDuration.value,
|
|
|
|
range.maxFrameDuration.timescale,
|
|
|
|
range.minFrameDuration.value,
|
|
|
|
range.minFrameDuration.timescale
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool operator==(const media_frames_per_second &a,
|
|
|
|
const media_frames_per_second &b)
|
|
|
|
{
|
|
|
|
return a.numerator == b.numerator && a.denominator == b.denominator;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool operator!=(const media_frames_per_second &a,
|
|
|
|
const media_frames_per_second &b)
|
|
|
|
{
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool frame_rate_property_needs_update(obs_property_t *p,
|
|
|
|
const frame_rates_t &frame_rates)
|
|
|
|
{
|
|
|
|
auto fps_num = frame_rates.size();
|
|
|
|
auto num = obs_property_frame_rate_fps_ranges_count(p);
|
|
|
|
if (fps_num != num)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
vector<bool> fps_found(fps_num);
|
|
|
|
for (size_t i = 0; i < num; i++) {
|
|
|
|
auto min_ = obs_property_frame_rate_fps_range_min(p, i);
|
|
|
|
auto max_ = obs_property_frame_rate_fps_range_max(p, i);
|
|
|
|
|
|
|
|
auto it = find(begin(frame_rates), end(frame_rates),
|
|
|
|
make_pair(min_, max_));
|
|
|
|
if (it == end(frame_rates))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
fps_found[it - begin(frame_rates)] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return any_of(begin(fps_found), end(fps_found),
|
|
|
|
[](bool b) { return !b; });
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool update_frame_rate_property(obs_properties_t *props,
|
|
|
|
const config_helper &conf, obs_property_t *p=nullptr)
|
|
|
|
{
|
|
|
|
if (!p)
|
|
|
|
p = obs_properties_get(props, "frame_rate");
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto valid_dims = conf.dims();
|
|
|
|
auto frame_rates = enumerate_frame_rates(conf.dev(), valid_dims);
|
|
|
|
|
|
|
|
bool was_enabled = obs_property_enabled(p);
|
|
|
|
obs_property_set_enabled(p, !frame_rates.empty());
|
|
|
|
|
|
|
|
if (!frame_rate_property_needs_update(p, frame_rates))
|
|
|
|
return was_enabled != obs_property_enabled(p);
|
|
|
|
|
|
|
|
obs_property_frame_rate_fps_ranges_clear(p);
|
|
|
|
for (auto &pair : frame_rates)
|
|
|
|
obs_property_frame_rate_fps_range_add(p,
|
|
|
|
pair.first, pair.second);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static vector<AVCaptureDeviceFormat*> enumerate_formats(AVCaptureDevice *dev,
|
|
|
|
const CMVideoDimensions &dims,
|
|
|
|
const media_frames_per_second &fps)
|
|
|
|
{
|
|
|
|
vector<AVCaptureDeviceFormat*> result;
|
|
|
|
|
|
|
|
find_formats(fps, dev, &dims, [&](AVCaptureDeviceFormat *format)
|
|
|
|
{
|
|
|
|
result.push_back(format);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool input_format_property_needs_update(obs_property_t *p,
|
|
|
|
const vector<AVCaptureDeviceFormat*> &formats,
|
|
|
|
const FourCharCode *fourcc_)
|
|
|
|
{
|
|
|
|
bool fourcc_found = !fourcc_;
|
|
|
|
vector<bool> if_found(formats.size());
|
|
|
|
|
|
|
|
auto num = obs_property_list_item_count(p);
|
|
|
|
for (size_t i = 1; i < num; i++) { // skip auto entry
|
|
|
|
FourCharCode fourcc = obs_property_list_item_int(p, i);
|
|
|
|
fourcc_found = fourcc_found || fourcc == *fourcc_;
|
|
|
|
|
|
|
|
auto pos = find_if(begin(formats), end(formats),
|
|
|
|
[&](AVCaptureDeviceFormat *format)
|
|
|
|
{
|
|
|
|
FourCharCode fourcc_ = 0;
|
|
|
|
format_description_subtype_name(
|
|
|
|
format.formatDescription, &fourcc_);
|
|
|
|
return fourcc_ == fourcc;
|
|
|
|
});
|
|
|
|
if (pos == end(formats))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if_found[pos - begin(formats)] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fourcc_found || any_of(begin(if_found), end(if_found),
|
|
|
|
[](bool b) { return !b; });
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool update_input_format_property(obs_properties_t *props,
|
|
|
|
const config_helper &conf, obs_property_t *p=nullptr)
|
|
|
|
{
|
|
|
|
if (!p)
|
|
|
|
p = obs_properties_get(props, "input_format");
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto update_enabled = [&](bool enabled)
|
|
|
|
{
|
|
|
|
bool was_enabled = obs_property_enabled(p);
|
|
|
|
obs_property_set_enabled(p, enabled);
|
|
|
|
return was_enabled != enabled;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto valid_dims = conf.dims();
|
|
|
|
auto valid_fps = conf.fps();
|
|
|
|
auto valid_if = conf.input_format();
|
|
|
|
|
|
|
|
if (!valid_dims || !valid_fps)
|
|
|
|
return update_enabled(false);
|
|
|
|
|
|
|
|
auto formats = enumerate_formats(conf.dev(), *valid_dims, *valid_fps);
|
|
|
|
if (!input_format_property_needs_update(p, formats, valid_if))
|
|
|
|
return update_enabled(!formats.empty());
|
|
|
|
|
|
|
|
while (obs_property_list_item_count(p) > 1)
|
|
|
|
obs_property_list_item_remove(p, 1);
|
|
|
|
|
|
|
|
bool fourcc_found = !valid_if || *valid_if == INPUT_FORMAT_AUTO;
|
|
|
|
for (auto &format : formats) {
|
|
|
|
FourCharCode fourcc = 0;
|
|
|
|
const char *name = format_description_subtype_name(
|
|
|
|
format.formatDescription, &fourcc);
|
|
|
|
obs_property_list_add_int(p, name, fourcc);
|
|
|
|
fourcc_found = fourcc_found || fourcc == *valid_if;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fourcc_found) {
|
|
|
|
const char *name = fourcc_subtype_name(*valid_if);
|
|
|
|
obs_property_list_add_int(p, name, *valid_if);
|
|
|
|
}
|
|
|
|
|
|
|
|
return update_enabled(!formats.empty());
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
static bool properties_device_changed(obs_properties_t *props, obs_property_t *p,
|
|
|
|
obs_data_t *settings)
|
2014-06-04 10:55:54 -07:00
|
|
|
{
|
|
|
|
NSString *uid = get_string(settings, "device");
|
|
|
|
AVCaptureDevice *dev = [AVCaptureDevice deviceWithUniqueID:uid];
|
|
|
|
|
2014-06-11 18:05:00 -07:00
|
|
|
NSString *name = get_string(settings, "device_name");
|
2015-08-14 08:21:34 -07:00
|
|
|
bool dev_list_updated = update_device_list(p, uid, name,
|
|
|
|
!dev && uid.length);
|
2014-06-11 18:05:00 -07:00
|
|
|
|
|
|
|
p = obs_properties_get(props, "preset");
|
2014-06-25 09:17:02 -07:00
|
|
|
bool preset_list_changed = check_preset(dev, p, settings);
|
|
|
|
bool autoselect_changed = autoselect_preset(dev, settings);
|
|
|
|
|
2015-08-18 11:00:55 -07:00
|
|
|
config_helper conf{settings};
|
|
|
|
bool res_changed = update_resolution_property(props, conf);
|
|
|
|
bool fps_changed = update_frame_rate_property(props, conf);
|
|
|
|
bool if_changed = update_input_format_property(props, conf);
|
|
|
|
|
|
|
|
return preset_list_changed || autoselect_changed || dev_list_updated
|
|
|
|
|| res_changed || fps_changed || if_changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool properties_use_preset_changed(obs_properties_t *props,
|
|
|
|
obs_property_t *, obs_data_t *settings)
|
|
|
|
{
|
|
|
|
auto use_preset = obs_data_get_bool(settings, "use_preset");
|
|
|
|
|
|
|
|
config_helper conf{settings};
|
|
|
|
|
|
|
|
bool updated = false;
|
|
|
|
bool visible = false;
|
|
|
|
obs_property_t *p = nullptr;
|
|
|
|
|
|
|
|
auto noop = [](obs_properties_t *, const config_helper&,
|
|
|
|
obs_property_t *)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define UPDATE_PROPERTY(prop, uses_preset, func) \
|
|
|
|
p = obs_properties_get(props, prop); \
|
|
|
|
visible = use_preset == uses_preset; \
|
|
|
|
updated = obs_property_visible(p) != visible || updated; \
|
|
|
|
obs_property_set_visible(p, visible);\
|
|
|
|
updated = func(props, conf, p) || updated;
|
|
|
|
|
|
|
|
UPDATE_PROPERTY("preset", true, noop);
|
|
|
|
UPDATE_PROPERTY("resolution", false, update_resolution_property);
|
|
|
|
UPDATE_PROPERTY("frame_rate", false, update_frame_rate_property);
|
|
|
|
UPDATE_PROPERTY("input_format", false, update_input_format_property);
|
|
|
|
|
|
|
|
return updated;
|
2014-06-11 17:30:06 -07:00
|
|
|
}
|
2014-06-04 10:55:54 -07:00
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
static bool properties_preset_changed(obs_properties_t *, obs_property_t *p,
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_data_t *settings)
|
2014-06-11 17:30:06 -07:00
|
|
|
{
|
|
|
|
NSString *uid = get_string(settings, "device");
|
|
|
|
AVCaptureDevice *dev = [AVCaptureDevice deviceWithUniqueID:uid];
|
2014-06-04 10:55:54 -07:00
|
|
|
|
2014-06-25 09:17:02 -07:00
|
|
|
bool preset_list_changed = check_preset(dev, p, settings);
|
|
|
|
bool autoselect_changed = autoselect_preset(dev, settings);
|
|
|
|
|
|
|
|
return preset_list_changed || autoselect_changed;
|
2014-06-04 10:55:54 -07:00
|
|
|
}
|
|
|
|
|
2015-08-18 11:00:55 -07:00
|
|
|
static bool properties_resolution_changed(obs_properties_t *props,
|
|
|
|
obs_property_t *p, obs_data_t *settings)
|
|
|
|
{
|
|
|
|
config_helper conf{settings};
|
|
|
|
|
|
|
|
bool res_updated = update_resolution_property(props, conf, p);
|
|
|
|
bool fps_updated = update_frame_rate_property(props, conf);
|
|
|
|
bool if_updated = update_input_format_property(props, conf);
|
|
|
|
|
|
|
|
return res_updated || fps_updated || if_updated;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool properties_frame_rate_changed(obs_properties_t *props,
|
|
|
|
obs_property_t *p, obs_data_t *settings)
|
|
|
|
{
|
|
|
|
config_helper conf{settings};
|
|
|
|
|
|
|
|
bool fps_updated = update_frame_rate_property(props, conf, p);
|
|
|
|
bool if_updated = update_input_format_property(props, conf);
|
|
|
|
|
|
|
|
return fps_updated || if_updated;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool properties_input_format_changed(obs_properties_t *props,
|
|
|
|
obs_property_t *p, obs_data_t *settings)
|
|
|
|
{
|
|
|
|
config_helper conf{settings};
|
|
|
|
|
|
|
|
return update_input_format_property(props, conf, p);
|
|
|
|
}
|
|
|
|
|
2015-10-23 02:55:12 -07:00
|
|
|
static void add_preset_properties(obs_properties_t *props)
|
|
|
|
{
|
|
|
|
obs_property_t *preset_list = obs_properties_add_list(props, "preset",
|
|
|
|
TEXT_PRESET, OBS_COMBO_TYPE_LIST,
|
|
|
|
OBS_COMBO_FORMAT_STRING);
|
|
|
|
for (NSString *preset in presets())
|
|
|
|
obs_property_list_add_string(preset_list,
|
|
|
|
preset_names(preset).UTF8String,
|
|
|
|
preset.UTF8String);
|
|
|
|
|
|
|
|
obs_property_set_modified_callback(preset_list,
|
|
|
|
properties_preset_changed);
|
|
|
|
}
|
|
|
|
|
2015-08-18 11:00:55 -07:00
|
|
|
static void add_manual_properties(obs_properties_t *props)
|
|
|
|
{
|
|
|
|
obs_property_t *resolutions = obs_properties_add_list(props,
|
|
|
|
"resolution", TEXT_RESOLUTION, OBS_COMBO_TYPE_LIST,
|
|
|
|
OBS_COMBO_FORMAT_STRING);
|
|
|
|
obs_property_set_enabled(resolutions, false);
|
|
|
|
obs_property_set_modified_callback(resolutions,
|
|
|
|
properties_resolution_changed);
|
|
|
|
|
|
|
|
obs_property_t *frame_rates = obs_properties_add_frame_rate(props,
|
|
|
|
"frame_rate", TEXT_FRAME_RATE);
|
|
|
|
/*obs_property_frame_rate_option_add(frame_rates, "match obs",
|
|
|
|
TEXT_MATCH_OBS);*/
|
|
|
|
obs_property_set_enabled(frame_rates, false);
|
|
|
|
obs_property_set_modified_callback(frame_rates,
|
|
|
|
properties_frame_rate_changed);
|
|
|
|
|
|
|
|
obs_property_t *input_format = obs_properties_add_list(props,
|
|
|
|
"input_format", TEXT_INPUT_FORMAT,
|
|
|
|
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
|
|
|
|
obs_property_list_add_int(input_format, TEXT_AUTO,
|
|
|
|
INPUT_FORMAT_AUTO);
|
|
|
|
obs_property_set_enabled(input_format, false);
|
|
|
|
obs_property_set_modified_callback(input_format,
|
|
|
|
properties_input_format_changed);
|
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
static obs_properties_t *av_capture_properties(void*)
|
2014-04-28 14:58:08 -07:00
|
|
|
{
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_properties_t *props = obs_properties_create();
|
2014-04-28 14:58:08 -07:00
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_property_t *dev_list = obs_properties_add_list(props, "device",
|
2014-07-09 22:12:57 -07:00
|
|
|
TEXT_DEVICE, OBS_COMBO_TYPE_LIST,
|
2014-04-28 14:58:08 -07:00
|
|
|
OBS_COMBO_FORMAT_STRING);
|
2015-08-14 08:21:34 -07:00
|
|
|
obs_property_list_add_string(dev_list, "", "");
|
2014-04-28 14:58:08 -07:00
|
|
|
for (AVCaptureDevice *dev in [AVCaptureDevice
|
|
|
|
devicesWithMediaType:AVMediaTypeVideo]) {
|
|
|
|
obs_property_list_add_string(dev_list,
|
|
|
|
dev.localizedName.UTF8String,
|
|
|
|
dev.uniqueID.UTF8String);
|
|
|
|
}
|
2014-06-04 10:55:54 -07:00
|
|
|
|
|
|
|
obs_property_set_modified_callback(dev_list,
|
|
|
|
properties_device_changed);
|
2014-04-28 14:58:08 -07:00
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_property_t *use_preset = obs_properties_add_bool(props,
|
2014-07-09 22:12:57 -07:00
|
|
|
"use_preset", TEXT_USE_PRESET);
|
2015-08-18 11:00:55 -07:00
|
|
|
obs_property_set_modified_callback(use_preset,
|
|
|
|
properties_use_preset_changed);
|
2014-04-28 14:58:08 -07:00
|
|
|
|
2015-10-23 02:55:12 -07:00
|
|
|
add_preset_properties(props);
|
2014-04-28 14:58:08 -07:00
|
|
|
|
2015-08-18 11:00:55 -07:00
|
|
|
add_manual_properties(props);
|
|
|
|
|
2015-02-23 23:43:49 -08:00
|
|
|
obs_properties_add_bool(props, "buffering",
|
|
|
|
obs_module_text("Buffering"));
|
|
|
|
|
2014-04-28 14:58:08 -07:00
|
|
|
return props;
|
|
|
|
}
|
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
static void switch_device(av_capture *capture, NSString *uid,
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_data_t *settings)
|
2014-04-28 14:58:08 -07:00
|
|
|
{
|
2014-06-04 10:55:54 -07:00
|
|
|
if (!uid)
|
2014-04-28 14:58:08 -07:00
|
|
|
return;
|
|
|
|
|
2014-06-04 10:55:54 -07:00
|
|
|
if (capture->device)
|
|
|
|
remove_device(capture);
|
2014-04-28 14:58:08 -07:00
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
capture->uid = uid;
|
2014-06-04 10:57:32 -07:00
|
|
|
|
2015-08-14 08:04:41 -07:00
|
|
|
if (!uid.length) {
|
|
|
|
AVLOG(LOG_INFO, "No device selected, stopping capture");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-04 10:55:54 -07:00
|
|
|
AVCaptureDevice *dev = [AVCaptureDevice deviceWithUniqueID:uid];
|
|
|
|
if (!dev) {
|
2015-08-14 08:04:41 -07:00
|
|
|
AVLOG(LOG_WARNING, "Device with unique id '%s' not found",
|
2014-06-04 10:55:54 -07:00
|
|
|
uid.UTF8String);
|
|
|
|
return;
|
2014-04-28 14:58:08 -07:00
|
|
|
}
|
2014-06-04 10:55:54 -07:00
|
|
|
|
2015-08-11 11:12:37 -07:00
|
|
|
capture_device(capture, dev, settings);
|
2014-06-04 10:55:54 -07:00
|
|
|
}
|
|
|
|
|
2015-08-18 11:00:55 -07:00
|
|
|
static void update_preset(av_capture *capture, obs_data_t *settings)
|
2014-06-04 10:55:54 -07:00
|
|
|
{
|
2015-08-18 11:00:55 -07:00
|
|
|
unlock_device(capture);
|
|
|
|
|
2014-06-11 17:30:06 -07:00
|
|
|
NSString *preset = get_string(settings, "preset");
|
|
|
|
if (![capture->device supportsAVCaptureSessionPreset:preset]) {
|
2015-08-14 08:04:41 -07:00
|
|
|
AVLOG(LOG_WARNING, "Preset %s not available",
|
|
|
|
preset.UTF8String);
|
2014-06-11 17:30:06 -07:00
|
|
|
preset = select_preset(capture->device, preset);
|
|
|
|
}
|
|
|
|
|
|
|
|
capture->session.sessionPreset = preset;
|
|
|
|
AVLOG(LOG_INFO, "Selected preset %s", preset.UTF8String);
|
2015-02-23 23:43:49 -08:00
|
|
|
|
2015-08-18 11:00:55 -07:00
|
|
|
start_capture(capture);
|
|
|
|
}
|
|
|
|
|
2015-08-18 11:00:55 -07:00
|
|
|
static void update_manual(av_capture *capture, obs_data_t *settings)
|
|
|
|
{
|
|
|
|
if (init_manual(capture, capture->device, settings))
|
|
|
|
start_capture(capture);
|
|
|
|
}
|
|
|
|
|
2015-08-18 11:00:55 -07:00
|
|
|
static void av_capture_update(void *data, obs_data_t *settings)
|
|
|
|
{
|
|
|
|
auto capture = static_cast<av_capture*>(data);
|
|
|
|
|
|
|
|
NSString *uid = get_string(settings, "device");
|
|
|
|
|
|
|
|
if (!capture->device || ![capture->device.uniqueID isEqualToString:uid])
|
|
|
|
return switch_device(capture, uid, settings);
|
|
|
|
|
2015-08-18 11:00:55 -07:00
|
|
|
if (obs_data_get_bool(settings, "use_preset")) {
|
2015-08-18 11:00:55 -07:00
|
|
|
update_preset(capture, settings);
|
2015-08-18 11:00:55 -07:00
|
|
|
} else {
|
|
|
|
update_manual(capture, settings);
|
|
|
|
}
|
2015-08-18 11:00:55 -07:00
|
|
|
|
2015-02-23 23:43:49 -08:00
|
|
|
av_capture_enable_buffering(capture,
|
|
|
|
obs_data_get_bool(settings, "buffering"));
|
2014-04-28 14:58:08 -07:00
|
|
|
}
|
|
|
|
|
2015-08-11 07:15:21 -07:00
|
|
|
OBS_DECLARE_MODULE()
|
|
|
|
OBS_MODULE_USE_DEFAULT_LOCALE("mac-avcapture", "en-US")
|
|
|
|
|
|
|
|
bool obs_module_load(void)
|
|
|
|
{
|
2015-08-11 11:12:37 -07:00
|
|
|
obs_source_info av_capture_info = {
|
2015-08-11 07:15:21 -07:00
|
|
|
.id = "av_capture_input",
|
|
|
|
.type = OBS_SOURCE_TYPE_INPUT,
|
|
|
|
.output_flags = OBS_SOURCE_ASYNC_VIDEO,
|
|
|
|
.get_name = av_capture_getname,
|
|
|
|
.create = av_capture_create,
|
|
|
|
.destroy = av_capture_destroy,
|
|
|
|
.get_defaults = av_capture_defaults,
|
|
|
|
.get_properties = av_capture_properties,
|
|
|
|
.update = av_capture_update,
|
|
|
|
};
|
2014-04-28 14:58:08 -07:00
|
|
|
|
2015-08-11 07:15:21 -07:00
|
|
|
obs_register_source(&av_capture_info);
|
|
|
|
return true;
|
|
|
|
}
|