2014-06-10 98 views
4

我一直作为2014 WWDC Video - 'Direct Access to Video Encoding and Decoding'简单提到寻找高和低如何创建一个VTCompressionSession迅速)。如何在swift中创建VTCompressionSession?

下面的代码工作在Objective-C:

#import <Foundation/Foundation.h> 
#import <VideoToolbox/VideoToolbox.h> 

int main(int argc, const char * argv[]) { 
    @autoreleasepool { 
     VTCompressionSessionRef session; 
     VTCompressionSessionCreate(NULL, 500, 500, kCMVideoCodecType_H264, NULL, NULL, NULL, NULL, NULL, &session); 

     NSLog(@"created VTCompressionSession"); 
    } 
    return 0; 
} 

但无论怎样我都试过,我不能找到一种方法,导入VTCompressionSessionCreate迅速

import Foundation 
import VideoToolbox 

VideoToolbox.VTCompressionSessionCreate() 

println("created VTCompressionSession") 

该代码例如中断:Module 'VideoToolbox' has no member named 'VTCompressionSessionCreate'
只需拨打VTCompressionSessionCreate即可创建错误消息Use of unresolved identifier 'VTCompressionSessionCreate'

它看起来像它没有暴露在swift,因为我可以调用像VTCompressionSessionEncodeFrame就好了。我错过了明显的东西吗?

回答

2

我的解决办法是,写目标c函数和通过桥接报头添加到迅速将其暴露:

Compression.h

#import <VideoToolbox/VideoToolbox.h> 
VTCompressionSessionRef CreateCompressionSession(); 

Compression.m

#import <Cocoa/Cocoa.h> 
#import <Foundation/Foundation.h> 
#import <VideoToolbox/VideoToolbox.h> 

VTCompressionSessionRef CreateCompressionSession(){ 
    NSLog(@"CreateCompressionSession"); 
    VTCompressionSessionRef session; 
    VTCompressionSessionCreate(NULL, 500, 500, kCMVideoCodecType_H264, NULL, NULL, NULL, NULL, NULL, &session); 
    return session; 
} 

流-Bridging-Header.h

#import "CompressionSession.h" 

现在你可以在迅速运行下面的代码:

​​
0

这是我如何在迅速做到了:

var session: Unmanaged<VTCompressionSession>? 
VTCompressionSessionCreate(nil, 320, 200, CMVideoCodecType(kCMVideoCodecType_H264), nil, nil, nil, nil, nil, &session) 
compressionSession = session?.takeRetainedValue() 

其中

var compressionSession: VTCompressionSessionRef? 

不要忘记import CoreMedia

+0

这个例子不再是(如果它的工作)适用于OS X 10.10或iOS 8传递零中的' VTCompressionSessionCreate' outputCallback参数(第8个参数)给出错误代码-12092。 –

+0

以上将给出类型错误。在xcode的7 /迅速2以下可能的工作: VAR会话:UnsafeMutablePointer <?VTCompressionSession> =零 设状态= VTCompressionSessionCreate(kCFAllocatorDefault,的Int32(pixelWidth)的Int32(pixelHeight),kCMVideoCodecType_H264,零,零,零/ *使用默认的* /,nil,nil,session) –

0

对于Swift 3:

var compressionSesionOut = UnsafeMutablePointer<VTCompressionSession?>.allocate(capacity: 1) 
VTCompressionSessionCreate(nil, 100, 100, kCMVideoCodecType_H264, nil, nil, nil, nil, nil, compressionSesionOut) 

然后,你可以这样访问它,例如:

let vtCompressionSession: VTCompressionSession = compressionSesionOut.pointee.unsafelyUnwrapped 
VTSessionSetProperty(vtCompressionSession, kVTCompressionPropertyKey_RealTime, kCFBooleanTrue)