How to quickly create a VTCompressionSession?

I looked high and low how to create a VTCompressionSession (in fast ), as summarized in 2014 WWDC Video - “Direct access to video encoding and decoding” .

The following code works in 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;
}

But no matter what I tried, I cannot find a way to import VTCompressionSessionCreateinto swift .

import Foundation
import VideoToolbox

VideoToolbox.VTCompressionSessionCreate()

println("created VTCompressionSession")

This code, for example, is broken down into: Module 'VideoToolbox' has no member named 'VTCompressionSessionCreate'.
Just a call VTCompressionSessionCreatecreates an error message Use of unresolved identifier 'VTCompressionSessionCreate'.

It seems that the API does not appear in quickly , as I can just call methods like VTCompressionSessionEncodeFrame. Am I missing something?

+4
3

, objective-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;
}

--header.h

#import "CompressionSession.h"

:

import Cocoa
import Foundation
import VideoToolbox

let session = CreateCompressionSession()
+2

:

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

For Swift 3:

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

Then you can access it, for example:

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

Source: https://habr.com/ru/post/1543938/


All Articles