Commit 367b4a35 authored by Josep Torra's avatar Josep Torra
Browse files

qtkit: drop the qtkitvideosrc element

The QTKit framework had been deprecated for long in favour of AVFundation
framework and we already have avfvideosrc that provides the same
functionality.

https://bugzilla.gnome.org/show_bug.cgi?id=782078
parent cdfa0897
......@@ -528,7 +528,6 @@ dnl *** plug-ins to exclude ***
AC_CHECK_HEADER(AVFoundation/AVFoundation.h, HAVE_AVFOUNDATION="yes", HAVE_AVFOUNDATION="no", [-])
AC_CHECK_HEADER(MobileCoreServices/MobileCoreServices.h, HAVE_IOS="yes", HAVE_IOS="no", [-])
AC_CHECK_HEADER(VideoToolbox/VideoToolbox.h, HAVE_VIDEOTOOLBOX="yes", HAVE_VIDEOTOOLBOX="no", [-])
AC_CHECK_HEADER(QTKit/QTKit.h, HAVE_QTKIT="yes", HAVE_QTKIT="no", [-])
if test "x$HAVE_VIDEOTOOLBOX" = "xyes"; then
old_LIBS=$LIBS
......@@ -555,10 +554,6 @@ if test "x$HAVE_VIDEOTOOLBOX" = "xyes"; then
AC_DEFINE(HAVE_VIDEOTOOLBOX, 1, [Define if building with VideoToolbox])
fi
AM_CONDITIONAL(HAVE_QTKIT, test "x$HAVE_QTKIT" = "xyes")
if test "x$HAVE_QTKIT" = "xyes"; then
AC_DEFINE(HAVE_QTKIT, 1, [Define if building with QTKit])
fi
dnl disable gst plugins we might not be able to build on this
dnl platform: (ugly but minimally invasive)
dnl FIXME: maybe move to sys, or make work with winsock2
......
......@@ -65,7 +65,6 @@ libgstapplemedia_la_LDFLAGS = \
-Wl,-framework -Wl,AudioToolbox
noinst_HEADERS = \
qtkitvideosrc.h \
avfvideosrc.h \
avsamplevideosink.h \
vtenc.h \
......@@ -104,14 +103,6 @@ libgstapplemedia_la_LDFLAGS += \
endif
if HAVE_QTKIT
libgstapplemedia_la_SOURCES += \
qtkitvideosrc.m
libgstapplemedia_la_LDFLAGS += \
-Wl,-framework -Wl,QTKit
endif
if HAVE_AVFOUNDATION
libgstapplemedia_la_SOURCES += \
......
......@@ -27,9 +27,6 @@
#include "iosassetsrc.h"
#include "iosglmemory.h"
#endif
#ifdef HAVE_QTKIT
#include "qtkitvideosrc.h"
#endif
#ifdef HAVE_AVFOUNDATION
#include "avfvideosrc.h"
#include "avfassetsrc.h"
......@@ -76,11 +73,6 @@ plugin_init (GstPlugin * plugin)
enable_mt_mode ();
#endif
#ifdef HAVE_QTKIT
res = gst_element_register (plugin, "qtkitvideosrc", GST_RANK_SECONDARY,
GST_TYPE_QTKIT_VIDEO_SRC);
#endif
#ifdef HAVE_AVFOUNDATION
res &= gst_element_register (plugin, "avfvideosrc", AV_RANK,
GST_TYPE_AVF_VIDEO_SRC);
......
/*
* Copyright (C) 2009 Ole André Vadla Ravnås <oleavr@soundrop.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_QTKIT_VIDEO_SRC_H__
#define __GST_QTKIT_VIDEO_SRC_H__
#include <gst/base/gstpushsrc.h>
G_BEGIN_DECLS
#define GST_TYPE_QTKIT_VIDEO_SRC \
(gst_qtkit_video_src_get_type ())
#define GST_QTKIT_VIDEO_SRC(obj) \
(G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_QTKIT_VIDEO_SRC, GstQTKitVideoSrc))
#define GST_QTKIT_VIDEO_SRC_CAST(obj) \
((GstQTKitVideoSrc *) (obj))
#define GST_QTKIT_VIDEO_SRC_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_QTKIT_VIDEO_SRC, GstQTKitVideoSrcClass))
#define GST_QTKIT_VIDEO_SRC_IMPL(obj) \
((GstQTKitVideoSrcImpl *) GST_QTKIT_VIDEO_SRC_CAST (obj)->impl)
#define GST_IS_QTKIT_VIDEO_SRC(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_QTKIT_VIDEO_SRC))
#define GST_IS_QTKIT_VIDEO_SRC_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_QTKIT_VIDEO_SRC))
typedef struct _GstQTKitVideoSrc GstQTKitVideoSrc;
typedef struct _GstQTKitVideoSrcClass GstQTKitVideoSrcClass;
struct _GstQTKitVideoSrc
{
GstPushSrc push_src;
gpointer impl;
};
struct _GstQTKitVideoSrcClass
{
GstPushSrcClass parent_class;
};
GType gst_qtkit_video_src_get_type (void);
G_END_DECLS
#endif /* __GST_QTKIT_VIDEO_SRC_H__ */
/*
* Copyright (C) 2009 Ole André Vadla Ravnås <oleavr@soundrop.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "qtkitvideosrc.h"
#import "corevideobuffer.h"
#include "glcontexthelper.h"
#import <QTKit/QTKit.h>
#define DEFAULT_DEVICE_INDEX -1
#define DEVICE_YUV_FOURCC "UYVY"
#define DEVICE_FPS_N 30
#define DEVICE_FPS_D 1
#define FRAME_QUEUE_SIZE 2
GST_DEBUG_CATEGORY (gst_qtkit_video_src_debug);
#define GST_CAT_DEFAULT gst_qtkit_video_src_debug
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (
"video/x-raw, "
"format =" DEVICE_YUV_FOURCC ", "
"width = (int) 640, "
"height = (int) 480, "
"framerate = [0/1, 100/1], "
"pixel-aspect-ratio = (fraction) 1/1"
"; "
"video/x-raw, "
"format =" DEVICE_YUV_FOURCC ", "
"width = (int) 160, "
"height = (int) 120, "
"framerate = [0/1, 100/1], "
"pixel-aspect-ratio = (fraction) 1/1"
"; "
"video/x-raw, "
"format =" DEVICE_YUV_FOURCC ", "
"width = (int) 176, "
"height = (int) 144, "
"framerate = [0/1, 100/1], "
"pixel-aspect-ratio = (fraction) 12/11"
"; "
"video/x-raw, "
"format =" DEVICE_YUV_FOURCC ", "
"width = (int) 320, "
"height = (int) 240, "
"framerate = [0/1, 100/1], "
"pixel-aspect-ratio = (fraction) 1/1"
"; "
"video/x-raw, "
"format =" DEVICE_YUV_FOURCC ", "
"width = (int) 352, "
"height = (int) 288, "
"framerate = [0/1, 100/1], "
"pixel-aspect-ratio = (fraction) 12/11"
";"
)
);
typedef enum _QueueState {
NO_FRAMES = 1,
HAS_FRAME_OR_STOP_REQUEST,
} QueueState;
G_DEFINE_TYPE (GstQTKitVideoSrc, gst_qtkit_video_src, GST_TYPE_PUSH_SRC);
@interface GstQTKitVideoSrcImpl : NSObject {
GstElement *element;
GstBaseSrc *baseSrc;
GstPushSrc *pushSrc;
int deviceIndex;
QTCaptureSession *session;
QTCaptureDeviceInput *input;
QTCaptureDecompressedVideoOutput *output;
QTCaptureDevice *device;
NSConditionLock *queueLock;
NSMutableArray *queue;
BOOL stopRequest;
gint width, height;
gint fps_n, fps_d;
GstClockTime duration;
guint64 offset;
GstGLContextHelper *ctxh;
GstVideoTextureCache *textureCache;
GstVideoInfo outputInfo;
}
- (id)init;
- (id)initWithSrc:(GstPushSrc *)src;
@property int deviceIndex;
- (BOOL)openDevice;
- (void)closeDevice;
- (BOOL)setCaps:(GstCaps *)caps;
- (BOOL)start;
- (BOOL)stop;
- (BOOL)unlock;
- (BOOL)unlockStop;
- (GstCaps *)fixate:(GstCaps *)caps;
- (BOOL)query:(GstQuery *)query;
- (GstStateChangeReturn)changeState:(GstStateChange)transition;
- (GstFlowReturn)create:(GstBuffer **)buf;
- (void)timestampBuffer:(GstBuffer *)buf;
- (void)captureOutput:(QTCaptureOutput *)captureOutput
didOutputVideoFrame:(CVImageBufferRef)videoFrame
withSampleBuffer:(QTSampleBuffer *)sampleBuffer
fromConnection:(QTCaptureConnection *)connection;
@end
@implementation GstQTKitVideoSrcImpl
- (id)init
{
return [self initWithSrc:NULL];
}
- (id)initWithSrc:(GstPushSrc *)src
{
if ((self = [super init])) {
element = GST_ELEMENT_CAST (src);
baseSrc = GST_BASE_SRC_CAST (src);
pushSrc = src;
deviceIndex = DEFAULT_DEVICE_INDEX;
device = nil;
gst_base_src_set_live (baseSrc, TRUE);
gst_base_src_set_format (baseSrc, GST_FORMAT_TIME);
self->ctxh = NULL;
textureCache = NULL;
gst_video_info_init (&outputInfo);
}
return self;
}
@synthesize deviceIndex;
- (BOOL)openDevice
{
NSString *mediaType = QTMediaTypeVideo;
NSError *error = nil;
if (deviceIndex == -1) {
device = [QTCaptureDevice defaultInputDeviceWithMediaType:mediaType];
if (device == nil) {
GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND,
("No video capture devices found"), (NULL));
goto openFailed;
}
} else {
NSArray *devices = [QTCaptureDevice inputDevicesWithMediaType:mediaType];
if (deviceIndex >= [devices count]) {
GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND,
("Invalid video capture device index"), (NULL));
goto openFailed;
}
device = [devices objectAtIndex:deviceIndex];
}
[device retain];
GST_INFO ("Opening '%s'", [[device localizedDisplayName] UTF8String]);
if (![device open:&error]) {
GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND,
("Failed to open device '%s'",
[[device localizedDisplayName] UTF8String]), (NULL));
goto openFailed;
}
return YES;
/* ERRORS */
openFailed:
{
[device release];
device = nil;
return NO;
}
}
- (void)closeDevice
{
g_assert (![session isRunning]);
[session release];
session = nil;
[input release];
input = nil;
[output release];
output = nil;
[device release];
device = nil;
}
- (BOOL)setCaps:(GstCaps *)caps
{
GstStructure *s;
NSDictionary *outputAttrs;
BOOL success;
NSTimeInterval interval;
g_assert (device != nil);
GST_INFO ("setting up session");
s = gst_caps_get_structure (caps, 0);
gst_structure_get_int (s, "width", &width);
gst_structure_get_int (s, "height", &height);
if (!gst_structure_get_fraction (s, "framerate", &fps_n, &fps_d)) {
fps_n = DEVICE_FPS_N;
fps_d = DEVICE_FPS_D;
}
GST_INFO ("got caps %dx%d %d/%d", width, height, fps_n, fps_d);
input = [[QTCaptureDeviceInput alloc] initWithDevice:device];
output = [[QTCaptureDecompressedVideoOutput alloc] init];
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
[output setAutomaticallyDropsLateVideoFrames:YES];
#endif
outputAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithUnsignedInt:k2vuyPixelFormat],
(id)kCVPixelBufferPixelFormatTypeKey,
[NSNumber numberWithUnsignedInt:width],
(id)kCVPixelBufferWidthKey,
[NSNumber numberWithUnsignedInt:height],
(id)kCVPixelBufferHeightKey,
nil
];
[output setPixelBufferAttributes:outputAttrs];
if (fps_n != 0) {
duration = gst_util_uint64_scale (GST_SECOND, fps_d, fps_n);
gst_util_fraction_to_double (fps_d, fps_n, (gdouble *) &interval);
} else {
duration = GST_CLOCK_TIME_NONE;
interval = 0;
}
[output setMinimumVideoFrameInterval:interval];
session = [[QTCaptureSession alloc] init];
success = [session addInput:input
error:nil];
g_assert (success);
success = [session addOutput:output
error:nil];
g_assert (success);
gst_gl_context_helper_ensure_context (ctxh);
GST_INFO_OBJECT (element, "pushing textures, context %p old context %p",
ctxh->context, textureCache ? textureCache->ctx : NULL);
if (textureCache && textureCache->ctx != ctxh->context) {
gst_video_texture_cache_free (textureCache);
textureCache = NULL;
}
textureCache = gst_video_texture_cache_new (ctxh->context);
gst_video_texture_cache_set_format (textureCache, GST_VIDEO_FORMAT_UYVY, caps);
gst_video_info_set_format (&outputInfo, GST_VIDEO_FORMAT_UYVY, width, height);
[output setDelegate:self];
[session startRunning];
return YES;
}
- (BOOL)start
{
NSRunLoop *mainRunLoop;
queueLock = [[NSConditionLock alloc] initWithCondition:NO_FRAMES];
queue = [[NSMutableArray alloc] initWithCapacity:FRAME_QUEUE_SIZE];
stopRequest = NO;
offset = 0;
width = height = 0;
fps_n = 0;
fps_d = 1;
duration = GST_CLOCK_TIME_NONE;
ctxh = gst_gl_context_helper_new (element);
/* this will trigger negotiation and open the device in setCaps */
gst_base_src_start_complete (baseSrc, GST_FLOW_OK);
mainRunLoop = [NSRunLoop mainRunLoop];
if ([mainRunLoop currentMode] == nil) {
/* QTCaptureSession::addInput and QTCaptureSession::addOutput, called from
* setCaps, call NSObject::performSelectorOnMainThread internally. If the
* mainRunLoop is not running we need to run it for a while for those
* methods to complete.
*/
GST_INFO ("mainRunLoop not running");
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
}
return YES;
}
- (BOOL)stop
{
[session stopRunning];
[output setDelegate:nil];
[queueLock release];
queueLock = nil;
[queue release];
queue = nil;
gst_gl_context_helper_free (ctxh);
ctxh = NULL;
if (textureCache)
gst_video_texture_cache_free (textureCache);
textureCache = NULL;
return YES;
}
- (BOOL)query:(GstQuery *)query
{
BOOL result = NO;
if (GST_QUERY_TYPE (query) == GST_QUERY_LATENCY) {
if (device != nil) {
GstClockTime min_latency, max_latency;
min_latency = max_latency = duration; /* for now */
result = YES;
GST_DEBUG_OBJECT (element, "reporting latency of min %" GST_TIME_FORMAT
" max %" GST_TIME_FORMAT,
GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
gst_query_set_latency (query, TRUE, min_latency, max_latency);
}
} else {
result = GST_BASE_SRC_CLASS (gst_qtkit_video_src_parent_class)->query (baseSrc, query);
}
return result;
}
- (BOOL)unlock
{
[queueLock lock];
stopRequest = YES;
[queueLock unlockWithCondition:HAS_FRAME_OR_STOP_REQUEST];
return YES;
}
- (BOOL)unlockStop
{
[queueLock lock];
stopRequest = NO;
[queueLock unlockWithCondition:NO_FRAMES];
return YES;
}
- (GstCaps *)fixate:(GstCaps *)caps
{
GstStructure *structure;
caps = gst_caps_truncate (caps);
structure = gst_caps_get_structure (caps, 0);
if (gst_structure_has_field (structure, "framerate")) {
gst_structure_fixate_field_nearest_fraction (structure, "framerate",
DEVICE_FPS_N, DEVICE_FPS_D);
}
return caps;
}
- (GstStateChangeReturn)changeState:(GstStateChange)transition
{
GstStateChangeReturn ret;
if (transition == GST_STATE_CHANGE_NULL_TO_READY) {
if (![self openDevice])
return GST_STATE_CHANGE_FAILURE;
}
ret = GST_ELEMENT_CLASS (gst_qtkit_video_src_parent_class)->change_state (element, transition);
if (transition == GST_STATE_CHANGE_READY_TO_NULL)
[self closeDevice];
return ret;
}
- (void)captureOutput:(QTCaptureOutput *)captureOutput
didOutputVideoFrame:(CVImageBufferRef)videoFrame
withSampleBuffer:(QTSampleBuffer *)sampleBuffer
fromConnection:(QTCaptureConnection *)connection
{
[queueLock lock];
if (stopRequest) {
[queueLock unlock];
return;
}
GST_INFO ("got new frame");
if ([queue count] == FRAME_QUEUE_SIZE)
[queue removeLastObject];
[queue insertObject:(id)videoFrame
atIndex:0];
[queueLock unlockWithCondition:HAS_FRAME_OR_STOP_REQUEST];
}
- (GstFlowReturn)create:(GstBuffer **)buf
{
CVPixelBufferRef frame;
[queueLock lockWhenCondition:HAS_FRAME_OR_STOP_REQUEST];
if (stopRequest) {
[queueLock unlock];
return GST_FLOW_FLUSHING;
}
frame = (CVPixelBufferRef) [queue lastObject];
CVBufferRetain (frame);
[queue removeLastObject];
[queueLock unlockWithCondition:
([queue count] == 0) ? NO_FRAMES : HAS_FRAME_OR_STOP_REQUEST];
*buf = gst_core_video_buffer_new ((CVBufferRef)frame, &outputInfo, textureCache);
CVBufferRelease (frame);
[self timestampBuffer:*buf];
return GST_FLOW_OK;
}
- (void)timestampBuffer:(GstBuffer *)buf
{
GstClock *clock;
GstClockTime timestamp;
GST_OBJECT_LOCK (element);
clock = GST_ELEMENT_CLOCK (element);
if (clock != NULL) {
gst_object_ref (clock);
timestamp = element->base_time;
} else {
timestamp = GST_CLOCK_TIME_NONE;
}
GST_OBJECT_UNLOCK (element);
if (clock != NULL) {
timestamp = gst_clock_get_time (clock) - timestamp;
if (timestamp > duration)
timestamp -= duration;
else
timestamp = 0;
gst_object_unref (clock);
clock = NULL;
}
GST_BUFFER_OFFSET (buf) = offset++;
GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET (buf) + 1;
GST_BUFFER_TIMESTAMP (buf) = timestamp;