2014-06-10 44 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)