Skip to content

Instantly share code, notes, and snippets.

@jacobsologub
Created November 16, 2019 06:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobsologub/d69336988f7def21e13a66a2b01e8a18 to your computer and use it in GitHub Desktop.
Save jacobsologub/d69336988f7def21e13a66a2b01e8a18 to your computer and use it in GitHub Desktop.
libhypno objective-c wrapper(s)
//
// libhypno_Platform.h
// HYPNOApp
//
// Created by Jacob Sologub on 11/15/19.
// Copyright © 2019 Jacob Sologub. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
typedef void (^PlatformLogBlock) (NSString* message);
@interface hypno_Platform : NSObject
/**
* Initializes the Platform.
*
* This method should be called somewhere in the beginning of your
* application's run loop before interacting with the hypno library.
*/
+ (void) initializePlatform;
/**
* Shuts down the Platform.
*
* This method should be called right before your application terminates.
*/
+ (void) shutdownPlatform;
/**
* You can assign a lambda to this callback object to have it called when
* there's a new system log.
*/
+ (void) setOnSystemLog: (PlatformLogBlock) block;
/**
* You can assign a lambda to this callback object to have it called when
* there's a new console log.
*/
+ (void) setOnConsoleLog: (PlatformLogBlock) block;
@end
NS_ASSUME_NONNULL_END
//
// libhypno_Platform.m
// HYPNOApp
//
// Created by Jacob Sologub on 11/15/19.
// Copyright © 2019 Jacob Sologub. All rights reserved.
//
#import "hypno_Platform.h"
#include <libhypno/hypno.h>
namespace hypno {
static std::pair<PlatformLogBlock, PlatformLogBlock> platformLogBlocks{};
}
@implementation hypno_Platform
+ (void) initializePlatform {
hypno::Platform::initialize();
hypno::Platform::instance()->onSystemLog = [] (std::string_view message) {
if (hypno::platformLogBlocks.first) {
hypno::platformLogBlocks.first ([NSString stringWithUTF8String: message.data()]);
}
};
hypno::Platform::instance()->onConsoleLog = [] (std::string_view message) {
if (hypno::platformLogBlocks.second) {
hypno::platformLogBlocks.second ([NSString stringWithUTF8String: message.data()]);
}
};
}
+ (void) shutdownPlatform {
hypno::Platform::shutdown();
}
+ (void) setOnSystemLog: (PlatformLogBlock) onSystemLog {
hypno::platformLogBlocks.first = onSystemLog;
}
+ (void) setOnConsoleLog: (PlatformLogBlock) onConsoleLog {
hypno::platformLogBlocks.second = onConsoleLog;
}
@end
//
// libhypno_Video.h
// HYPNOApp
//
// Created by Jacob Sologub on 11/15/19.
// Copyright © 2019 Jacob Sologub. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface hypno_Configuration : NSObject
@property (nonatomic, retain) NSURL* script;
@property (nonatomic, retain) NSArray<NSURL*>* cameraAssets;
@property (nonatomic, retain) NSArray<NSURL*>* otherAssets;
@end
@interface hypno_Video : NSObject
/**
* Creates a Video object with a specified configuration.
*
* @see hypno::Configuration
*/
+ (instancetype) create: (hypno_Configuration*) configuration;
/**
* Returns a string describing an error or nil if there was no error.
*/
@property (nonatomic, readonly) NSString* error;
/**
* Returns the AVComposition object associated with this Video.
*
* @see https://developer.apple.com/documentation/avfoundation/avcomposition?language=objc
*/
@property (nonatomic, readonly) AVComposition* composition;
/**
* Returns the AVVideoComposition object associated with this Video.
*
* @see https://developer.apple.com/documentation/avfoundation/avvideocomposition?language=objc
*/
@property (nonatomic, readonly) AVVideoComposition* videoComposition;
/**
* Returns the AVAudioMix object associated with this Video.
*
* @see https://developer.apple.com/documentation/avfoundation/avaudiomix?language=objc
*/
@property (nonatomic, readonly) AVAudioMix* audioMix;
@end
NS_ASSUME_NONNULL_END
//
// libhypno_Video.m
// HYPNOApp
//
// Created by Jacob Sologub on 11/15/19.
// Copyright © 2019 Jacob Sologub. All rights reserved.
//
#import "hypno_Video.h"
#include <libhypno/hypno.h>
@implementation hypno_Configuration
@synthesize script;
@synthesize cameraAssets;
@synthesize otherAssets;
@end
@interface hypno_Video()
@property (nonatomic, assign) std::shared_ptr<hypno::Video> video;
@end
@implementation hypno_Video
@synthesize video;
@dynamic error;
@dynamic composition;
@dynamic videoComposition;
@dynamic audioMix;
- (id) initWithConfiguration: (hypno_Configuration*) configuration {
if ((self = [super init]) != nil) {
auto config = hypno::Configuration { configuration.script.path.UTF8String };
for (NSURL* item in configuration.cameraAssets) {
config.cameraAssets.emplace_back (item.path.UTF8String);
}
video = hypno::Video::create (config);
}
return self;
}
- (void) dealloc {
}
+ (instancetype) create: (hypno_Configuration*) configuration {
return [[hypno_Video alloc] initWithConfiguration: configuration];
}
- (NSString*) error {
if (auto err = video->getError()) {
return [NSString stringWithUTF8String: err->c_str()];
}
return nil;
}
- (AVComposition*) composition {
return video->getComposition();
}
- (AVVideoComposition*) videoComposition {
return video->getVideoComposition();
}
- (AVAudioMix*) audioMix {
return video->getAudioMix();
}
@end
@jacobsologub
Copy link
Author

Using libhypno objective-c wrapper(s)

// initialize HYPNO platform inside your application:didFinishLaunchingWithOptions method
[hypno_Platform initializePlatform];
// shutdown HYPNO platform inside your applicationWillTerminate method
[hypno_Platform shutdownPlatform];
// create a hypno video

hypno_Configuration* configuration = [hypno_Configuration new];
configuration.script = [NSURL fileURLWithPath: scriptFile];
configuration.cameraAssets = @[ movieUrl ];

video = [hypno_Video create: configuration];
if (video.error) {
    NSLog (@"%@", video.error);
}
else {
    AVPlayerItem* compositionPlayerItem = [AVPlayerItem playerItemWithAsset: video.composition];
    compositionPlayerItem.videoComposition = video.videoComposition;
    compositionPlayerItem.audioMix = video.audioMix;
    AVPlayer* player = [AVPlayer playerWithPlayerItem: compositionPlayerItem];
    // etc.
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment