├── .gitignore ├── LICENSE ├── README.md ├── VideoCore.podspec ├── api └── iOS │ ├── VCPreviewView.h │ ├── VCPreviewView.mm │ ├── VCSimpleSession.h │ └── VCSimpleSession.mm ├── demo └── SampleBroadcaster │ ├── Podfile │ ├── Podfile.lock │ ├── SampleBroadcaster.xcodeproj │ ├── project.pbxproj │ └── xcuserdata │ │ └── jinchudarwin.xcuserdatad │ │ └── xcschemes │ │ ├── SampleBroadcaster.xcscheme │ │ └── xcschememanagement.plist │ └── SampleBroadcaster │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json │ ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard │ ├── Info.plist │ ├── LiveKeeper.h │ ├── LiveKeeper.m │ ├── LivingVC.h │ ├── LivingVC.m │ ├── PrepareLiveVC.h │ ├── PrepareLiveVC.m │ ├── Vendor │ └── YYKit │ │ ├── YYTextWeakProxy.h │ │ └── YYTextWeakProxy.m │ └── main.m ├── docs ├── Architecture.md ├── Mixers.md ├── Outputs.md ├── SettingUpAGraph.md ├── Sources.md └── Transforms.md ├── filters ├── Basic │ ├── AntiqueVideoFilter.cpp │ ├── AntiqueVideoFilter.hpp │ ├── BasicVideoFilterBGRA.cpp │ ├── BasicVideoFilterBGRA.h │ ├── BasicVideoFilterBGRAinYUVAout.cpp │ ├── BasicVideoFilterBGRAinYUVAout.h │ ├── BeautyVideoFilter.cpp │ ├── BeautyVideoFilter.hpp │ ├── FisheyeVideoFilter.cpp │ ├── FisheyeVideoFilter.h │ ├── GlowVideoFilter.cpp │ ├── GlowVideoFilter.h │ ├── GrayscaleVideoFilter.cpp │ ├── GrayscaleVideoFilter.h │ ├── InvertColorsVideoFilter.cpp │ ├── InvertColorsVideoFilter.h │ ├── SepiaVideoFilter.cpp │ └── SepiaVideoFilter.h ├── FilterFactory.cpp ├── FilterFactory.h ├── IFilter.hpp └── IVideoFilter.hpp ├── mixers ├── Apple │ ├── AudioMixer.cpp │ └── AudioMixer.h ├── GenericAudioMixer.cpp ├── GenericAudioMixer.h ├── IAudioMixer.hpp ├── IMixer.hpp ├── IVideoMixer.hpp └── iOS │ ├── GLESVideoMixer.h │ └── GLESVideoMixer.mm ├── rtmp ├── RTMPSession.cpp ├── RTMPSession.h └── RTMPTypes.h ├── sources ├── Apple │ ├── PixelBufferSource.h │ └── PixelBufferSource.mm ├── ISource.hpp └── iOS │ ├── CameraSource.h │ ├── CameraSource.mm │ ├── GLESUtil.h │ ├── MicSource.h │ └── MicSource.mm ├── stream ├── Apple │ ├── StreamSession.h │ └── StreamSession.mm ├── IStreamSession.hpp ├── IThroughputAdaptation.h ├── TCPThroughputAdaptation.cpp └── TCPThroughputAdaptation.h ├── system ├── Buffer.hpp ├── JobQueue.hpp ├── Logger.cpp ├── Logger.hpp ├── PreBuffer.cpp ├── PreBuffer.hpp ├── h264 │ ├── Golomb.cpp │ └── Golomb.h ├── pixelBuffer │ ├── Apple │ │ ├── PixelBuffer.cpp │ │ └── PixelBuffer.h │ ├── GenericPixelBuffer.cpp │ ├── GenericPixelBuffer.h │ └── IPixelBuffer.hpp └── util.h └── transforms ├── Apple ├── AppleH264Encode.h ├── AppleH264Encode.mm ├── MP4Multiplexer.h └── MP4Multiplexer.mm ├── AspectTransform.cpp ├── AspectTransform.h ├── IEncoder.hpp ├── IMetaData.hpp ├── IOutput.hpp ├── IOutputSession.hpp ├── ITransform.hpp ├── PositionTransform.cpp ├── PositionTransform.h ├── RTMP ├── AACPacketizer.cpp ├── AACPacketizer.h ├── H264Packetizer.cpp └── H264Packetizer.h ├── Split.cpp ├── Split.h └── iOS ├── AACEncode.cpp ├── AACEncode.h ├── iOSH264Encode.h └── iOSH264Encode.mm /.gitignore: -------------------------------------------------------------------------------- 1 | # CocoaPods 2 | demo/SampleBroadcaster/Pods 3 | # Xcode 4 | *.build 5 | *.dia 6 | *.tlog 7 | *.o 8 | *.d 9 | *.obj 10 | *.log 11 | *.cache 12 | *.dll 13 | *.lib 14 | *.idb 15 | *.pdb 16 | .DS_Store 17 | build/ 18 | *.xcuserstate 19 | 20 | *.xcworkspacedata 21 | 22 | 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 James G. Hurley 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. -------------------------------------------------------------------------------- /VideoCore.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = "VideoCore" 3 | s.version = "0.4.1" 4 | s.summary = "An audio and video manipulation and streaming pipeline with support for RTMP." 5 | s.description = <<-DESC 6 | This is a work-in-progress library with the 7 | intention of being an audio and video manipulation 8 | and streaming pipeline for iOS. 9 | DESC 10 | s.homepage = "https://github.com/dourgulf/VideoCore" 11 | s.license = 'MIT' 12 | s.authors = { "dawenhing" => "dawenhing@outlook.com" } 13 | s.source = { :git => "https://github.com/dourgulf/VideoCore", :tag => s.version.to_s } 14 | 15 | s.requires_arc = false 16 | 17 | s.header_dir = 'videocore' 18 | s.header_mappings_dir = '.' 19 | 20 | s.source_files = [ 'mixers/**/*.h*', 'mixers/**/*.cpp', 'mixers/**/*.m*', 21 | 'rtmp/**/*.h*', 'rtmp/**/*.cpp', 'rtmp/**/*.m*', 22 | 'sources/**/*.h*', 'sources/**/*.cpp', 'sources/**/*.m*', 23 | 'stream/**/*.h*', 'stream/**/*.cpp', 'stream/**/*.m*', 24 | 'system/**/*.h*', 'system/**/*.cpp', 'system/**/*.m*', 25 | 'transforms/**/*.h*', 'transforms/**/*.cpp', 'transforms/**/*.m*', 26 | 'api/**/*.h*', 'api/**/*.m*', 27 | 'filters/**/*.cpp', 'filters/**/*.h*' ] 28 | 29 | s.frameworks = [ 'VideoToolbox', 'AudioToolbox', 'AVFoundation', 'CFNetwork', 'CoreMedia', 30 | 'CoreVideo', 'OpenGLES', 'Foundation', 'CoreGraphics' ] 31 | 32 | s.libraries = 'c++' 33 | 34 | s.dependency 'glm', '~> 0.9.4.6' 35 | s.dependency 'UriParser-cpp', '~> 0.1.3' 36 | 37 | s.xcconfig = { } 38 | 39 | s.ios.deployment_target = '8.0' 40 | 41 | # Before we can get OS X deployment working, we'll need to use sub-specs to 42 | # separate out the source files for OS X vs. iOS 43 | #s.osx.deployment_target = '10.7' 44 | end 45 | -------------------------------------------------------------------------------- /api/iOS/VCPreviewView.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | 26 | #import 27 | 28 | @interface VCPreviewView : UIView 29 | 30 | - (void) drawFrame: (CVPixelBufferRef) pixelBuffer mirrored:(BOOL)mirrored; 31 | 32 | @end 33 | -------------------------------------------------------------------------------- /demo/SampleBroadcaster/Podfile: -------------------------------------------------------------------------------- 1 | platform :ios, '8.0' 2 | target 'SampleBroadcaster' do 3 | pod 'AFNetworking', '~>3.1.0' 4 | pod 'Masonry', '~>1.0.0' 5 | pod 'ReactiveCocoa', '~>2.5' 6 | pod 'VideoCore', path: '../../../VideoCore' 7 | end -------------------------------------------------------------------------------- /demo/SampleBroadcaster/Podfile.lock: -------------------------------------------------------------------------------- 1 | PODS: 2 | - AFNetworking (3.1.0): 3 | - AFNetworking/NSURLSession (= 3.1.0) 4 | - AFNetworking/Reachability (= 3.1.0) 5 | - AFNetworking/Security (= 3.1.0) 6 | - AFNetworking/Serialization (= 3.1.0) 7 | - AFNetworking/UIKit (= 3.1.0) 8 | - AFNetworking/NSURLSession (3.1.0): 9 | - AFNetworking/Reachability 10 | - AFNetworking/Security 11 | - AFNetworking/Serialization 12 | - AFNetworking/Reachability (3.1.0) 13 | - AFNetworking/Security (3.1.0) 14 | - AFNetworking/Serialization (3.1.0) 15 | - AFNetworking/UIKit (3.1.0): 16 | - AFNetworking/NSURLSession 17 | - glm (0.9.4.6) 18 | - Masonry (1.0.2) 19 | - ReactiveCocoa (2.5): 20 | - ReactiveCocoa/UI (= 2.5) 21 | - ReactiveCocoa/Core (2.5): 22 | - ReactiveCocoa/no-arc 23 | - ReactiveCocoa/no-arc (2.5) 24 | - ReactiveCocoa/UI (2.5): 25 | - ReactiveCocoa/Core 26 | - UriParser-cpp (0.1.3) 27 | - VideoCore (0.4.1): 28 | - glm (~> 0.9.4.6) 29 | - UriParser-cpp (~> 0.1.3) 30 | 31 | DEPENDENCIES: 32 | - AFNetworking (~> 3.1.0) 33 | - Masonry (~> 1.0.0) 34 | - ReactiveCocoa (~> 2.5) 35 | - VideoCore (from `../../../VideoCore`) 36 | 37 | EXTERNAL SOURCES: 38 | VideoCore: 39 | :path: "../../../VideoCore" 40 | 41 | SPEC CHECKSUMS: 42 | AFNetworking: 5e0e199f73d8626b11e79750991f5d173d1f8b67 43 | glm: e22c8d9df10404059b19c2952f9fa44d9a9f3056 44 | Masonry: 7c429b56da9d4ee0bbb3ed77a5ea710d6a5df39e 45 | ReactiveCocoa: e2db045570aa97c695e7aa97c2bcab222ae51f4a 46 | UriParser-cpp: cbbe00080ee432ec4833dca863a7fc83adc9b01a 47 | VideoCore: cb764754eefb2c550eac76cc53df8d24ea69edee 48 | 49 | PODFILE CHECKSUM: 839ac29da976d8b9d8d5c88e51c52fef80fdab0d 50 | 51 | COCOAPODS: 1.1.1 52 | -------------------------------------------------------------------------------- /demo/SampleBroadcaster/SampleBroadcaster.xcodeproj/xcuserdata/jinchudarwin.xcuserdatad/xcschemes/SampleBroadcaster.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /demo/SampleBroadcaster/SampleBroadcaster.xcodeproj/xcuserdata/jinchudarwin.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | SampleBroadcaster.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | D090E6DF1D1E61DD0006AB52 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /demo/SampleBroadcaster/SampleBroadcaster/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // SampleBroadcaster 4 | // 5 | // Created by jinchu darwin on 16/6/25. 6 | // Copyright © 2016年 dawenhing.top. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /demo/SampleBroadcaster/SampleBroadcaster/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // SampleBroadcaster 4 | // 5 | // Created by jinchu darwin on 16/6/25. 6 | // Copyright © 2016年 dawenhing.top. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | - (void)applicationWillResignActive:(UIApplication *)application { 24 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 25 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 26 | } 27 | 28 | - (void)applicationDidEnterBackground:(UIApplication *)application { 29 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 30 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 31 | } 32 | 33 | - (void)applicationWillEnterForeground:(UIApplication *)application { 34 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 35 | } 36 | 37 | - (void)applicationDidBecomeActive:(UIApplication *)application { 38 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 39 | } 40 | 41 | - (void)applicationWillTerminate:(UIApplication *)application { 42 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /demo/SampleBroadcaster/SampleBroadcaster/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /demo/SampleBroadcaster/SampleBroadcaster/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /demo/SampleBroadcaster/SampleBroadcaster/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UIBackgroundModes 26 | 27 | audio 28 | remote-notification 29 | 30 | UILaunchStoryboardName 31 | LaunchScreen 32 | UIMainStoryboardFile 33 | Main 34 | UIRequiredDeviceCapabilities 35 | 36 | armv7 37 | 38 | UISupportedInterfaceOrientations 39 | 40 | UIInterfaceOrientationPortrait 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /demo/SampleBroadcaster/SampleBroadcaster/LiveKeeper.h: -------------------------------------------------------------------------------- 1 | // 2 | // LiveKeeper.h 3 | // SampleBroadcaster 4 | // 5 | // Created by jinchu darwin on 16/7/13. 6 | // Copyright © 2016年 dawenhing.top. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "ReactiveCocoa.h" 11 | 12 | @interface LiveKeeper : NSObject 13 | 14 | - (instancetype)initWithURL:(NSURL *)pushURL; 15 | 16 | @property (assign, nonatomic) NSInteger maxRecoverCount; // 最多连续恢复多少次? 17 | @property (assign, nonatomic, getter=isUserStartedLive) BOOL userStartedLive; // 用户是否已经开播(不表示推流的任何状态情况) 18 | @property (assign, nonatomic, getter=isRecoving) BOOL recoving; // 是否正在恢复开播 19 | @property (assign, nonatomic, getter=isPushBroken) BOOL pushBroken; // 推流是否中断了 20 | 21 | @property (readonly, nonatomic) RACSubject *shoultRecover; // 需要回复直播的信号 22 | @property (readonly, nonatomic) RACSubject *recoverToomuch; // 连续失败太多, 无法恢复的信号 23 | @property (readonly, nonatomic) RACSubject *recoverTimeout; // 恢复超时 24 | 25 | @end 26 | -------------------------------------------------------------------------------- /demo/SampleBroadcaster/SampleBroadcaster/LiveKeeper.m: -------------------------------------------------------------------------------- 1 | // 2 | // LiveKeeper.m 3 | // SampleBroadcaster 4 | // 5 | // Created by jinchu darwin on 16/7/13. 6 | // Copyright © 2016年 dawenhing.top. All rights reserved. 7 | // 8 | 9 | #import "LiveKeeper.h" 10 | #import "AFNetworkReachabilityManager.h" 11 | #import "YYTextWeakProxy.h" 12 | 13 | // 默认最多连续重连 14 | static const NSUInteger kDefaultMaxRecoverCount = 3; 15 | // 2分钟 16 | static const NSTimeInterval kBrokenRecoverTimeout = 2 * 60; 17 | 18 | @interface LiveKeeper () 19 | 20 | @property (copy, nonatomic) NSURL *pushURL; 21 | @property (strong, nonatomic) RACSubject *shoultRecover; 22 | @property (strong, nonatomic) RACSubject *recoverToomuch; 23 | @property (strong, nonatomic) RACSubject *recoverTimeout; 24 | @property (weak, nonatomic) NSTimer *recoverTimer; 25 | 26 | @end 27 | 28 | @implementation LiveKeeper { 29 | NSInteger _recoverCounter; 30 | AFNetworkReachabilityManager *_reachability; 31 | NSTimer *_brokenRecoverTimer; 32 | } 33 | 34 | - (instancetype)initWithURL:(NSURL *)pushURL; 35 | { 36 | self = [super init]; 37 | if (self) { 38 | _userStartedLive = NO; 39 | _recoving = NO; 40 | _pushBroken = NO; 41 | _recoverCounter = 0; 42 | _maxRecoverCount = kDefaultMaxRecoverCount; 43 | _pushURL = pushURL; 44 | [self createSignals]; 45 | [self startNetworkMonitor]; 46 | } 47 | return self; 48 | } 49 | 50 | - (void)dealloc { 51 | [self stopNetworkMonitor]; 52 | [self stopBrokenRecoverTimer]; 53 | } 54 | 55 | - (BOOL)needRecover { 56 | if (self.isPushBroken && self.isUserStartedLive && !self.isRecoving) { 57 | return YES; 58 | } 59 | return NO; 60 | } 61 | 62 | - (BOOL)isRecoverToomuch { 63 | return _recoverCounter > _maxRecoverCount; 64 | } 65 | 66 | - (void)setRecoving:(BOOL)recoving { 67 | _recoving = recoving; 68 | if (recoving) { 69 | _recoverCounter++; 70 | [self stopBrokenRecoverTimer]; 71 | } 72 | } 73 | 74 | - (void)setPushBroken:(BOOL)pushBroken { 75 | _pushBroken = pushBroken; 76 | if (!pushBroken) { 77 | // 重连已经恢复了, 重置次数 78 | _recoverCounter = 0; 79 | } 80 | else { 81 | [self startBrokenRecoverTimer]; 82 | } 83 | } 84 | 85 | - (void)createSignals { 86 | self.shoultRecover = [RACSubject subject]; 87 | self.recoverToomuch = [RACSubject subject]; 88 | self.recoverTimeout = [RACSubject subject]; 89 | } 90 | 91 | - (void)startBrokenRecoverTimer { 92 | YYTextWeakProxy *proxy = [YYTextWeakProxy proxyWithTarget:self]; 93 | _brokenRecoverTimer = [NSTimer scheduledTimerWithTimeInterval:kBrokenRecoverTimeout 94 | target:proxy 95 | selector:@selector(onBrokenRecoverTimeout) 96 | userInfo:nil 97 | repeats:NO]; 98 | } 99 | - (void)stopBrokenRecoverTimer { 100 | [_brokenRecoverTimer invalidate]; 101 | _brokenRecoverTimer = nil; 102 | } 103 | 104 | - (void)onBrokenRecoverTimeout { 105 | [self.recoverTimeout sendNext:@(YES)]; 106 | } 107 | 108 | - (void)startNetworkMonitor { 109 | _reachability = [AFNetworkReachabilityManager managerForDomain:[self.pushURL host]]; 110 | [_reachability startMonitoring]; 111 | @weakify(self); 112 | [_reachability setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { 113 | @strongify(self); 114 | [self checkRecoverStatus]; 115 | }]; 116 | } 117 | 118 | - (void)stopNetworkMonitor { 119 | [_reachability stopMonitoring]; 120 | } 121 | 122 | - (void)checkRecoverStatus { 123 | if ([_reachability isReachable]) { 124 | if ([self isRecoverToomuch]) { 125 | [self.recoverToomuch sendNext:@(YES)]; 126 | } 127 | else if ([self needRecover]) { 128 | [self.shoultRecover sendNext:@(YES)]; 129 | } 130 | } 131 | } 132 | 133 | @end 134 | -------------------------------------------------------------------------------- /demo/SampleBroadcaster/SampleBroadcaster/LivingVC.h: -------------------------------------------------------------------------------- 1 | // 2 | // LivingVC.h 3 | // SampleBroadcaster 4 | // 5 | // Created by jinchu darwin on 16/7/5. 6 | // Copyright © 2016年 dawenhing.top. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface LivingVC : UIViewController 12 | 13 | @property (copy, nonatomic) NSString *pushURL; 14 | @property (copy, nonatomic) NSString *streamName; 15 | @property (assign, nonatomic) int bitrate; 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /demo/SampleBroadcaster/SampleBroadcaster/LivingVC.m: -------------------------------------------------------------------------------- 1 | // 2 | // LivingVC.m 3 | // SampleBroadcaster 4 | // 5 | // Created by jinchu darwin on 16/7/5. 6 | // Copyright © 2016年 dawenhing.top. All rights reserved. 7 | // 8 | 9 | #import "LivingVC.h" 10 | 11 | #import 12 | #import "Masonry.h" 13 | #import "LiveKeeper.h" 14 | 15 | @interface LivingVC() 16 | 17 | @property (strong, nonatomic) VCSimpleSession *liveSession; 18 | @property (weak, nonatomic) IBOutlet UILabel *liveStatusLabel; 19 | @property (strong, nonatomic) LiveKeeper *liveKeeper; 20 | 21 | @end 22 | 23 | @implementation LivingVC 24 | 25 | - (void)viewDidLoad { 26 | [super viewDidLoad]; 27 | [self setupLiveKeeper]; 28 | [self setupSession]; 29 | } 30 | 31 | - (void)viewDidAppear:(BOOL)animated{ 32 | [super viewDidAppear:animated]; 33 | 34 | [self startPushVideo]; 35 | } 36 | 37 | - (void)viewWillDisappear:(BOOL)animated { 38 | [super viewWillDisappear:animated]; 39 | 40 | [self stopPushVideo]; 41 | } 42 | 43 | - (void)didReceiveMemoryWarning { 44 | [super didReceiveMemoryWarning]; 45 | // Dispose of any resources that can be recreated. 46 | } 47 | 48 | - (void)setupLiveKeeper { 49 | self.liveKeeper = [[LiveKeeper alloc] initWithURL:[NSURL URLWithString:self.pushURL]]; 50 | 51 | [self.liveKeeper.recoverTimeout subscribeNext:^(id x) { 52 | [self updateStatusText:@"Recover timeout"]; 53 | }]; 54 | 55 | [self.liveKeeper.shoultRecover subscribeNext:^(id x) { 56 | [self updateStatusText:@"Recovering"]; 57 | self.liveKeeper.recoving = YES; 58 | [self.liveSession endRtmpSession]; 59 | [self.liveSession startRtmpSessionWithURL:self.pushURL andStreamKey:self.streamName]; 60 | }]; 61 | 62 | [self.liveKeeper.recoverToomuch subscribeNext:^(id x) { 63 | [self updateStatusText:@"Recover failed"]; 64 | }]; 65 | } 66 | 67 | - (void)setupSession { 68 | CGSize videoSize = [UIScreen mainScreen].bounds.size; 69 | 70 | // 取偶数大小,避免VideoCore做除法运算的误差导致的一个绿色边框的现象 71 | if ((int)videoSize.width % 2 == 1) { 72 | videoSize.width += 1; 73 | } 74 | if ((int)videoSize.height %2 == 1) { 75 | videoSize.height += 1; 76 | } 77 | self.liveSession = [[VCSimpleSession alloc] initWithVideoSize:videoSize 78 | frameRate:25 79 | bitrate:1000 * 1000 80 | useInterfaceOrientation:YES 81 | cameraState:VCCameraStateFront]; 82 | self.liveSession.delegate = self; 83 | [self.view insertSubview:self.liveSession.previewView atIndex:0]; 84 | [self.liveSession.previewView mas_makeConstraints:^(MASConstraintMaker *make) { 85 | make.edges.equalTo(self.view); 86 | }]; 87 | } 88 | 89 | - (void)releaseSession { 90 | self.liveSession = nil; 91 | } 92 | 93 | - (void)startPushVideo { 94 | if (self.liveSession == nil) { 95 | return ; 96 | } 97 | 98 | NSLog(@"Streaming:%@/%@ with bitrate:%d", self.pushURL, self.streamName, self.bitrate); 99 | if (self.bitrate > 0) { 100 | self.liveSession.bitrate = self.bitrate * 1000; 101 | self.liveSession.useAdaptiveBitrate = NO; 102 | } 103 | else { 104 | self.liveSession.bitrate = 1000 * 1000; 105 | self.liveSession.useAdaptiveBitrate = YES; 106 | } 107 | self.liveKeeper.userStartedLive = YES; 108 | [self.liveSession startRtmpSessionWithURL:self.pushURL andStreamKey:self.streamName]; 109 | } 110 | 111 | - (void)stopPushVideo { 112 | self.liveKeeper.userStartedLive = NO; 113 | [self.liveSession endRtmpSession]; 114 | } 115 | 116 | - (void)updateStatusText:(NSString *)text { 117 | self.liveStatusLabel.text = [NSString stringWithFormat:@"RTMP Status:%@", text]; 118 | 119 | } 120 | #pragma mark - Video session 121 | - (void)connectionStatusChanged:(VCSessionState) state { 122 | dispatch_async(dispatch_get_main_queue(), ^{ 123 | NSString *stateText = @""; 124 | switch (state) { 125 | case VCSessionStatePreviewStarted: 126 | stateText = @"Preview"; 127 | break; 128 | case VCSessionStateStarting: 129 | stateText = @"Starting"; 130 | break; 131 | case VCSessionStateStarted: 132 | stateText = @"Started"; 133 | self.liveKeeper.recoving = NO; 134 | self.liveKeeper.pushBroken = NO; 135 | break; 136 | case VCSessionStateEnded: 137 | stateText = @"Ended"; 138 | self.liveKeeper.pushBroken = YES; 139 | break; 140 | case VCSessionStateError: 141 | stateText = @"Error"; 142 | self.liveKeeper.pushBroken = YES; 143 | break; 144 | default: 145 | stateText = @"Unknown"; 146 | break; 147 | } 148 | [self updateStatusText:stateText]; 149 | }); 150 | } 151 | 152 | #pragma mark CameraSource delegate 153 | - (void)didAddCameraSource:(VCSimpleSession*)session { 154 | NSLog(@"didAddCameraSource"); 155 | } 156 | 157 | - (void) detectedThroughput: (NSInteger)throughputInBytesPerSecond videoRate:(NSInteger) rate { 158 | } 159 | 160 | @end 161 | -------------------------------------------------------------------------------- /demo/SampleBroadcaster/SampleBroadcaster/PrepareLiveVC.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // SampleBroadcaster 4 | // 5 | // Created by jinchu darwin on 16/6/25. 6 | // Copyright © 2016年 dawenhing.top. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface PrepareLiveVC : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /demo/SampleBroadcaster/SampleBroadcaster/PrepareLiveVC.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // SampleBroadcaster 4 | // 5 | // Created by jinchu darwin on 16/6/25. 6 | // Copyright © 2016年 dawenhing.top. All rights reserved. 7 | // 8 | 9 | #import "PrepareLiveVC.h" 10 | #import "LivingVC.h" 11 | 12 | @interface PrepareLiveVC () { 13 | } 14 | 15 | @property (weak, nonatomic) IBOutlet UITextField *streamURLText; 16 | @property (weak, nonatomic) IBOutlet UITextField *streamNameText; 17 | @property (weak, nonatomic) IBOutlet UITextField *bitrateText; 18 | 19 | @end 20 | 21 | @implementation PrepareLiveVC { 22 | } 23 | 24 | - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 25 | if ([[segue identifier] isEqualToString:@"showLivingVC"]) { 26 | LivingVC *vc = (LivingVC *)segue.destinationViewController; 27 | vc.pushURL = self.streamURLText.text; 28 | vc.streamName = self.streamNameText.text; 29 | vc.bitrate = [[[[NSNumberFormatter alloc] init] numberFromString:self.bitrateText.text] intValue]; 30 | } 31 | } 32 | 33 | @end 34 | -------------------------------------------------------------------------------- /demo/SampleBroadcaster/SampleBroadcaster/Vendor/YYKit/YYTextWeakProxy.h: -------------------------------------------------------------------------------- 1 | // 2 | // YYTextWeakProxy.h 3 | // YYText 4 | // 5 | // Created by ibireme on 14/10/18. 6 | // Copyright (c) 2015 ibireme. 7 | // 8 | // This source code is licensed under the MIT-style license found in the 9 | // LICENSE file in the root directory of this source tree. 10 | // 11 | 12 | #import 13 | 14 | NS_ASSUME_NONNULL_BEGIN 15 | 16 | /** 17 | A proxy used to hold a weak object. 18 | It can be used to avoid retain cycles, such as the target in NSTimer or CADisplayLink. 19 | 20 | sample code: 21 | 22 | @implementation MyView { 23 | NSTimer *_timer; 24 | } 25 | 26 | - (void)initTimer { 27 | YYTextWeakProxy *proxy = [YYTextWeakProxy proxyWithTarget:self]; 28 | _timer = [NSTimer timerWithTimeInterval:0.1 target:proxy selector:@selector(tick:) userInfo:nil repeats:YES]; 29 | } 30 | 31 | - (void)tick:(NSTimer *)timer {...} 32 | @end 33 | */ 34 | @interface YYTextWeakProxy : NSProxy 35 | 36 | /** 37 | The proxy target. 38 | */ 39 | @property (nullable, nonatomic, weak, readonly) id target; 40 | 41 | /** 42 | Creates a new weak proxy for target. 43 | 44 | @param target Target object. 45 | 46 | @return A new proxy object. 47 | */ 48 | - (instancetype)initWithTarget:(id)target; 49 | 50 | /** 51 | Creates a new weak proxy for target. 52 | 53 | @param target Target object. 54 | 55 | @return A new proxy object. 56 | */ 57 | + (instancetype)proxyWithTarget:(id)target; 58 | 59 | @end 60 | 61 | NS_ASSUME_NONNULL_END 62 | -------------------------------------------------------------------------------- /demo/SampleBroadcaster/SampleBroadcaster/Vendor/YYKit/YYTextWeakProxy.m: -------------------------------------------------------------------------------- 1 | // 2 | // YYTextWeakProxy.m 3 | // YYText 4 | // 5 | // Created by ibireme on 14/10/18. 6 | // Copyright (c) 2015 ibireme. 7 | // 8 | // This source code is licensed under the MIT-style license found in the 9 | // LICENSE file in the root directory of this source tree. 10 | // 11 | 12 | #import "YYTextWeakProxy.h" 13 | 14 | 15 | @implementation YYTextWeakProxy 16 | 17 | - (instancetype)initWithTarget:(id)target { 18 | _target = target; 19 | return self; 20 | } 21 | 22 | + (instancetype)proxyWithTarget:(id)target { 23 | return [[YYTextWeakProxy alloc] initWithTarget:target]; 24 | } 25 | 26 | - (id)forwardingTargetForSelector:(SEL)selector { 27 | return _target; 28 | } 29 | 30 | - (void)forwardInvocation:(NSInvocation *)invocation { 31 | void *null = NULL; 32 | [invocation setReturnValue:&null]; 33 | } 34 | 35 | - (NSMethodSignature *)methodSignatureForSelector:(SEL)selector { 36 | return [NSObject instanceMethodSignatureForSelector:@selector(init)]; 37 | } 38 | 39 | - (BOOL)respondsToSelector:(SEL)aSelector { 40 | return [_target respondsToSelector:aSelector]; 41 | } 42 | 43 | - (BOOL)isEqual:(id)object { 44 | return [_target isEqual:object]; 45 | } 46 | 47 | - (NSUInteger)hash { 48 | return [_target hash]; 49 | } 50 | 51 | - (Class)superclass { 52 | return [_target superclass]; 53 | } 54 | 55 | - (Class)class { 56 | return [_target class]; 57 | } 58 | 59 | - (BOOL)isKindOfClass:(Class)aClass { 60 | return [_target isKindOfClass:aClass]; 61 | } 62 | 63 | - (BOOL)isMemberOfClass:(Class)aClass { 64 | return [_target isMemberOfClass:aClass]; 65 | } 66 | 67 | - (BOOL)conformsToProtocol:(Protocol *)aProtocol { 68 | return [_target conformsToProtocol:aProtocol]; 69 | } 70 | 71 | - (BOOL)isProxy { 72 | return YES; 73 | } 74 | 75 | - (NSString *)description { 76 | return [_target description]; 77 | } 78 | 79 | - (NSString *)debugDescription { 80 | return [_target debugDescription]; 81 | } 82 | 83 | @end 84 | -------------------------------------------------------------------------------- /demo/SampleBroadcaster/SampleBroadcaster/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // SampleBroadcaster 4 | // 5 | // Created by jinchu darwin on 16/6/25. 6 | // Copyright © 2016年 dawenhing.top. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /docs/Architecture.md: -------------------------------------------------------------------------------- 1 | ## Architecture Overview 2 | 3 | VideoCore's architecture is very simple and modular. The basic concept of VideoCore's graph approach is to connect components that perform specific operations on the data to go from the supplied input format to the required output format. There are three main component types used to accomplish this: *[Sources](#sources)*, *[Outputs](#outputs)*, and *[Transforms](#transforms)*. It should be noted, though, that Transforms are a special class of Outputs that will pass their data to another output. 4 | 5 | 6 | ### Sources 7 | *See [Sources](./Sources.md) for more information on sources* 8 | 9 | Sources are subclasses of `videocore::ISource` and reside at the beginning of the transformation chain for a particular input type. A source may, for example, vend sample buffers from an iPhone's camera or audio buffers from an application. 10 | 11 | 12 | ### Outputs 13 | *See [Outputs](./Outputs.md) for more information on sources* 14 | 15 | ### Transforms 16 | *See [Transforms](./Transforms.md) -------------------------------------------------------------------------------- /docs/Mixers.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dourgulf/VideoCore/3c7134ea6f26e94e1c6c20bcc67bf6b0c37ce6ee/docs/Mixers.md -------------------------------------------------------------------------------- /docs/Outputs.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dourgulf/VideoCore/3c7134ea6f26e94e1c6c20bcc67bf6b0c37ce6ee/docs/Outputs.md -------------------------------------------------------------------------------- /docs/SettingUpAGraph.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dourgulf/VideoCore/3c7134ea6f26e94e1c6c20bcc67bf6b0c37ce6ee/docs/SettingUpAGraph.md -------------------------------------------------------------------------------- /docs/Sources.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dourgulf/VideoCore/3c7134ea6f26e94e1c6c20bcc67bf6b0c37ce6ee/docs/Sources.md -------------------------------------------------------------------------------- /docs/Transforms.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dourgulf/VideoCore/3c7134ea6f26e94e1c6c20bcc67bf6b0c37ce6ee/docs/Transforms.md -------------------------------------------------------------------------------- /filters/Basic/AntiqueVideoFilter.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // AntiqueVideoFilter.hpp 3 | // Pods 4 | // 5 | // Created by Alex.Shi on 16/3/3. 6 | // 7 | // 8 | 9 | #ifndef AntiqueVideoFilter_hpp 10 | #define AntiqueVideoFilter_hpp 11 | 12 | #include 13 | 14 | namespace videocore { 15 | namespace filters { 16 | class AntiqueVideoFilter : public IVideoFilter { 17 | 18 | public: 19 | AntiqueVideoFilter(); 20 | ~AntiqueVideoFilter(); 21 | 22 | public: 23 | virtual void initialize(); 24 | virtual bool initialized() const { return m_initialized; }; 25 | virtual std::string const name() { return "com.videocore.filters.antique"; }; 26 | virtual void bind(); 27 | virtual void unbind(); 28 | 29 | public: 30 | 31 | const char * const vertexKernel() const ; 32 | const char * const pixelKernel() const ; 33 | 34 | private: 35 | static bool registerFilter(); 36 | static bool s_registered; 37 | private: 38 | 39 | unsigned int m_vao; 40 | unsigned int m_uMatrix; 41 | int mToneCurveTextureUniformLocation; 42 | 43 | bool m_initialized; 44 | bool m_bound; 45 | 46 | }; 47 | } 48 | } 49 | 50 | #endif /* AntiqueVideoFilter_hpp */ 51 | -------------------------------------------------------------------------------- /filters/Basic/BasicVideoFilterBGRA.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #include 5 | 6 | 7 | #ifdef TARGET_OS_IPHONE 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #endif 15 | 16 | namespace videocore { namespace filters { 17 | 18 | bool BasicVideoFilterBGRA::s_registered = BasicVideoFilterBGRA::registerFilter(); 19 | 20 | bool 21 | BasicVideoFilterBGRA::registerFilter() 22 | { 23 | FilterFactory::_register("com.videocore.filters.bgra", []() { return new BasicVideoFilterBGRA(); }); 24 | return true; 25 | } 26 | 27 | BasicVideoFilterBGRA::BasicVideoFilterBGRA() 28 | : IVideoFilter(), m_initialized(false), m_bound(false) 29 | { 30 | 31 | } 32 | BasicVideoFilterBGRA::~BasicVideoFilterBGRA() 33 | { 34 | glDeleteProgram(m_program); 35 | glDeleteVertexArraysOES(1, &m_vao); 36 | } 37 | 38 | const char * const 39 | BasicVideoFilterBGRA::vertexKernel() const 40 | { 41 | 42 | KERNEL(GL_ES2_3, m_language, 43 | attribute vec2 aPos; 44 | attribute vec2 aCoord; 45 | varying vec2 vCoord; 46 | uniform mat4 uMat; 47 | void main(void) { 48 | gl_Position = uMat * vec4(aPos,0.,1.); 49 | vCoord = aCoord; 50 | } 51 | ) 52 | 53 | return nullptr; 54 | } 55 | 56 | const char * const 57 | BasicVideoFilterBGRA::pixelKernel() const 58 | { 59 | 60 | KERNEL(GL_ES2_3, m_language, 61 | precision mediump float; 62 | varying vec2 vCoord; 63 | uniform sampler2D uTex0; 64 | void main(void) { 65 | gl_FragData[0] = texture2D(uTex0, vCoord); 66 | } 67 | ) 68 | 69 | return nullptr; 70 | } 71 | void 72 | BasicVideoFilterBGRA::initialize() 73 | { 74 | switch(m_language) { 75 | case GL_ES2_3: 76 | case GL_2: { 77 | setProgram(build_program(vertexKernel(), pixelKernel())); 78 | glGenVertexArraysOES(1, &m_vao); 79 | glBindVertexArrayOES(m_vao); 80 | m_uMatrix = glGetUniformLocation(m_program, "uMat"); 81 | int attrpos = glGetAttribLocation(m_program, "aPos"); 82 | int attrtex = glGetAttribLocation(m_program, "aCoord"); 83 | int unitex = glGetUniformLocation(m_program, "uTex0"); 84 | glUniform1i(unitex, 0); 85 | glEnableVertexAttribArray(attrpos); 86 | glEnableVertexAttribArray(attrtex); 87 | glVertexAttribPointer(attrpos, BUFFER_SIZE_POSITION, GL_FLOAT, GL_FALSE, BUFFER_STRIDE, BUFFER_OFFSET_POSITION); 88 | glVertexAttribPointer(attrtex, BUFFER_SIZE_POSITION, GL_FLOAT, GL_FALSE, BUFFER_STRIDE, BUFFER_OFFSET_TEXTURE); 89 | m_initialized = true; 90 | } 91 | break; 92 | case GL_3: 93 | break; 94 | } 95 | } 96 | void 97 | BasicVideoFilterBGRA::bind() 98 | { 99 | switch(m_language) { 100 | case GL_ES2_3: 101 | case GL_2: 102 | if(!m_bound) { 103 | if(!initialized()) { 104 | initialize(); 105 | } 106 | glUseProgram(m_program); 107 | glBindVertexArrayOES(m_vao); 108 | } 109 | glUniformMatrix4fv(m_uMatrix, 1, GL_FALSE, &m_matrix[0][0]); 110 | break; 111 | case GL_3: 112 | break; 113 | } 114 | } 115 | void 116 | BasicVideoFilterBGRA::unbind() 117 | { 118 | m_bound = false; 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /filters/Basic/BasicVideoFilterBGRA.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #ifndef videocore_BasicVideoFilterBGRA_h 26 | #define videocore_BasicVideoFilterBGRA_h 27 | #include 28 | 29 | namespace videocore { 30 | namespace filters { 31 | class BasicVideoFilterBGRA : public IVideoFilter { 32 | 33 | public: 34 | BasicVideoFilterBGRA(); 35 | ~BasicVideoFilterBGRA(); 36 | 37 | public: 38 | virtual void initialize(); 39 | virtual bool initialized() const { return m_initialized; }; 40 | virtual std::string const name() { return "com.videocore.filters.bgra"; }; 41 | virtual void bind(); 42 | virtual void unbind(); 43 | 44 | public: 45 | 46 | const char * const vertexKernel() const ; 47 | const char * const pixelKernel() const ; 48 | 49 | private: 50 | static bool registerFilter(); 51 | static bool s_registered; 52 | private: 53 | 54 | unsigned int m_vao; 55 | unsigned int m_uMatrix; 56 | bool m_initialized; 57 | bool m_bound; 58 | 59 | }; 60 | } 61 | } 62 | 63 | #endif /* defined(videocore_BasicVideoFilterBGRA_h) */ 64 | -------------------------------------------------------------------------------- /filters/Basic/BasicVideoFilterBGRAinYUVAout.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #ifdef __APPLE__ 4 | #include 5 | # ifdef TARGET_OS_IPHONE 6 | # include 7 | # include 8 | # include 9 | # include 10 | # endif 11 | #endif 12 | 13 | namespace videocore { namespace filters { 14 | 15 | bool BasicVideoFilterBGRAinYUVAout::s_registered = BasicVideoFilterBGRAinYUVAout::registerFilter(); 16 | 17 | bool 18 | BasicVideoFilterBGRAinYUVAout::registerFilter() 19 | { 20 | FilterFactory::_register("com.videocore.filters.bgra2yuva", []() { return new BasicVideoFilterBGRAinYUVAout(); }); 21 | return true; 22 | } 23 | 24 | BasicVideoFilterBGRAinYUVAout::BasicVideoFilterBGRAinYUVAout() 25 | : IVideoFilter(), m_initialized(false), m_bound(false) 26 | { 27 | 28 | } 29 | BasicVideoFilterBGRAinYUVAout::~BasicVideoFilterBGRAinYUVAout() 30 | { 31 | glDeleteProgram(m_program); 32 | glDeleteVertexArrays(1, &m_vao); 33 | } 34 | 35 | const char * const 36 | BasicVideoFilterBGRAinYUVAout::vertexKernel() const 37 | { 38 | 39 | KERNEL(GL_ES2_3, m_language, 40 | attribute vec2 aPos; 41 | attribute vec2 aCoord; 42 | varying vec2 vCoord; 43 | uniform mat4 uMat; 44 | void main(void) { 45 | gl_Position = uMat * vec4(aPos,0.,1.); 46 | vCoord = aCoord; 47 | } 48 | ) 49 | 50 | return nullptr; 51 | } 52 | 53 | const char * const 54 | BasicVideoFilterBGRAinYUVAout::pixelKernel() const 55 | { 56 | 57 | KERNEL(GL_ES2_3, m_language, 58 | precision mediump float; 59 | varying vec2 vCoord; 60 | uniform sampler2D uTex0; 61 | const mat4 RGBtoYUV(0.257, 0.439, -0.148, 0.0, 62 | 0.504, -0.368, -0.291, 0.0, 63 | 0.098, -0.071, 0.439, 0.0, 64 | 0.0625, 0.500, 0.500, 1.0 ); 65 | void main(void) { 66 | gl_FragData[0] = texture2D(uTex0, vCoord) * RGBtoYUV; 67 | } 68 | ) 69 | 70 | return nullptr; 71 | } 72 | void 73 | BasicVideoFilterBGRAinYUVAout::initialize() 74 | { 75 | switch(m_language) { 76 | case GL_ES2_3: 77 | case GL_2: { 78 | setProgram(build_program(vertexKernel(), pixelKernel())); 79 | glGenVertexArrays(1, &m_vao); 80 | glBindVertexArray(m_vao); 81 | m_uMatrix = glGetUniformLocation(m_program, "uMat"); 82 | int attrpos = glGetAttribLocation(m_program, "aPos"); 83 | int attrtex = glGetAttribLocation(m_program, "aCoord"); 84 | int unitex = glGetUniformLocation(m_program, "uTex0"); 85 | glUniform1i(unitex, 0); 86 | glEnableVertexAttribArray(attrpos); 87 | glEnableVertexAttribArray(attrtex); 88 | glVertexAttribPointer(attrpos, BUFFER_SIZE_POSITION, GL_FLOAT, GL_FALSE, BUFFER_STRIDE, BUFFER_OFFSET_POSITION); 89 | glVertexAttribPointer(attrtex, BUFFER_SIZE_POSITION, GL_FLOAT, GL_FALSE, BUFFER_STRIDE, BUFFER_OFFSET_TEXTURE); 90 | m_initialized = true; 91 | } 92 | break; 93 | case GL_3: 94 | break; 95 | } 96 | } 97 | void 98 | BasicVideoFilterBGRAinYUVAout::bind() 99 | { 100 | switch(m_language) { 101 | case GL_ES2_3: 102 | case GL_2: 103 | if(!m_bound) { 104 | if(!initialized()) { 105 | initialize(); 106 | } 107 | glUseProgram(m_program); 108 | glBindVertexArray(m_vao); 109 | } 110 | glUniformMatrix4fv(m_uMatrix, 1, GL_FALSE, &m_matrix[0][0]); 111 | break; 112 | case GL_3: 113 | break; 114 | } 115 | } 116 | void 117 | BasicVideoFilterBGRAinYUVAout::unbind() 118 | { 119 | m_bound = false; 120 | } 121 | } 122 | } -------------------------------------------------------------------------------- /filters/Basic/BasicVideoFilterBGRAinYUVAout.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __mobcrush__BasicVideoFilterBGRAinYUVAout__ 3 | #define __mobcrush__BasicVideoFilterBGRAinYUVAout__ 4 | 5 | #include 6 | 7 | namespace videocore { 8 | namespace filters { 9 | class BasicVideoFilterBGRAinYUVAout : public IVideoFilter { 10 | 11 | public: 12 | BasicVideoFilterBGRAinYUVAout(); 13 | ~BasicVideoFilterBGRAinYUVAout(); 14 | 15 | public: 16 | virtual void initialize(); 17 | virtual bool initialized() const { return m_initialized; }; 18 | virtual std::string const name() { return "com.videocore.filters.bgra2yuva"; }; 19 | virtual void bind(); 20 | virtual void unbind(); 21 | 22 | public: 23 | 24 | const char * const vertexKernel() const ; 25 | const char * const pixelKernel() const ; 26 | 27 | private: 28 | static bool registerFilter(); 29 | static bool s_registered; 30 | private: 31 | 32 | unsigned int m_vao; 33 | unsigned int m_uMatrix; 34 | bool m_initialized; 35 | bool m_bound; 36 | 37 | }; 38 | } 39 | } 40 | 41 | #endif /* defined(__mobcrush__BasicVideoFilterBGRAinYUVAout__) */ 42 | -------------------------------------------------------------------------------- /filters/Basic/BeautyVideoFilter.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // BeautyVideoFilter.hpp 3 | // Pods 4 | // 5 | // Created by Alex.Shi on 16/3/2. 6 | // 7 | // 8 | 9 | #ifndef BeautyVideoFilter_hpp 10 | #define BeautyVideoFilter_hpp 11 | 12 | #include 13 | 14 | namespace videocore { 15 | namespace filters { 16 | class BeautyVideoFilter : public IVideoFilter { 17 | 18 | public: 19 | BeautyVideoFilter(); 20 | ~BeautyVideoFilter(); 21 | 22 | public: 23 | virtual void initialize(); 24 | virtual bool initialized() const { return m_initialized; }; 25 | virtual std::string const name() { return "com.videocore.filters.beauty"; }; 26 | virtual void bind(); 27 | virtual void unbind(); 28 | 29 | public: 30 | 31 | const char * const vertexKernel() const ; 32 | const char * const pixelKernel() const ; 33 | 34 | private: 35 | static bool registerFilter(); 36 | static bool s_registered; 37 | private: 38 | 39 | unsigned int m_vao; 40 | unsigned int m_uMatrix; 41 | unsigned int m_uWH; 42 | unsigned int m_beautyParam; 43 | unsigned int m_uWidth; 44 | unsigned int m_uHeight; 45 | 46 | bool m_initialized; 47 | bool m_bound; 48 | 49 | }; 50 | } 51 | } 52 | 53 | #endif /* BeautyVideoFilter_hpp */ 54 | -------------------------------------------------------------------------------- /filters/Basic/FisheyeVideoFilter.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #include 5 | 6 | 7 | #ifdef TARGET_OS_IPHONE 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #endif 15 | 16 | namespace videocore { namespace filters { 17 | 18 | bool FisheyeVideoFilter::s_registered = FisheyeVideoFilter::registerFilter(); 19 | 20 | bool 21 | FisheyeVideoFilter::registerFilter() 22 | { 23 | FilterFactory::_register("com.videocore.filters.fisheye", []() { return new FisheyeVideoFilter(); }); 24 | return true; 25 | } 26 | 27 | FisheyeVideoFilter::FisheyeVideoFilter() 28 | : IVideoFilter(), m_initialized(false), m_bound(false) 29 | { 30 | 31 | } 32 | FisheyeVideoFilter::~FisheyeVideoFilter() 33 | { 34 | glDeleteProgram(m_program); 35 | glDeleteVertexArrays(1, &m_vao); 36 | } 37 | 38 | const char * const 39 | FisheyeVideoFilter::vertexKernel() const 40 | { 41 | 42 | KERNEL(GL_ES2_3, m_language, 43 | attribute vec2 aPos; 44 | attribute vec2 aCoord; 45 | varying vec2 vCoord; 46 | uniform mat4 uMat; 47 | void main(void) { 48 | gl_Position = uMat * vec4(aPos,0.,1.); 49 | vCoord = aCoord; 50 | } 51 | ) 52 | 53 | return nullptr; 54 | } 55 | 56 | const char * const 57 | FisheyeVideoFilter::pixelKernel() const 58 | { 59 | 60 | KERNEL(GL_ES2_3, m_language, 61 | precision mediump float; 62 | varying vec2 vCoord; 63 | uniform sampler2D uTex0; 64 | void main(void) { 65 | vec2 uv = vCoord - 0.5; 66 | float z = sqrt(1.0 - uv.x * uv.x - uv.y * uv.y); 67 | float a = 1.0 / (z * tan(-5.2)); // FOV 68 | gl_FragColor = texture2D(uTex0, (uv * a) + 0.5); 69 | } 70 | ) 71 | 72 | return nullptr; 73 | } 74 | void 75 | FisheyeVideoFilter::initialize() 76 | { 77 | switch(m_language) { 78 | case GL_ES2_3: 79 | case GL_2: { 80 | setProgram(build_program(vertexKernel(), pixelKernel())); 81 | glGenVertexArrays(1, &m_vao); 82 | glBindVertexArray(m_vao); 83 | m_uMatrix = glGetUniformLocation(m_program, "uMat"); 84 | int attrpos = glGetAttribLocation(m_program, "aPos"); 85 | int attrtex = glGetAttribLocation(m_program, "aCoord"); 86 | int unitex = glGetUniformLocation(m_program, "uTex0"); 87 | glUniform1i(unitex, 0); 88 | glEnableVertexAttribArray(attrpos); 89 | glEnableVertexAttribArray(attrtex); 90 | glVertexAttribPointer(attrpos, BUFFER_SIZE_POSITION, GL_FLOAT, GL_FALSE, BUFFER_STRIDE, BUFFER_OFFSET_POSITION); 91 | glVertexAttribPointer(attrtex, BUFFER_SIZE_POSITION, GL_FLOAT, GL_FALSE, BUFFER_STRIDE, BUFFER_OFFSET_TEXTURE); 92 | m_initialized = true; 93 | } 94 | break; 95 | case GL_3: 96 | break; 97 | } 98 | } 99 | void 100 | FisheyeVideoFilter::bind() 101 | { 102 | switch(m_language) { 103 | case GL_ES2_3: 104 | case GL_2: 105 | if(!m_bound) { 106 | if(!initialized()) { 107 | initialize(); 108 | } 109 | glUseProgram(m_program); 110 | glBindVertexArray(m_vao); 111 | } 112 | glUniformMatrix4fv(m_uMatrix, 1, GL_FALSE, &m_matrix[0][0]); 113 | break; 114 | case GL_3: 115 | break; 116 | } 117 | } 118 | void 119 | FisheyeVideoFilter::unbind() 120 | { 121 | m_bound = false; 122 | } 123 | } 124 | } -------------------------------------------------------------------------------- /filters/Basic/FisheyeVideoFilter.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #ifndef videocore_FisheyeVideoFilter_h 26 | #define videocore_FisheyeVideoFilter_h 27 | #include 28 | 29 | namespace videocore { 30 | namespace filters { 31 | class FisheyeVideoFilter : public IVideoFilter { 32 | 33 | public: 34 | FisheyeVideoFilter(); 35 | ~FisheyeVideoFilter(); 36 | 37 | public: 38 | virtual void initialize(); 39 | virtual bool initialized() const { return m_initialized; }; 40 | virtual std::string const name() { return "com.videocore.filters.fisheye"; }; 41 | virtual void bind(); 42 | virtual void unbind(); 43 | 44 | public: 45 | 46 | const char * const vertexKernel() const ; 47 | const char * const pixelKernel() const ; 48 | 49 | private: 50 | static bool registerFilter(); 51 | static bool s_registered; 52 | private: 53 | 54 | unsigned int m_vao; 55 | unsigned int m_uMatrix; 56 | bool m_initialized; 57 | bool m_bound; 58 | 59 | }; 60 | } 61 | } 62 | 63 | #endif /* defined(videocore_FisheyeVideoFilter_h) */ 64 | -------------------------------------------------------------------------------- /filters/Basic/GlowVideoFilter.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #include 5 | 6 | 7 | #ifdef TARGET_OS_IPHONE 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #endif 15 | 16 | namespace videocore { namespace filters { 17 | 18 | bool GlowVideoFilter::s_registered = GlowVideoFilter::registerFilter(); 19 | 20 | bool 21 | GlowVideoFilter::registerFilter() 22 | { 23 | FilterFactory::_register("com.videocore.filters.glow", []() { return new GlowVideoFilter(); }); 24 | return true; 25 | } 26 | 27 | GlowVideoFilter::GlowVideoFilter() 28 | : IVideoFilter(), m_initialized(false), m_bound(false) 29 | { 30 | 31 | } 32 | GlowVideoFilter::~GlowVideoFilter() 33 | { 34 | glDeleteProgram(m_program); 35 | glDeleteVertexArraysOES(1, &m_vao); 36 | } 37 | 38 | const char * const 39 | GlowVideoFilter::vertexKernel() const 40 | { 41 | 42 | KERNEL(GL_ES2_3, m_language, 43 | attribute vec2 aPos; 44 | attribute vec2 aCoord; 45 | varying vec2 vCoord; 46 | uniform mat4 uMat; 47 | void main(void) { 48 | gl_Position = uMat * vec4(aPos,0.,1.); 49 | vCoord = aCoord; 50 | } 51 | ) 52 | 53 | return nullptr; 54 | } 55 | 56 | const char * const 57 | GlowVideoFilter::pixelKernel() const 58 | { 59 | 60 | KERNEL(GL_ES2_3, m_language, 61 | precision mediump float; 62 | varying vec2 vCoord; 63 | uniform sampler2D uTex0; 64 | const float step_w = 0.0015625; 65 | const float step_h = 0.0027778; 66 | void main(void) { 67 | vec3 t1 = texture2D(uTex0, vec2(vCoord.x - step_w, vCoord.y - step_h)).bgr; 68 | vec3 t2 = texture2D(uTex0, vec2(vCoord.x, vCoord.y - step_h)).bgr; 69 | vec3 t3 = texture2D(uTex0, vec2(vCoord.x + step_w, vCoord.y - step_h)).bgr; 70 | vec3 t4 = texture2D(uTex0, vec2(vCoord.x - step_w, vCoord.y)).bgr; 71 | vec3 t5 = texture2D(uTex0, vCoord).bgr; 72 | vec3 t6 = texture2D(uTex0, vec2(vCoord.x + step_w, vCoord.y)).bgr; 73 | vec3 t7 = texture2D(uTex0, vec2(vCoord.x - step_w, vCoord.y + step_h)).bgr; 74 | vec3 t8 = texture2D(uTex0, vec2(vCoord.x, vCoord.y + step_h)).bgr; 75 | vec3 t9 = texture2D(uTex0, vec2(vCoord.x + step_w, vCoord.y + step_h)).bgr; 76 | 77 | vec3 xx= t1 + 2.0*t2 + t3 - t7 - 2.0*t8 - t9; 78 | vec3 yy = t1 - t3 + 2.0*t4 - 2.0*t6 + t7 - t9; 79 | 80 | vec3 rr = sqrt(xx * xx + yy * yy); 81 | 82 | gl_FragColor.a = 1.0; 83 | gl_FragColor.rgb = rr * 2.0 * t5; 84 | } 85 | ) 86 | 87 | return nullptr; 88 | } 89 | void 90 | GlowVideoFilter::initialize() 91 | { 92 | switch(m_language) { 93 | case GL_ES2_3: 94 | case GL_2: { 95 | setProgram(build_program(vertexKernel(), pixelKernel())); 96 | glGenVertexArraysOES(1, &m_vao); 97 | glBindVertexArrayOES(m_vao); 98 | m_uMatrix = glGetUniformLocation(m_program, "uMat"); 99 | int attrpos = glGetAttribLocation(m_program, "aPos"); 100 | int attrtex = glGetAttribLocation(m_program, "aCoord"); 101 | int unitex = glGetUniformLocation(m_program, "uTex0"); 102 | glUniform1i(unitex, 0); 103 | glEnableVertexAttribArray(attrpos); 104 | glEnableVertexAttribArray(attrtex); 105 | glVertexAttribPointer(attrpos, BUFFER_SIZE_POSITION, GL_FLOAT, GL_FALSE, BUFFER_STRIDE, BUFFER_OFFSET_POSITION); 106 | glVertexAttribPointer(attrtex, BUFFER_SIZE_POSITION, GL_FLOAT, GL_FALSE, BUFFER_STRIDE, BUFFER_OFFSET_TEXTURE); 107 | m_initialized = true; 108 | } 109 | break; 110 | case GL_3: 111 | break; 112 | } 113 | } 114 | void 115 | GlowVideoFilter::bind() 116 | { 117 | switch(m_language) { 118 | case GL_ES2_3: 119 | case GL_2: 120 | if(!m_bound) { 121 | if(!initialized()) { 122 | initialize(); 123 | } 124 | glUseProgram(m_program); 125 | glBindVertexArrayOES(m_vao); 126 | } 127 | glUniformMatrix4fv(m_uMatrix, 1, GL_FALSE, &m_matrix[0][0]); 128 | break; 129 | case GL_3: 130 | break; 131 | } 132 | } 133 | void 134 | GlowVideoFilter::unbind() 135 | { 136 | m_bound = false; 137 | } 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /filters/Basic/GlowVideoFilter.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #ifndef videocore_GlowVideoFilter_h 26 | #define videocore_GlowVideoFilter_h 27 | #include 28 | 29 | namespace videocore { 30 | namespace filters { 31 | class GlowVideoFilter : public IVideoFilter { 32 | 33 | public: 34 | GlowVideoFilter(); 35 | ~GlowVideoFilter(); 36 | 37 | public: 38 | virtual void initialize(); 39 | virtual bool initialized() const { return m_initialized; }; 40 | virtual std::string const name() { return "com.videocore.filters.glow"; }; 41 | virtual void bind(); 42 | virtual void unbind(); 43 | 44 | public: 45 | 46 | const char * const vertexKernel() const ; 47 | const char * const pixelKernel() const ; 48 | 49 | private: 50 | static bool registerFilter(); 51 | static bool s_registered; 52 | private: 53 | 54 | unsigned int m_vao; 55 | unsigned int m_uMatrix; 56 | bool m_initialized; 57 | bool m_bound; 58 | 59 | }; 60 | } 61 | } 62 | 63 | #endif /* defined(videocore_GlowVideoFilter_h) */ 64 | -------------------------------------------------------------------------------- /filters/Basic/GrayscaleVideoFilter.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #include 5 | 6 | 7 | #ifdef TARGET_OS_IPHONE 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #endif 15 | 16 | namespace videocore { namespace filters { 17 | 18 | bool GrayscaleVideoFilter::s_registered = GrayscaleVideoFilter::registerFilter(); 19 | 20 | bool 21 | GrayscaleVideoFilter::registerFilter() 22 | { 23 | FilterFactory::_register("com.videocore.filters.grayscale", []() { return new GrayscaleVideoFilter(); }); 24 | return true; 25 | } 26 | 27 | GrayscaleVideoFilter::GrayscaleVideoFilter() 28 | : IVideoFilter(), m_initialized(false), m_bound(false) 29 | { 30 | 31 | } 32 | GrayscaleVideoFilter::~GrayscaleVideoFilter() 33 | { 34 | glDeleteProgram(m_program); 35 | glDeleteVertexArraysOES(1, &m_vao); 36 | } 37 | 38 | const char * const 39 | GrayscaleVideoFilter::vertexKernel() const 40 | { 41 | 42 | KERNEL(GL_ES2_3, m_language, 43 | attribute vec2 aPos; 44 | attribute vec2 aCoord; 45 | varying vec2 vCoord; 46 | uniform mat4 uMat; 47 | void main(void) { 48 | gl_Position = uMat * vec4(aPos,0.,1.); 49 | vCoord = aCoord; 50 | } 51 | ) 52 | 53 | return nullptr; 54 | } 55 | 56 | const char * const 57 | GrayscaleVideoFilter::pixelKernel() const 58 | { 59 | 60 | KERNEL(GL_ES2_3, m_language, 61 | precision mediump float; 62 | varying vec2 vCoord; 63 | uniform sampler2D uTex0; 64 | void main(void) { 65 | vec4 color = texture2D(uTex0, vCoord); 66 | float gray = dot(color.rgb, vec3(0.3, 0.59, 0.11)); 67 | gl_FragColor = vec4(gray, gray, gray, color.a); 68 | } 69 | ) 70 | 71 | return nullptr; 72 | } 73 | void 74 | GrayscaleVideoFilter::initialize() 75 | { 76 | switch(m_language) { 77 | case GL_ES2_3: 78 | case GL_2: { 79 | setProgram(build_program(vertexKernel(), pixelKernel())); 80 | glGenVertexArraysOES(1, &m_vao); 81 | glBindVertexArrayOES(m_vao); 82 | m_uMatrix = glGetUniformLocation(m_program, "uMat"); 83 | int attrpos = glGetAttribLocation(m_program, "aPos"); 84 | int attrtex = glGetAttribLocation(m_program, "aCoord"); 85 | int unitex = glGetUniformLocation(m_program, "uTex0"); 86 | glUniform1i(unitex, 0); 87 | glEnableVertexAttribArray(attrpos); 88 | glEnableVertexAttribArray(attrtex); 89 | glVertexAttribPointer(attrpos, BUFFER_SIZE_POSITION, GL_FLOAT, GL_FALSE, BUFFER_STRIDE, BUFFER_OFFSET_POSITION); 90 | glVertexAttribPointer(attrtex, BUFFER_SIZE_POSITION, GL_FLOAT, GL_FALSE, BUFFER_STRIDE, BUFFER_OFFSET_TEXTURE); 91 | m_initialized = true; 92 | } 93 | break; 94 | case GL_3: 95 | break; 96 | } 97 | } 98 | void 99 | GrayscaleVideoFilter::bind() 100 | { 101 | switch(m_language) { 102 | case GL_ES2_3: 103 | case GL_2: 104 | if(!m_bound) { 105 | if(!initialized()) { 106 | initialize(); 107 | } 108 | glUseProgram(m_program); 109 | glBindVertexArrayOES(m_vao); 110 | } 111 | glUniformMatrix4fv(m_uMatrix, 1, GL_FALSE, &m_matrix[0][0]); 112 | break; 113 | case GL_3: 114 | break; 115 | } 116 | } 117 | void 118 | GrayscaleVideoFilter::unbind() 119 | { 120 | m_bound = false; 121 | } 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /filters/Basic/GrayscaleVideoFilter.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #ifndef videocore_GrayscaleVideoFilter_h 26 | #define videocore_GrayscaleVideoFilter_h 27 | #include 28 | 29 | namespace videocore { 30 | namespace filters { 31 | class GrayscaleVideoFilter : public IVideoFilter { 32 | 33 | public: 34 | GrayscaleVideoFilter(); 35 | ~GrayscaleVideoFilter(); 36 | 37 | public: 38 | virtual void initialize(); 39 | virtual bool initialized() const { return m_initialized; }; 40 | virtual std::string const name() { return "com.videocore.filters.grayscale"; }; 41 | virtual void bind(); 42 | virtual void unbind(); 43 | 44 | public: 45 | 46 | const char * const vertexKernel() const ; 47 | const char * const pixelKernel() const ; 48 | 49 | private: 50 | static bool registerFilter(); 51 | static bool s_registered; 52 | private: 53 | 54 | unsigned int m_vao; 55 | unsigned int m_uMatrix; 56 | bool m_initialized; 57 | bool m_bound; 58 | 59 | }; 60 | } 61 | } 62 | 63 | #endif /* defined(videocore_GrayscaleVideoFilter_h) */ 64 | -------------------------------------------------------------------------------- /filters/Basic/InvertColorsVideoFilter.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #include 5 | 6 | 7 | #ifdef TARGET_OS_IPHONE 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #endif 15 | 16 | namespace videocore { namespace filters { 17 | 18 | bool InvertColorsVideoFilter::s_registered = InvertColorsVideoFilter::registerFilter(); 19 | 20 | bool 21 | InvertColorsVideoFilter::registerFilter() 22 | { 23 | FilterFactory::_register("com.videocore.filters.invertColors", []() { return new InvertColorsVideoFilter(); }); 24 | return true; 25 | } 26 | 27 | InvertColorsVideoFilter::InvertColorsVideoFilter() 28 | : IVideoFilter(), m_initialized(false), m_bound(false) 29 | { 30 | 31 | } 32 | InvertColorsVideoFilter::~InvertColorsVideoFilter() 33 | { 34 | glDeleteProgram(m_program); 35 | glDeleteVertexArrays(1, &m_vao); 36 | } 37 | 38 | const char * const 39 | InvertColorsVideoFilter::vertexKernel() const 40 | { 41 | 42 | KERNEL(GL_ES2_3, m_language, 43 | attribute vec2 aPos; 44 | attribute vec2 aCoord; 45 | varying vec2 vCoord; 46 | uniform mat4 uMat; 47 | void main(void) { 48 | gl_Position = uMat * vec4(aPos,0.,1.); 49 | vCoord = aCoord; 50 | } 51 | ) 52 | 53 | return nullptr; 54 | } 55 | 56 | const char * const 57 | InvertColorsVideoFilter::pixelKernel() const 58 | { 59 | 60 | KERNEL(GL_ES2_3, m_language, 61 | precision mediump float; 62 | varying vec2 vCoord; 63 | uniform sampler2D uTex0; 64 | void main(void) { 65 | vec4 color = texture2D(uTex0, vCoord); 66 | gl_FragColor = vec4(1.0 - color.r, 1.0 - color.g, 1.0 - color.b, color.a); 67 | } 68 | ) 69 | 70 | return nullptr; 71 | } 72 | void 73 | InvertColorsVideoFilter::initialize() 74 | { 75 | switch(m_language) { 76 | case GL_ES2_3: 77 | case GL_2: { 78 | setProgram(build_program(vertexKernel(), pixelKernel())); 79 | glGenVertexArrays(1, &m_vao); 80 | glBindVertexArray(m_vao); 81 | m_uMatrix = glGetUniformLocation(m_program, "uMat"); 82 | int attrpos = glGetAttribLocation(m_program, "aPos"); 83 | int attrtex = glGetAttribLocation(m_program, "aCoord"); 84 | int unitex = glGetUniformLocation(m_program, "uTex0"); 85 | glUniform1i(unitex, 0); 86 | glEnableVertexAttribArray(attrpos); 87 | glEnableVertexAttribArray(attrtex); 88 | glVertexAttribPointer(attrpos, BUFFER_SIZE_POSITION, GL_FLOAT, GL_FALSE, BUFFER_STRIDE, BUFFER_OFFSET_POSITION); 89 | glVertexAttribPointer(attrtex, BUFFER_SIZE_POSITION, GL_FLOAT, GL_FALSE, BUFFER_STRIDE, BUFFER_OFFSET_TEXTURE); 90 | m_initialized = true; 91 | } 92 | break; 93 | case GL_3: 94 | break; 95 | } 96 | } 97 | void 98 | InvertColorsVideoFilter::bind() 99 | { 100 | switch(m_language) { 101 | case GL_ES2_3: 102 | case GL_2: 103 | if(!m_bound) { 104 | if(!initialized()) { 105 | initialize(); 106 | } 107 | glUseProgram(m_program); 108 | glBindVertexArray(m_vao); 109 | } 110 | glUniformMatrix4fv(m_uMatrix, 1, GL_FALSE, &m_matrix[0][0]); 111 | break; 112 | case GL_3: 113 | break; 114 | } 115 | } 116 | void 117 | InvertColorsVideoFilter::unbind() 118 | { 119 | m_bound = false; 120 | } 121 | } 122 | } -------------------------------------------------------------------------------- /filters/Basic/InvertColorsVideoFilter.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #ifndef videocore_InvertColorsVideoFilter_h 26 | #define videocore_InvertColorsVideoFilter_h 27 | #include 28 | 29 | namespace videocore { 30 | namespace filters { 31 | class InvertColorsVideoFilter : public IVideoFilter { 32 | 33 | public: 34 | InvertColorsVideoFilter(); 35 | ~InvertColorsVideoFilter(); 36 | 37 | public: 38 | virtual void initialize(); 39 | virtual bool initialized() const { return m_initialized; }; 40 | virtual std::string const name() { return "com.videocore.filters.invertColors"; }; 41 | virtual void bind(); 42 | virtual void unbind(); 43 | 44 | public: 45 | 46 | const char * const vertexKernel() const ; 47 | const char * const pixelKernel() const ; 48 | 49 | private: 50 | static bool registerFilter(); 51 | static bool s_registered; 52 | private: 53 | 54 | unsigned int m_vao; 55 | unsigned int m_uMatrix; 56 | bool m_initialized; 57 | bool m_bound; 58 | 59 | }; 60 | } 61 | } 62 | 63 | #endif /* defined(videocore_InvertColorsVideoFilter_h) */ 64 | -------------------------------------------------------------------------------- /filters/Basic/SepiaVideoFilter.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #include 5 | 6 | 7 | #ifdef TARGET_OS_IPHONE 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #endif 15 | 16 | namespace videocore { namespace filters { 17 | 18 | bool SepiaVideoFilter::s_registered = SepiaVideoFilter::registerFilter(); 19 | 20 | bool 21 | SepiaVideoFilter::registerFilter() 22 | { 23 | FilterFactory::_register("com.videocore.filters.sepia", []() { return new SepiaVideoFilter(); }); 24 | return true; 25 | } 26 | 27 | SepiaVideoFilter::SepiaVideoFilter() 28 | : IVideoFilter(), m_initialized(false), m_bound(false) 29 | { 30 | 31 | } 32 | SepiaVideoFilter::~SepiaVideoFilter() 33 | { 34 | glDeleteProgram(m_program); 35 | glDeleteVertexArrays(1, &m_vao); 36 | } 37 | 38 | const char * const 39 | SepiaVideoFilter::vertexKernel() const 40 | { 41 | 42 | KERNEL(GL_ES2_3, m_language, 43 | attribute vec2 aPos; 44 | attribute vec2 aCoord; 45 | varying vec2 vCoord; 46 | uniform mat4 uMat; 47 | void main(void) { 48 | gl_Position = uMat * vec4(aPos,0.,1.); 49 | vCoord = aCoord; 50 | } 51 | ) 52 | 53 | return nullptr; 54 | } 55 | 56 | const char * const 57 | SepiaVideoFilter::pixelKernel() const 58 | { 59 | 60 | KERNEL(GL_ES2_3, m_language, 61 | precision mediump float; 62 | varying vec2 vCoord; 63 | uniform sampler2D uTex0; 64 | const vec3 SEPIA = vec3(1.2, 1.0, 0.8); 65 | void main(void) { 66 | vec4 color = texture2D(uTex0, vCoord); 67 | float gray = dot(color.rgb, vec3(0.3, 0.59, 0.11)); 68 | vec3 sepiaColor = vec3(gray) * SEPIA; 69 | color.rgb = mix(color.rgb, sepiaColor, 0.75); 70 | gl_FragColor = color; 71 | } 72 | ) 73 | 74 | return nullptr; 75 | } 76 | void 77 | SepiaVideoFilter::initialize() 78 | { 79 | switch(m_language) { 80 | case GL_ES2_3: 81 | case GL_2: { 82 | setProgram(build_program(vertexKernel(), pixelKernel())); 83 | glGenVertexArrays(1, &m_vao); 84 | glBindVertexArray(m_vao); 85 | m_uMatrix = glGetUniformLocation(m_program, "uMat"); 86 | int attrpos = glGetAttribLocation(m_program, "aPos"); 87 | int attrtex = glGetAttribLocation(m_program, "aCoord"); 88 | int unitex = glGetUniformLocation(m_program, "uTex0"); 89 | glUniform1i(unitex, 0); 90 | glEnableVertexAttribArray(attrpos); 91 | glEnableVertexAttribArray(attrtex); 92 | glVertexAttribPointer(attrpos, BUFFER_SIZE_POSITION, GL_FLOAT, GL_FALSE, BUFFER_STRIDE, BUFFER_OFFSET_POSITION); 93 | glVertexAttribPointer(attrtex, BUFFER_SIZE_POSITION, GL_FLOAT, GL_FALSE, BUFFER_STRIDE, BUFFER_OFFSET_TEXTURE); 94 | m_initialized = true; 95 | } 96 | break; 97 | case GL_3: 98 | break; 99 | } 100 | } 101 | void 102 | SepiaVideoFilter::bind() 103 | { 104 | switch(m_language) { 105 | case GL_ES2_3: 106 | case GL_2: 107 | if(!m_bound) { 108 | if(!initialized()) { 109 | initialize(); 110 | } 111 | glUseProgram(m_program); 112 | glBindVertexArray(m_vao); 113 | } 114 | glUniformMatrix4fv(m_uMatrix, 1, GL_FALSE, &m_matrix[0][0]); 115 | break; 116 | case GL_3: 117 | break; 118 | } 119 | } 120 | void 121 | SepiaVideoFilter::unbind() 122 | { 123 | m_bound = false; 124 | } 125 | } 126 | } -------------------------------------------------------------------------------- /filters/Basic/SepiaVideoFilter.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #ifndef videocore_SepiaVideoFilter_h 26 | #define videocore_SepiaVideoFilter_h 27 | #include 28 | 29 | namespace videocore { 30 | namespace filters { 31 | class SepiaVideoFilter : public IVideoFilter { 32 | 33 | public: 34 | SepiaVideoFilter(); 35 | ~SepiaVideoFilter(); 36 | 37 | public: 38 | virtual void initialize(); 39 | virtual bool initialized() const { return m_initialized; }; 40 | virtual std::string const name() { return "com.videocore.filters.sepia"; }; 41 | virtual void bind(); 42 | virtual void unbind(); 43 | 44 | public: 45 | 46 | const char * const vertexKernel() const ; 47 | const char * const pixelKernel() const ; 48 | 49 | private: 50 | static bool registerFilter(); 51 | static bool s_registered; 52 | private: 53 | 54 | unsigned int m_vao; 55 | unsigned int m_uMatrix; 56 | bool m_initialized; 57 | bool m_bound; 58 | 59 | }; 60 | } 61 | } 62 | 63 | #endif /* defined(videocore_SepiaVideoFilter_h) */ 64 | -------------------------------------------------------------------------------- /filters/FilterFactory.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | namespace videocore { 12 | std::map* FilterFactory::s_registration = nullptr ; 13 | 14 | FilterFactory::FilterFactory() { 15 | { 16 | filters::BasicVideoFilterBGRA b; 17 | filters::GrayscaleVideoFilter g; 18 | filters::InvertColorsVideoFilter i; 19 | filters::SepiaVideoFilter s; 20 | filters::FisheyeVideoFilter f; 21 | filters::GlowVideoFilter gl; 22 | filters::AntiqueVideoFilter antique; 23 | filters::BeautyVideoFilter beauty; 24 | } 25 | } 26 | IFilter* 27 | FilterFactory::filter(std::string name) { 28 | IFilter* filter = nullptr; 29 | 30 | auto it = m_filters.find(name) ; 31 | if( it != m_filters.end() ) { 32 | filter = it->second.get(); 33 | } else if (FilterFactory::s_registration != nullptr) { 34 | auto iit = FilterFactory::s_registration->find(name); 35 | 36 | if(iit != FilterFactory::s_registration->end()) { 37 | m_filters[name].reset(iit->second()); 38 | filter = m_filters[name].get(); 39 | } 40 | } 41 | 42 | return filter; 43 | } 44 | 45 | void 46 | FilterFactory::_register(std::string name, InstantiateFilter instantiation) 47 | { 48 | if(!s_registration) { 49 | s_registration = new std::map(); 50 | } 51 | s_registration->emplace(std::make_pair(name, instantiation)); 52 | } 53 | } -------------------------------------------------------------------------------- /filters/FilterFactory.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef __videocore__FilterFactory__ 4 | #define __videocore__FilterFactory__ 5 | #include 6 | #include 7 | #include 8 | #include 9 | namespace videocore { 10 | 11 | using InstantiateFilter = std::function ; 12 | 13 | class FilterFactory { 14 | public: 15 | FilterFactory(); 16 | ~FilterFactory() {}; 17 | 18 | IFilter* filter(std::string name); 19 | 20 | public: 21 | static void _register(std::string name, InstantiateFilter instantiation ); 22 | 23 | private: 24 | std::map> m_filters; 25 | 26 | private: 27 | static std::map* s_registration; 28 | }; 29 | 30 | } 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /filters/IFilter.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #ifndef videocore_IFilter_hpp 26 | #define videocore_IFilter_hpp 27 | #include 28 | 29 | namespace videocore { 30 | 31 | 32 | class IFilter { 33 | 34 | public: 35 | virtual ~IFilter() {} ; 36 | 37 | virtual void initialize() = 0; 38 | virtual bool initialized() const = 0; 39 | 40 | virtual void bind() = 0; 41 | virtual void unbind() = 0; 42 | 43 | virtual std::string const name() = 0; 44 | }; 45 | } 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /filters/IVideoFilter.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | 26 | // 27 | // and whatever you do, dont forget to smile, because if you don't smile, 28 | // then the execution would smell like funky drawers 29 | // 30 | 31 | #ifndef videocore_IVideoFilter_hpp 32 | #define videocore_IVideoFilter_hpp 33 | 34 | #include 35 | #include 36 | 37 | #define KERNEL(_language, _target, _kernelstr) if(_language == _target){ do { return # _kernelstr ; } while(0); } 38 | 39 | 40 | namespace videocore { 41 | 42 | enum FilterLanguage { 43 | GL_ES2_3, 44 | GL_2, 45 | GL_3 46 | }; 47 | 48 | class IVideoFilter : public IFilter { 49 | 50 | public: 51 | 52 | virtual ~IVideoFilter() {} ; 53 | 54 | virtual const char * const vertexKernel() const = 0; 55 | virtual const char * const pixelKernel() const = 0; 56 | 57 | 58 | public: 59 | 60 | void incomingMatrix(glm::mat4& matrix) { m_matrix = matrix; }; 61 | void imageDimensions(float w, float h) { m_dimensions.w = w; m_dimensions.h = h; }; 62 | 63 | void setFilterLanguage(FilterLanguage language) { m_language = language ; }; 64 | void setProgram(int program) { m_program = program; }; 65 | const int program() const { return m_program; }; 66 | 67 | protected: 68 | IVideoFilter() : m_program(0), m_matrix(1.f), m_dimensions({ 1.f, 1.f }), m_language(GL_ES2_3) {}; 69 | 70 | glm::mat4 m_matrix; 71 | struct { float w, h ; } m_dimensions; 72 | int m_program; 73 | 74 | FilterLanguage m_language; 75 | 76 | }; 77 | } 78 | 79 | #endif 80 | -------------------------------------------------------------------------------- /mixers/Apple/AudioMixer.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | 26 | #ifndef __videocore__AudioMixer__ 27 | #define __videocore__AudioMixer__ 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | namespace videocore { namespace Apple { 35 | /* 36 | * Takes raw LPCM buffers from a variety of sources, resamples, and mixes them and ouputs a single LPCM buffer. 37 | * Differs from GenericAudioMixer because it uses CoreAudio to resample. 38 | * 39 | */ 40 | class AudioMixer : public GenericAudioMixer 41 | { 42 | public: 43 | /*! 44 | * Constructor. 45 | * 46 | * \param outChannelCount number of channels to output. 47 | * \param outFrequencyInHz sampling rate to output. 48 | * \param outBitsPerChannel number of bits per channel to output 49 | * \param frameDuration The duration of a single frame of audio. For example, AAC uses 1024 samples per frame 50 | * and therefore the duration is 1024 / sampling rate 51 | */ 52 | AudioMixer(int outChannelCount, 53 | int outFrequencyInHz, 54 | int outBitsPerChannel, 55 | double frameDuration); 56 | 57 | /*! Destructor */ 58 | ~AudioMixer(); 59 | 60 | protected: 61 | 62 | /*! 63 | * Called to resample a buffer of audio samples. You can change the quality of the resampling method 64 | * by changing s_samplingRateConverterComplexity and s_samplingRateConverterQuality in Apple/AudioMixer.cpp. 65 | * 66 | * \param buffer The input samples 67 | * \param size The buffer size in bytes 68 | * \param metadata The associated AudioBufferMetadata that specifies the properties of this buffer. 69 | * 70 | * \return An audio buffer that has been resampled to match the output properties of the mixer. 71 | */ 72 | std::shared_ptr resample(const uint8_t* const buffer, 73 | size_t size, 74 | AudioBufferMetadata& metadata); 75 | 76 | private: 77 | /*! Used by AudioConverterFillComplexBuffer. Do not call manually. */ 78 | static OSStatus ioProc(AudioConverterRef audioConverter, 79 | UInt32 *ioNumDataPackets, 80 | AudioBufferList* ioData, 81 | AudioStreamPacketDescription** ioPacketDesc, 82 | void* inUserData ); 83 | 84 | private: 85 | 86 | using ConverterInst = struct { AudioStreamBasicDescription asbdIn, asbdOut; AudioConverterRef converter; }; 87 | 88 | std::unordered_map m_converters; 89 | 90 | 91 | }; 92 | } 93 | } 94 | #endif /* defined(__videocore__AudioMixer__) */ 95 | -------------------------------------------------------------------------------- /mixers/IAudioMixer.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #ifndef videocore_IAudioMixer_hpp 26 | #define videocore_IAudioMixer_hpp 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | namespace videocore { 33 | 34 | 35 | /*! Enum values for the AudioBufferMetadata tuple */ 36 | enum { 37 | kAudioMetadataFrequencyInHz, /*!< Specifies the sampling rate of the buffer */ 38 | kAudioMetadataBitsPerChannel, /*!< Specifies the number of bits per channel */ 39 | kAudioMetadataChannelCount, /*!< Specifies the number of channels */ 40 | kAudioMetadataFlags, /*!< Specifies the audio flags */ 41 | kAudioMetadataBytesPerFrame, /*!< Specifies the number of bytes per frame */ 42 | kAudioMetadataNumberFrames, /*!< Number of sample frames in the buffer. */ 43 | kAudioMetadataUsesOSStruct, /*!< Indicates that the audio is not raw but instead uses a platform-specific struct */ 44 | kAudioMetadataLoops, /*!< Indicates whether or not the buffer should loop. Currently ignored. */ 45 | kAudioMetadataSource /*!< A smart pointer to the source. */ 46 | }; 47 | 48 | /*! 49 | * Specifies the properties of the incoming audio buffer. 50 | */ 51 | typedef MetaData<'soun', int, int, int, int, int, int, bool, bool, std::weak_ptr > AudioBufferMetadata; 52 | 53 | class ISource; 54 | 55 | /*! IAudioMixer interface. Defines the required interface methods for Audio mixers. */ 56 | class IAudioMixer : public IMixer 57 | { 58 | public: 59 | 60 | /*! Virtual destructor */ 61 | virtual ~IAudioMixer() {}; 62 | 63 | /*! 64 | * Set the output gain of the specified source. 65 | * 66 | * \param source A smart pointer to the source to be modified 67 | * \param gain A value between 0 and 1 representing the desired gain. 68 | */ 69 | virtual void setSourceGain(std::weak_ptr source, 70 | float gain) = 0; 71 | 72 | /*! 73 | * Set the channel count. 74 | * 75 | * \param channelCount The number of audio channels. 76 | */ 77 | virtual void setChannelCount(int channelCount) = 0; 78 | 79 | /*! 80 | * Set the channel count. 81 | * 82 | * \param frequencyInHz The audio sample frequency in Hz. 83 | */ 84 | virtual void setFrequencyInHz(float frequencyInHz) = 0; 85 | 86 | /*! 87 | * Set the amount of time to buffer before emitting mixed samples. 88 | * 89 | * \param duration The duration, in seconds, to buffer. 90 | */ 91 | virtual void setMinimumBufferDuration(const double duration) = 0; 92 | }; 93 | } 94 | 95 | #endif 96 | -------------------------------------------------------------------------------- /mixers/IMixer.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #ifndef videocore_IMixer_hpp 26 | #define videocore_IMixer_hpp 27 | 28 | #include 29 | 30 | namespace videocore 31 | { 32 | class ISource; 33 | 34 | /*! 35 | * IMixer interface. Defines the interface for registering and unregistering sources with mixers. 36 | */ 37 | class IMixer : public ITransform 38 | { 39 | public: 40 | /*! 41 | * Register a source with the mixer. There may be intermediate transforms between the source and 42 | * the mixer. 43 | * 44 | * \param source A smart pointer to the source being registered. 45 | * \param inBufferSize an optional parameter to specify the expected buffer size from the source. Only useful if 46 | * the buffer size is always the same. 47 | */ 48 | virtual void registerSource(std::shared_ptr source, 49 | size_t inBufferSize = 0) = 0; 50 | /*! 51 | * Unregister a source with the mixer. 52 | * 53 | * \param source A smart pointer to the source being unregistered. 54 | */ 55 | virtual void unregisterSource(std::shared_ptr source) = 0; 56 | 57 | 58 | virtual void start() = 0; 59 | 60 | /*! Virtual destructor */ 61 | virtual ~IMixer() {}; 62 | 63 | }; 64 | } 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /mixers/IVideoMixer.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #ifndef videocore_IVideoMixer_hpp 26 | #define videocore_IVideoMixer_hpp 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | #include 33 | 34 | namespace videocore 35 | { 36 | /*! Enum values for the VideoBufferMetadata tuple */ 37 | enum { 38 | kVideoMetadataZIndex, /*!< Specifies the z-Index the buffer (lower is farther back) */ 39 | kVideoMetadataMatrix, /*!< Specifies the transformation matrix to use. Pass an Identity matrix if no transformation is to be applied. 40 | Note that the compositor operates using homogeneous coordinates (-1 to 1) unless otherwise specified. */ 41 | kVideoMetadataBlends, 42 | kVideoMetadataSource, /*!< Specifies a smart pointer to the source */ 43 | }; 44 | 45 | /*! 46 | * Specifies the properties of the incoming image buffer. 47 | */ 48 | typedef MetaData<'vide', int, glm::mat4, bool, std::weak_ptr> VideoBufferMetadata; 49 | 50 | /*! IAudioMixer interface. Defines the required interface methods for Video mixers (compositors). */ 51 | class IVideoMixer : public IMixer 52 | { 53 | public: 54 | virtual ~IVideoMixer() {}; 55 | virtual void setSourceFilter(std::weak_ptr source, IVideoFilter* filter)=0; 56 | virtual FilterFactory& filterFactory() = 0; 57 | virtual void sync() = 0; 58 | }; 59 | } 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /sources/Apple/PixelBufferSource.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (C) 2014 James G. Hurley 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public 17 | License along with this library; if not, write to the Free Software 18 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 19 | USA 20 | 21 | */ 22 | #ifndef __videocore__PixelBufferSource__ 23 | #define __videocore__PixelBufferSource__ 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | #ifdef __APPLE__ 30 | # include 31 | #else 32 | # include 33 | 34 | typedef uint32_t OSType; 35 | 36 | #endif 37 | 38 | namespace videocore { namespace Apple { 39 | 40 | class PixelBufferSource : public ISource, public std::enable_shared_from_this 41 | { 42 | 43 | public: 44 | 45 | /* 46 | * @param width The desired width of the pixel buffer 47 | * @param height The desired height of the pixel buffer 48 | * @param pixelFormat The FourCC format of the pixel data. 49 | */ 50 | PixelBufferSource(int width, int height, OSType pixelFormat ); 51 | ~PixelBufferSource(); 52 | 53 | void setOutput(std::shared_ptr output); 54 | 55 | public: 56 | 57 | void pushPixelBuffer(void* data, size_t size); 58 | 59 | private: 60 | std::weak_ptr m_output; 61 | void* m_pixelBuffer; 62 | int m_width; 63 | int m_height; 64 | OSType m_pixelFormat; 65 | 66 | }; 67 | } 68 | } 69 | 70 | #endif /* defined(__videocore__PixelBufferSource__) */ 71 | -------------------------------------------------------------------------------- /sources/Apple/PixelBufferSource.mm: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (C) 2014 James G. Hurley 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public 17 | License along with this library; if not, write to the Free Software 18 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 19 | USA 20 | 21 | */ 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include 29 | 30 | namespace videocore { namespace Apple { 31 | 32 | PixelBufferSource::PixelBufferSource(int width, int height, OSType pixelFormat ) 33 | : m_pixelBuffer(nullptr), m_width(width), m_height(height), m_pixelFormat(pixelFormat) 34 | { 35 | CVPixelBufferRef pb = nullptr; 36 | CVReturn ret = kCVReturnSuccess; 37 | @autoreleasepool { 38 | NSDictionary* pixelBufferOptions = @{ (NSString*) kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA), 39 | (NSString*) kCVPixelBufferWidthKey : @(width), 40 | (NSString*) kCVPixelBufferHeightKey : @(height), 41 | (NSString*) kCVPixelBufferOpenGLESCompatibilityKey : @YES, 42 | (NSString*) kCVPixelBufferIOSurfacePropertiesKey : @{}}; 43 | 44 | ret = CVPixelBufferCreate(kCFAllocatorDefault, width, height, pixelFormat, (__bridge CFDictionaryRef)pixelBufferOptions, &pb); 45 | } 46 | if(!ret) { 47 | m_pixelBuffer = pb; 48 | } else { 49 | throw std::runtime_error("PixelBuffer creation failed"); 50 | } 51 | } 52 | PixelBufferSource::~PixelBufferSource() 53 | { 54 | if(m_pixelBuffer) { 55 | CVPixelBufferRelease((CVPixelBufferRef)m_pixelBuffer); 56 | m_pixelBuffer = nullptr; 57 | } 58 | } 59 | 60 | void 61 | PixelBufferSource::pushPixelBuffer(void *data, size_t size) 62 | { 63 | 64 | auto outp = m_output.lock(); 65 | 66 | if(outp) { 67 | CVPixelBufferLockBaseAddress((CVPixelBufferRef)m_pixelBuffer, 0); 68 | void* loc = CVPixelBufferGetBaseAddress((CVPixelBufferRef)m_pixelBuffer); 69 | memcpy(loc, data, size); 70 | CVPixelBufferUnlockBaseAddress((CVPixelBufferRef)m_pixelBuffer, 0); 71 | 72 | glm::mat4 mat = glm::mat4(1.f); 73 | VideoBufferMetadata md(0.); 74 | md.setData(4, mat, true, shared_from_this()); 75 | auto pixelBuffer = std::make_shared((CVPixelBufferRef)m_pixelBuffer, false); 76 | outp->pushBuffer((const uint8_t*)&pixelBuffer, sizeof(pixelBuffer), md); 77 | } 78 | } 79 | void 80 | PixelBufferSource::setOutput(std::shared_ptr output) 81 | { 82 | m_output = output; 83 | } 84 | 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /sources/ISource.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | 26 | #ifndef videocore_ISource_hpp 27 | #define videocore_ISource_hpp 28 | 29 | #include 30 | #include 31 | 32 | namespace videocore 33 | { 34 | /*! 35 | * ISource interface. Defines the interface for sources of data into a graph. 36 | */ 37 | class ISource 38 | { 39 | public: 40 | /*! 41 | * Set the output for the source. 42 | * 43 | * \param output a component that conforms to the videocore::IOutput interface and is compatible with the 44 | * data being vended by the source. 45 | */ 46 | virtual void setOutput(std::shared_ptr output) = 0; 47 | 48 | virtual void setFilter(std::shared_ptr) {} ; 49 | virtual IFilter* const filter() { return nullptr; }; 50 | 51 | /*! Virtual destructor */ 52 | virtual ~ISource() {}; 53 | }; 54 | 55 | } 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /sources/iOS/GLESUtil.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #ifndef videocore_GLESUtil_h 26 | #define videocore_GLESUtil_h 27 | 28 | #import 29 | #import 30 | #import 31 | 32 | #define BUFFER_OFFSET(i) ((void*)(i)) 33 | #define BUFFER_OFFSET_POSITION BUFFER_OFFSET(0) 34 | #define BUFFER_OFFSET_TEXTURE BUFFER_OFFSET(8) 35 | #define BUFFER_SIZE_POSITION 2 36 | #define BUFFER_SIZE_TEXTURE 2 37 | #define BUFFER_STRIDE (sizeof(float) * 4) 38 | 39 | #ifdef DEBUG 40 | #define GL_ERRORS(line) { GLenum glerr; while((glerr = glGetError())) {\ 41 | switch(glerr)\ 42 | {\ 43 | case GL_NO_ERROR:\ 44 | break;\ 45 | case GL_INVALID_ENUM:\ 46 | DLog("OGL(" __FILE__ "):: %d: Invalid Enum\n", line );\ 47 | break;\ 48 | case GL_INVALID_VALUE:\ 49 | DLog("OGL(" __FILE__ "):: %d: Invalid Value\n", line );\ 50 | break;\ 51 | case GL_INVALID_OPERATION:\ 52 | DLog("OGL(" __FILE__ "):: %d: Invalid Operation\n", line );\ 53 | break;\ 54 | case GL_OUT_OF_MEMORY:\ 55 | DLog("OGL(" __FILE__ "):: %d: Out of Memory\n", line );\ 56 | break;\ 57 | } } } 58 | 59 | #define GL_FRAMEBUFFER_STATUS(line) { GLenum status; status = glCheckFramebufferStatus(GL_FRAMEBUFFER); {\ 60 | switch(status)\ 61 | {\ 62 | case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:\ 63 | DLog("OGL(" __FILE__ "):: %d: Incomplete attachment\n", line);\ 64 | break;\ 65 | case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS:\ 66 | DLog("OGL(" __FILE__ "):: %d: Incomplete dimensions\n", line);\ 67 | break;\ 68 | case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:\ 69 | DLog("OGL(" __FILE__ "):: %d: Incomplete missing attachment\n", line);\ 70 | break;\ 71 | case GL_FRAMEBUFFER_UNSUPPORTED:\ 72 | DLog("OGL(" __FILE__ "):: %d: Framebuffer combination unsupported\n",line);\ 73 | break;\ 74 | } } } 75 | 76 | #else 77 | #define GL_ERRORS(line) 78 | #define GL_FRAMEBUFFER_STATUS(line) 79 | #endif 80 | 81 | #include 82 | 83 | static float s_vbo [] = 84 | { 85 | -1.f, -1.f, 0.f, 0.f, // 0 86 | 1.f, -1.f, 1.f, 0.f, // 1 87 | -1.f, 1.f, 0.f, 1.f, // 2 88 | 89 | 1.f, -1.f, 1.f, 0.f, // 1 90 | 1.f, 1.f, 1.f, 1.f, // 3 91 | -1.f, 1.f, 0.f, 1.f, // 2 92 | }; 93 | 94 | 95 | 96 | static const char s_vs [] = 97 | "attribute vec2 aPos;" 98 | "attribute vec2 aCoord;" 99 | "varying vec2 vCoord;" 100 | "void main(void) {" 101 | "gl_Position = vec4(aPos,0.,1.);" 102 | "vCoord = aCoord;" 103 | "}"; 104 | static const char s_vs_mat [] = 105 | "attribute vec2 aPos;" 106 | "attribute vec2 aCoord;" 107 | "varying vec2 vCoord;" 108 | "uniform mat4 uMat;" 109 | "void main(void) {" 110 | "gl_Position = uMat * vec4(aPos,0.,1.);" 111 | "vCoord = aCoord;" 112 | "}" 113 | ; 114 | static const char s_fs[] = 115 | "precision mediump float;" 116 | "varying vec2 vCoord;" 117 | "uniform sampler2D uTex0;" 118 | "void main(void) {" 119 | "gl_FragData[0] = texture2D(uTex0, vCoord);" 120 | "}"; 121 | 122 | static inline GLuint compile_shader(GLuint type, const char * source) 123 | { 124 | 125 | GLuint shader = glCreateShader(type); 126 | glShaderSource(shader, 1, &source, NULL); 127 | glCompileShader(shader); 128 | GLint compiled; 129 | glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); 130 | #ifdef DEBUG 131 | if (!compiled) { 132 | GLint length; 133 | char *log; 134 | 135 | glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length); 136 | 137 | log = (char*)malloc((size_t)(length)); 138 | glGetShaderInfoLog(shader, length, &length, &log[0]); 139 | DLog("%s compilation error: %s\n", (type == GL_VERTEX_SHADER ? "GL_VERTEX_SHADER" : "GL_FRAGMENT_SHADER"), log); 140 | free(log); 141 | 142 | return 0; 143 | } 144 | #endif 145 | return shader; 146 | 147 | } 148 | 149 | static inline GLuint build_program(const char * vertex, const char * fragment) 150 | { 151 | GLuint vshad, 152 | fshad, 153 | p; 154 | 155 | GLint len; 156 | #ifdef DEBUG 157 | char* log; 158 | #endif 159 | 160 | vshad = compile_shader(GL_VERTEX_SHADER, vertex); 161 | fshad = compile_shader(GL_FRAGMENT_SHADER, fragment); 162 | 163 | p = glCreateProgram(); 164 | glAttachShader(p, vshad); 165 | glAttachShader(p, fshad); 166 | glLinkProgram(p); 167 | glGetProgramiv(p,GL_INFO_LOG_LENGTH, &len); 168 | 169 | 170 | #ifdef DEBUG 171 | if(len) { 172 | log = (char*)malloc ( (size_t)(len) ) ; 173 | 174 | glGetProgramInfoLog(p, len, &len, log); 175 | 176 | DLog("program log: %s\n", log); 177 | free(log); 178 | } 179 | #endif 180 | 181 | 182 | glDeleteShader(vshad); 183 | glDeleteShader(fshad); 184 | return p; 185 | } 186 | 187 | 188 | #endif 189 | -------------------------------------------------------------------------------- /sources/iOS/MicSource.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #ifndef __videocore__MicSource__ 26 | #define __videocore__MicSource__ 27 | 28 | #include 29 | #import 30 | #import 31 | #include 32 | #include 33 | 34 | #import 35 | 36 | @class InterruptionHandler; 37 | 38 | namespace videocore { namespace iOS { 39 | 40 | /*! 41 | * Capture audio from the device's microphone. 42 | * 43 | */ 44 | 45 | class MicSource : public ISource, public std::enable_shared_from_this 46 | { 47 | public: 48 | 49 | /*! 50 | * Constructor. 51 | * 52 | * \param audioSampleRate the sample rate in Hz to capture audio at. Best results if this matches the mixer's sampling rate. 53 | * \param excludeAudioUnit An optional lambda method that is called when the source generates its Audio Unit. 54 | * The parameter of this method will be a reference to its Audio Unit. This is useful for 55 | * applications that may be capturing Audio Unit data and do not wish to capture this source. 56 | * 57 | */ 58 | MicSource(double sampleRate = 44100., 59 | int channelCount = 2, 60 | std::function excludeAudioUnit = nullptr); 61 | 62 | /*! Destructor */ 63 | ~MicSource(); 64 | 65 | void start(); 66 | void stop(); 67 | 68 | public: 69 | 70 | /*! ISource::setOutput */ 71 | void setOutput(std::shared_ptr output); 72 | 73 | /*! Used by the Audio Unit as a callback method */ 74 | void inputCallback(uint8_t* data, size_t data_size, int inNumberFrames); 75 | 76 | void interruptionBegan(); 77 | void interruptionEnded(); 78 | 79 | /*! 80 | * \return a reference to the source's Audio Unit 81 | */ 82 | const AudioUnit& audioUnit() const { return m_audioUnit; }; 83 | 84 | private: 85 | 86 | InterruptionHandler* m_interruptionHandler; 87 | 88 | AudioComponentInstance m_audioUnit; 89 | AudioComponent m_component; 90 | 91 | double m_sampleRate; 92 | int m_channelCount; 93 | 94 | std::weak_ptr m_output; 95 | 96 | }; 97 | 98 | } 99 | } 100 | @interface InterruptionHandler : NSObject 101 | { 102 | @public 103 | videocore::iOS::MicSource* _source; 104 | } 105 | - (void) handleInterruption: (NSNotification*) notification; 106 | @end 107 | 108 | #endif /* defined(__videocore__MicSource__) */ 109 | -------------------------------------------------------------------------------- /stream/Apple/StreamSession.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #ifndef __videocore__StreamSession__ 26 | #define __videocore__StreamSession__ 27 | 28 | #include 29 | #include 30 | 31 | #include "IStreamSession.hpp" 32 | 33 | namespace videocore { 34 | namespace Apple { 35 | 36 | class StreamSession : public IStreamSession 37 | { 38 | public: 39 | StreamSession(); 40 | ~StreamSession(); 41 | 42 | void connect(const std::string& host, int port, StreamSessionCallback_T) override; 43 | void disconnect() override; 44 | 45 | ssize_t write(uint8_t* buffer, size_t size) override; 46 | ssize_t read(uint8_t* buffer, size_t size) override; 47 | 48 | const StreamStatus_T status() const override { 49 | return m_status; 50 | }; 51 | public: 52 | void nsStreamCallback(void* stream, unsigned event); 53 | 54 | private: 55 | void setStatus(StreamStatus_T status, bool clear = false) override; 56 | void startNetwork(); 57 | private: 58 | void* m_inputStream; 59 | void* m_outputStream; 60 | void* m_runLoop; 61 | void* m_streamCallback; 62 | 63 | std::mutex m_connectedMutex; 64 | std::condition_variable m_connectedEvent; 65 | StreamSessionCallback_T m_callback; 66 | StreamStatus_T m_status; 67 | 68 | int m_outSocket; 69 | 70 | }; 71 | } 72 | } 73 | 74 | 75 | 76 | #endif /* defined(__videocore__StreamSession__) */ 77 | -------------------------------------------------------------------------------- /stream/IStreamSession.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #ifndef videocore_IStream_hpp 26 | #define videocore_IStream_hpp 27 | 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | namespace videocore { 34 | 35 | enum { 36 | 37 | kStreamStatusConnected = 1, 38 | kStreamStatusWriteBufferHasSpace = 1 << 1, 39 | kStreamStatusReadBufferHasBytes = 1 << 2, 40 | kStreamStatusErrorEncountered = 1 << 3, 41 | kStreamStatusEndStream = 1 << 4 42 | 43 | } ; 44 | typedef long StreamStatus_T; 45 | 46 | class IStreamSession; 47 | 48 | typedef std::function StreamSessionCallback_T; 49 | class IStreamSession 50 | { 51 | public: 52 | virtual ~IStreamSession() {}; 53 | 54 | virtual void connect(const std::string& host, int port, StreamSessionCallback_T sscb) = 0; 55 | virtual void disconnect() = 0; 56 | virtual ssize_t write(uint8_t* buffer, size_t size) = 0; 57 | virtual ssize_t read(uint8_t* buffer, size_t size) = 0; 58 | virtual const StreamStatus_T status() const = 0; 59 | 60 | private: 61 | virtual void setStatus(StreamStatus_T,bool clear = false) = 0; 62 | }; 63 | 64 | } 65 | 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /stream/IThroughputAdaptation.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #ifndef __videocore__IThroughputAdaptation__ 26 | #define __videocore__IThroughputAdaptation__ 27 | 28 | #include 29 | #include 30 | 31 | namespace videocore { 32 | 33 | using ThroughputCallback = std::function; 34 | 35 | 36 | class IThroughputAdaptation { 37 | public: 38 | virtual ~IThroughputAdaptation() {}; 39 | 40 | virtual void setThroughputCallback(ThroughputCallback callback) = 0; 41 | 42 | virtual void addSentBytesSample(size_t bytesSent) = 0; 43 | 44 | virtual void addBufferSizeSample(size_t bufferSize) = 0; 45 | 46 | virtual void addBufferDurationSample(int64_t bufferDuration) = 0; 47 | 48 | virtual void reset() = 0; 49 | 50 | virtual void start() = 0; 51 | }; 52 | } 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /stream/TCPThroughputAdaptation.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #ifndef __videocore__TCPThroughputAdaptation__ 26 | #define __videocore__TCPThroughputAdaptation__ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | namespace videocore { 36 | class TCPThroughputAdaptation : public IThroughputAdaptation 37 | { 38 | public: 39 | TCPThroughputAdaptation(); 40 | ~TCPThroughputAdaptation(); 41 | 42 | public: 43 | 44 | void setThroughputCallback(ThroughputCallback callback); 45 | 46 | void addSentBytesSample(size_t bytesSent); 47 | 48 | void addBufferSizeSample(size_t bufferSize); 49 | 50 | void addBufferDurationSample(int64_t bufferDuration); 51 | 52 | void reset(); 53 | void start(); 54 | private: 55 | void sampleThread(); 56 | 57 | private: 58 | 59 | std::chrono::steady_clock::time_point m_previousTurndown; 60 | std::chrono::steady_clock::time_point m_previousIncrease; 61 | 62 | std::thread m_thread; 63 | std::condition_variable m_cond; 64 | std::mutex m_sentMutex; 65 | std::mutex m_buffMutex; 66 | std::mutex m_durMutex; 67 | 68 | std::vector m_sentSamples; 69 | std::vector m_bufferSizeSamples; 70 | std::vector m_bufferDurationSamples; 71 | 72 | std::deque m_bwSamples; 73 | std::deque m_buffGrowth; 74 | std::deque m_turnSamples; 75 | std::vector m_bwWeights; 76 | 77 | ThroughputCallback m_callback; 78 | 79 | int m_bwSampleCount; 80 | int m_negSampleCount; 81 | 82 | float m_previousVector; 83 | 84 | bool m_started; 85 | bool m_exiting; 86 | bool m_hasFirstTurndown; 87 | 88 | }; 89 | } 90 | 91 | #endif 92 | -------------------------------------------------------------------------------- /system/Logger.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Loggeer.cpp 3 | // Pods 4 | // 5 | // Created by jinchu darwin on 15/10/8. 6 | // 7 | // 8 | 9 | #include 10 | 11 | #include 12 | 13 | namespace videocore { 14 | 15 | // 简单的实现,直接输出日志就好 16 | void Logger::log(bool synchronous, int level, int flag, int ctx, const char *file, const char *function, int line, const char *format, ...) 17 | { 18 | va_list args; 19 | va_start(args, format); 20 | 21 | vfprintf(stdout, format, args); 22 | va_end(args); 23 | } 24 | 25 | void Logger::dumpBuffer(const char *desc, uint8_t *buf, size_t size, const char *sep, size_t breaklen) { 26 | char hexBuf[size * 4 + 1]; 27 | int j = 0; 28 | for (size_t i=0; i 13 | #include 14 | 15 | namespace videocore { 16 | class Logger { 17 | public: 18 | static void log(bool synchronous, 19 | int level, 20 | int flag, 21 | int ctx, 22 | const char *file, 23 | const char *function, 24 | int line, 25 | const char* frmt, 26 | ...) __attribute__ ((format (printf, 8, 9))); 27 | static void dumpBuffer(const char *desc, uint8_t *buf, size_t size, const char *sep=" ", size_t breaklen = 16); 28 | }; 29 | 30 | // Define DLOG_LEVEL_DEF to control the output log message 31 | #define DLOG_MACRO(isAsynchronous, lvl, flg, ctx, fnct, frmt, ...) \ 32 | videocore::Logger::log(isAsynchronous, lvl, flg, ctx, __FILE__, fnct, __LINE__, frmt, ##__VA_ARGS__); 33 | 34 | #define SYNC_DLOG_MACRO(lvl, flg, ctx, frmt, ...) \ 35 | DLOG_MACRO(false, lvl, flg, ctx, frmt, ##__VA_ARGS__) 36 | 37 | #define ASYNC_DLOG_MACRO(lvl, flg, ctx, frmt, ...) \ 38 | DLOG_MACRO(true , lvl, flg, ctx, frmt, ##__VA_ARGS__) 39 | } 40 | 41 | 42 | #define DLOG_MAYBE(async, lvl, flg, ctx, frmt, ...) \ 43 | do { if(lvl & flg) DLOG_MACRO(async, lvl, flg, ctx, __FUNCTION__, frmt, ##__VA_ARGS__); } while(0) 44 | 45 | #define SYNC_DLOG_MAYBE(lvl, flg, ctx, frmt, ...) \ 46 | DLOG_MAYBE( NO, lvl, flg, ctx, frmt, ##__VA_ARGS__) 47 | 48 | #define ASYNC_LOG_MAYBE(lvl, flg, ctx, frmt, ...) \ 49 | DLOG_MAYBE(YES, lvl, flg, ctx, frmt, ##__VA_ARGS__) 50 | 51 | // FLAG value 52 | #define DLOG_FLAG_ERROR (1 << 0) 53 | #define DLOG_FLAG_WARN (1 << 1) 54 | #define DLOG_FLAG_INFO (1 << 2) 55 | #define DLOG_FLAG_DEBUG (1 << 3) 56 | #define DLOG_FLAG_VERBOSE (1 << 4) 57 | 58 | // LAVEL value 59 | #define DLOG_LEVEL_OFF 0 60 | #define DLOG_LEVEL_ERROR (DLOG_FLAG_ERROR) 61 | #define DLOG_LEVEL_WARN (DLOG_FLAG_ERROR | DLOG_FLAG_WARN) 62 | #define DLOG_LEVEL_INFO (DLOG_FLAG_ERROR | DLOG_FLAG_WARN | DLOG_FLAG_INFO) 63 | #define DLOG_LEVEL_DEBUG (DLOG_FLAG_ERROR | DLOG_FLAG_WARN | DLOG_FLAG_INFO | DLOG_FLAG_DEBUG) 64 | #define DLOG_LEVEL_VERBOSE (DLOG_FLAG_ERROR | DLOG_FLAG_WARN | DLOG_FLAG_INFO | DLOG_FLAG_DEBUG | DLOG_FLAG_VERBOSE) 65 | 66 | // Can log value 67 | #define DLOG_ERROR (DLOG_LEVEL_DEF & DLOG_FLAG_ERROR) 68 | #define DLOG_WARN (DLOG_LEVEL_DEF & DLOG_FLAG_WARN) 69 | #define DLOG_INFO (DLOG_LEVEL_DEF & DLOG_FLAG_INFO) 70 | #define DLOG_DEBUG (DLOG_LEVEL_DEF & DLOG_FLAG_DEBUG) 71 | #define DLOG_VERBOSE (DLOG_LEVEL_DEF & DLOG_FLAG_VERBOSE) 72 | 73 | #define DLOG_ASYNC_ENABLED 1 74 | 75 | // Can async log value 76 | #define DLOG_ASYNC_ERROR (0 && DLOG_ASYNC_ENABLED) 77 | #define DLOG_ASYNC_WARN (1 && DLOG_ASYNC_ENABLED) 78 | #define DLOG_ASYNC_INFO (1 && DLOG_ASYNC_ENABLED) 79 | #define DLOG_ASYNC_DEBUG (1 && DLOG_ASYNC_ENABLED) 80 | #define DLOG_ASYNC_VERBOSE (1 && DLOG_ASYNC_ENABLED) 81 | 82 | // Log macros 83 | #define DLogError(frmt, ...) DLOG_MAYBE(DLOG_ASYNC_ERROR, DLOG_LEVEL_DEF, DLOG_FLAG_ERROR, 0, frmt, ##__VA_ARGS__) 84 | #define DLogWarn(frmt, ...) DLOG_MAYBE(DLOG_ASYNC_WARN, DLOG_LEVEL_DEF, DLOG_FLAG_WARN, 0, frmt, ##__VA_ARGS__) 85 | #define DLogInfo(frmt, ...) DLOG_MAYBE(DLOG_ASYNC_INFO, DLOG_LEVEL_DEF, DLOG_FLAG_INFO, 0, frmt, ##__VA_ARGS__) 86 | #define DLogDebug(frmt, ...) DLOG_MAYBE(DLOG_ASYNC_DEBUG, DLOG_LEVEL_DEF, DLOG_FLAG_DEBUG, 0, frmt, ##__VA_ARGS__) 87 | #define DLogVerbose(frmt, ...) DLOG_MAYBE(DLOG_ASYNC_VERBOSE, DLOG_LEVEL_DEF, DLOG_FLAG_VERBOSE, 0, frmt, ##__VA_ARGS__) 88 | 89 | #endif /* Loggeer_hpp */ 90 | -------------------------------------------------------------------------------- /system/PreBuffer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // PreBuffer.cpp 3 | // Pods 4 | // 5 | // Created by jinchu darwin on 15/10/10. 6 | // 7 | // 8 | 9 | #include "PreBuffer.hpp" 10 | 11 | #include 12 | 13 | 14 | #include 15 | #include 16 | 17 | namespace videocore { 18 | /** 19 | * Pre-allocated buffer. Design for get the raw pointer for read and write 20 | * Must alloc enough space before writing 21 | * Must check how many byte in buffer before reading 22 | */ 23 | PreallocBuffer::PreallocBuffer(size_t capBytes){ 24 | m_preBufferSize = capBytes; 25 | m_preBuffer = (uint8_t *)malloc(m_preBufferSize); 26 | 27 | m_readPointer = m_preBuffer; 28 | m_writePointer = m_preBuffer; 29 | } 30 | 31 | PreallocBuffer::~PreallocBuffer(){ 32 | if (m_preBuffer) { 33 | free(m_preBuffer); 34 | } 35 | } 36 | 37 | /** 38 | * Make sure buffer has enough size for write 39 | * 40 | * @param capBytes ensure size 41 | */ 42 | void PreallocBuffer::ensureCapacityForWrite(size_t capBytes){ 43 | size_t availableSpace = this->availableSpace(); 44 | if (capBytes > availableSpace) { 45 | size_t additionalBytes = capBytes - availableSpace; 46 | size_t newPreBufferSize = m_preBufferSize + additionalBytes; 47 | uint8_t *newPreBuffer = (uint8_t *)realloc(m_preBuffer, newPreBufferSize); 48 | 49 | size_t readPointerOffset = m_readPointer - m_preBuffer; 50 | size_t writePointerOffset = m_writePointer - m_preBuffer; 51 | 52 | m_preBuffer = newPreBuffer; 53 | m_preBufferSize = newPreBufferSize; 54 | m_readPointer = m_preBuffer + readPointerOffset; 55 | m_writePointer = m_preBuffer + writePointerOffset; 56 | } 57 | } 58 | 59 | /** 60 | * Check how much bytes can be read 61 | * 62 | * @return read size 63 | */ 64 | size_t PreallocBuffer::availableBytes(){ 65 | return m_writePointer - m_readPointer; 66 | } 67 | 68 | /** 69 | * Raw read pointer 70 | * 71 | */ 72 | uint8_t *PreallocBuffer::readBuffer(){ 73 | return m_readPointer; 74 | } 75 | 76 | 77 | /** 78 | * Get raw pointer and availble size for read 79 | * 80 | * @param bufferPtr raw read pointer 81 | * @param availableBytesPtr how many bytes can read 82 | */ 83 | void PreallocBuffer::getReadBuffer(uint8_t **bufferPtr, size_t *availableBytesPtr){ 84 | if (bufferPtr) *bufferPtr = m_readPointer; 85 | if (availableBytesPtr) *availableBytesPtr = availableBytes(); 86 | } 87 | 88 | /** 89 | * Confirm read 90 | * 91 | */ 92 | void PreallocBuffer::didRead(size_t bytesRead){ 93 | m_readPointer += bytesRead; 94 | 95 | assert(m_readPointer <= m_writePointer); 96 | 97 | if (m_readPointer == m_writePointer) 98 | { 99 | // 缓冲区都被读走了,可以重置了 100 | reset(); 101 | } 102 | } 103 | 104 | /** 105 | * How many space for write 106 | * 107 | */ 108 | size_t PreallocBuffer::availableSpace(){ 109 | return m_preBufferSize - (m_writePointer - m_preBuffer); 110 | } 111 | 112 | /** 113 | * Raw write pointer 114 | * 115 | */ 116 | uint8_t *PreallocBuffer::writeBuffer(){ 117 | return m_writePointer; 118 | } 119 | 120 | /** 121 | * Get raw pointer and availble size for write 122 | * 123 | * @param bufferPtr raw write pointer 124 | * @param availableSpacePtr how many bytes can write 125 | */ 126 | void PreallocBuffer::getWriteBuffer(uint8_t **bufferPtr, size_t *availableSpacePtr){ 127 | if (bufferPtr) *bufferPtr = m_writePointer; 128 | if (availableSpacePtr) *availableSpacePtr = availableSpace(); 129 | } 130 | 131 | /** 132 | * Confirm written 133 | * 134 | */ 135 | void PreallocBuffer::didWrite(size_t bytesWritten){ 136 | m_writePointer += bytesWritten; 137 | assert(m_writePointer <= (m_preBuffer + m_preBufferSize)); 138 | } 139 | 140 | void PreallocBuffer::reset(){ 141 | m_readPointer = m_preBuffer; 142 | m_writePointer = m_preBuffer; 143 | } 144 | 145 | void PreallocBuffer::dumpInfo(){ 146 | DLog("PreallocBuffer begin:%p, writer:%p(%zd), reader:%p(%zd)\n", m_preBuffer, m_writePointer, availableSpace(), m_readPointer, availableBytes()); 147 | } 148 | 149 | } 150 | -------------------------------------------------------------------------------- /system/PreBuffer.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // PreBuffer.hpp 3 | // Pods 4 | // 5 | // Created by jinchu darwin on 15/10/10. 6 | // 7 | // 8 | 9 | #ifndef PreBuffer_hpp 10 | #define PreBuffer_hpp 11 | 12 | #include 13 | 14 | namespace videocore { 15 | class PreallocBuffer { 16 | public: 17 | PreallocBuffer(size_t capBytes); 18 | ~PreallocBuffer(); 19 | 20 | void ensureCapacityForWrite(size_t capBytes); 21 | 22 | size_t availableBytes(); // for read 23 | uint8_t *readBuffer(); 24 | 25 | void getReadBuffer(uint8_t **bufferPtr, size_t *availableBytesPtr); 26 | 27 | void didRead(size_t bytesRead); 28 | 29 | size_t availableSpace(); // for write 30 | uint8_t *writeBuffer(); 31 | 32 | void getWriteBuffer(uint8_t **bufferPtr, size_t *availableSpacePtr); 33 | 34 | void didWrite(size_t bytesWritten); 35 | 36 | void reset(); 37 | 38 | void dumpInfo(); 39 | 40 | private: 41 | uint8_t *m_preBuffer; 42 | size_t m_preBufferSize; 43 | 44 | uint8_t *m_readPointer; 45 | uint8_t *m_writePointer; 46 | }; 47 | } 48 | #endif /* PreBuffer_hpp */ 49 | 50 | -------------------------------------------------------------------------------- /system/h264/Golomb.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #include 26 | 27 | namespace videocore { namespace h264 { 28 | 29 | GolombDecode::GolombDecode(const WORD* inBitstream) 30 | : m_pCurrentPosition(const_cast(inBitstream)), m_leftBit(kBufWidth), m_bitsRead(0), m_lastBitsRead(0) 31 | { 32 | 33 | } 34 | GolombDecode::~GolombDecode() 35 | { 36 | 37 | } 38 | 39 | uint32_t 40 | GolombDecode::getBits(const size_t num_bits) 41 | { 42 | uint32_t val = 0; 43 | WORD bytes = *m_pCurrentPosition; 44 | const size_t bufWidth = kBufWidth; 45 | const size_t bitCount = std::min(num_bits, sizeof(WORD)*8); 46 | 47 | if(bitCount <= m_leftBit) 48 | { 49 | val = (bytes >> (bufWidth - bitCount)); 50 | m_leftBit -= bitCount; 51 | bytes <<= bitCount; 52 | 53 | } else { 54 | unsigned long remain = bitCount - m_leftBit; 55 | val = (bytes >> (bufWidth - m_leftBit)) << remain; 56 | bytes = *(++m_pCurrentPosition); 57 | val |= (bytes >> (bufWidth - remain)); 58 | bytes <<= remain; 59 | m_leftBit = bufWidth - remain; 60 | } 61 | m_bitsRead += bitCount; 62 | m_lastBitsRead = bitCount; 63 | 64 | *m_pCurrentPosition = bytes; 65 | 66 | if(!m_leftBit) { 67 | m_leftBit = bufWidth; 68 | m_pCurrentPosition++; 69 | } 70 | 71 | return val; 72 | } 73 | 74 | uint32_t 75 | GolombDecode::unsignedDecode() 76 | { 77 | WORD bytes = *m_pCurrentPosition; 78 | uint32_t ret = 0; 79 | const size_t bufWidth = kBufWidth; 80 | 81 | if(bytes!=0) 82 | { 83 | int number_of_bits = __builtin_clz(bytes); 84 | ret = getBits(number_of_bits * 2 + 1); 85 | ret--; 86 | 87 | } 88 | else if(m_leftBit > 0) 89 | { 90 | int number_of_bits; 91 | bytes = *(++m_pCurrentPosition); 92 | number_of_bits = __builtin_clz(bytes) +static_cast( m_leftBit ); 93 | m_leftBit = bufWidth; 94 | ret = getBits(number_of_bits*2+1); 95 | ret--; 96 | 97 | } 98 | 99 | return ret; 100 | } 101 | int32_t 102 | GolombDecode::signedDecode() 103 | { 104 | uint32_t unsignedVal = unsignedDecode(); 105 | 106 | int32_t val = unsignedVal >> 1 ; 107 | 108 | if( unsignedVal & 1 ) 109 | { 110 | val *= -1; 111 | } 112 | 113 | return val; 114 | } 115 | } 116 | } -------------------------------------------------------------------------------- /system/h264/Golomb.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #ifndef __videocore__Golomb__ 26 | #define __videocore__Golomb__ 27 | 28 | #include 29 | namespace videocore { namespace h264 { 30 | 31 | typedef uint32_t WORD; 32 | 33 | const size_t kBufWidth = sizeof(WORD)*8; 34 | 35 | static inline WORD swap(WORD input) { return __builtin_bswap32(input); }; 36 | 37 | class GolombDecode 38 | { 39 | public: 40 | 41 | GolombDecode(const WORD* inBitstream); 42 | ~GolombDecode(); 43 | 44 | uint32_t getBits(const size_t num_bits); 45 | uint32_t unsignedDecode(); 46 | int32_t signedDecode(); 47 | 48 | const size_t bitsRead() const { return m_bitsRead; }; 49 | const size_t lastBitsRead() const { return m_lastBitsRead; }; 50 | private: 51 | 52 | 53 | WORD* m_pCurrentPosition; 54 | 55 | size_t m_leftBit; 56 | size_t m_bitsRead; 57 | size_t m_lastBitsRead; 58 | }; 59 | 60 | } 61 | } 62 | #endif /* defined(__videocore__Golomb__) */ 63 | -------------------------------------------------------------------------------- /system/pixelBuffer/Apple/PixelBuffer.cpp: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | 4 | Video Core 5 | Copyright (c) 2014 James G. Hurley 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #include 28 | 29 | namespace videocore { namespace Apple { 30 | 31 | PixelBuffer::PixelBuffer(CVPixelBufferRef pb, bool temporary) 32 | : m_state(kVCPixelBufferStateAvailable), 33 | m_locked(false), 34 | m_pixelBuffer(CVPixelBufferRetain(pb)), 35 | m_temporary(temporary) 36 | { 37 | m_pixelFormat = (PixelBufferFormatType)CVPixelBufferGetPixelFormatType(pb); 38 | } 39 | PixelBuffer::~PixelBuffer() 40 | { 41 | CVPixelBufferRelease(m_pixelBuffer); 42 | } 43 | 44 | void 45 | PixelBuffer::lock(bool readonly) 46 | { 47 | m_locked = true; 48 | //CVPixelBufferLockBaseAddress( (CVPixelBufferRef)cvBuffer(), readonly ? kCVPixelBufferLock_ReadOnly : 0 ); 49 | } 50 | void 51 | PixelBuffer::unlock(bool readonly) 52 | { 53 | m_locked = false; 54 | //CVPixelBufferUnlockBaseAddress( (CVPixelBufferRef)cvBuffer(), readonly ? kCVPixelBufferLock_ReadOnly : 0 ); 55 | } 56 | 57 | } 58 | } -------------------------------------------------------------------------------- /system/pixelBuffer/Apple/PixelBuffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #ifndef videocore_PixelBuffer_hpp 26 | #define videocore_PixelBuffer_hpp 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | namespace videocore { namespace Apple { 33 | 34 | 35 | class PixelBuffer : public IPixelBuffer 36 | { 37 | 38 | public: 39 | 40 | PixelBuffer(CVPixelBufferRef pb, bool temporary = false); 41 | ~PixelBuffer(); 42 | 43 | static const size_t hash(std::shared_ptr buf) { return std::hash>()(buf); }; 44 | 45 | public: 46 | const int width() const { return (int)CVPixelBufferGetWidth(m_pixelBuffer); }; 47 | const int height() const { return (int)CVPixelBufferGetHeight(m_pixelBuffer); }; 48 | const void* baseAddress() const { return CVPixelBufferGetBaseAddress(m_pixelBuffer); }; 49 | 50 | const PixelBufferFormatType pixelFormat() const { return m_pixelFormat; }; 51 | 52 | void lock(bool readOnly = false); 53 | void unlock(bool readOnly = false); 54 | 55 | void setState(const PixelBufferState state) { m_state = state; }; 56 | const PixelBufferState state() const { return m_state; }; 57 | 58 | const bool isTemporary() const { return m_temporary; }; 59 | void setTemporary(const bool temporary) { m_temporary = temporary; }; 60 | 61 | public: 62 | const CVPixelBufferRef cvBuffer() const { return m_pixelBuffer; }; 63 | 64 | private: 65 | 66 | CVPixelBufferRef m_pixelBuffer; 67 | PixelBufferFormatType m_pixelFormat; 68 | PixelBufferState m_state; 69 | 70 | bool m_locked; 71 | bool m_temporary; 72 | }; 73 | 74 | typedef std::shared_ptr PixelBufferRef; 75 | 76 | } 77 | } 78 | 79 | #endif 80 | -------------------------------------------------------------------------------- /system/pixelBuffer/GenericPixelBuffer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace videocore { 4 | 5 | GenericPixelBuffer::GenericPixelBuffer(int width, int height, PixelBufferFormatType pixelFormat) 6 | { 7 | size_t bufferSize = 0; 8 | switch(pixelFormat) { 9 | case kVCPixelBufferFormat32BGRA: 10 | case kVCPixelBufferFormat32RGBA: 11 | bufferSize = width*height*4; 12 | break; 13 | case kVCPixelBufferFormatL565: 14 | bufferSize = width*height*2; 15 | break; 16 | case kCVPixelBufferFormat420v: 17 | bufferSize = width*height*1.5; 18 | break; 19 | 20 | } 21 | 22 | m_buffer = std::make_shared>(bufferSize); 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /system/pixelBuffer/GenericPixelBuffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #ifndef videocore_GenericPixelBuffer_h 26 | #define videocore_GenericPixelBuffer_h 27 | 28 | #include 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | namespace videocore { 35 | 36 | class GenericPixelBuffer : public IPixelBuffer { 37 | 38 | public: 39 | GenericPixelBuffer(int width, int height, PixelBufferFormatType pixelFormat); 40 | ~GenericPixelBuffer() {}; 41 | 42 | public: 43 | const int width() const { return m_width; }; 44 | const int height() const { return m_height; }; 45 | const void* baseAddress() const { return &(*m_buffer)[0]; }; 46 | 47 | const PixelBufferFormatType pixelFormat() const { return m_pixelFormat; }; 48 | 49 | void lock(bool readOnly = false) {}; 50 | void unlock(bool readOnly = false) {}; 51 | 52 | private: 53 | 54 | std::shared_ptr> m_buffer; 55 | 56 | int m_width; 57 | int m_height; 58 | 59 | PixelBufferFormatType m_pixelFormat; 60 | 61 | }; 62 | } 63 | #endif 64 | -------------------------------------------------------------------------------- /system/pixelBuffer/IPixelBuffer.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #ifndef videocore_IPixelBuffer_hpp 26 | #define videocore_IPixelBuffer_hpp 27 | 28 | #include 29 | 30 | namespace videocore { 31 | 32 | enum { 33 | kVCPixelBufferFormat32BGRA = 'bgra', 34 | kVCPixelBufferFormat32RGBA = 'rgba', 35 | kVCPixelBufferFormatL565 = 'L565', 36 | kCVPixelBufferFormat420v = '420v', 37 | } PixelBufferFormatType_; 38 | 39 | typedef uint32_t PixelBufferFormatType; 40 | 41 | typedef enum { 42 | kVCPixelBufferStateAvailable, 43 | kVCPixelBufferStateDequeued, 44 | kVCPixelBufferStateEnqueued, 45 | kVCPixelBufferStateAcquired 46 | } PixelBufferState; 47 | 48 | class IPixelBuffer { 49 | public: 50 | virtual ~IPixelBuffer() {}; 51 | 52 | virtual const int width() const = 0; 53 | virtual const int height() const = 0; 54 | 55 | virtual const PixelBufferFormatType pixelFormat() const = 0; 56 | 57 | virtual const void* baseAddress() const = 0; 58 | 59 | virtual void lock(bool readOnly = false) = 0; 60 | virtual void unlock(bool readOnly = false) = 0; 61 | 62 | virtual void setState(const PixelBufferState state) = 0; 63 | virtual const PixelBufferState state() const = 0; 64 | 65 | virtual const bool isTemporary() const = 0; /* mark if the pixel buffer needs to disappear soon */ 66 | virtual void setTemporary(const bool temporary) = 0; 67 | }; 68 | } 69 | 70 | #endif -------------------------------------------------------------------------------- /system/util.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | 26 | #ifndef __videocore_util_h 27 | #define __videocore_util_h 28 | 29 | #ifndef NDEBUG 30 | #define DLog(...) printf(__VA_ARGS__); 31 | #else 32 | #define DLog(...) {} 33 | #endif 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /transforms/Apple/AppleH264Encode.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (C) 2014 James G. Hurley 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public 17 | License along with this library; if not, write to the Free Software 18 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 19 | USA 20 | 21 | */ 22 | #include 23 | #include 24 | #include 25 | 26 | #include 27 | 28 | namespace videocore { namespace Apple { 29 | 30 | class H264Encode : public IEncoder 31 | { 32 | public: 33 | H264Encode( int frame_w, int frame_h, int fps, int bitrate, bool useBaseline = true, int ctsOffset = 0 ); 34 | ~H264Encode(); 35 | 36 | CVPixelBufferPoolRef pixelBufferPool(); 37 | 38 | public: 39 | /*! ITransform */ 40 | void setOutput(std::shared_ptr output) { m_output = output; }; 41 | 42 | // Input is expecting a CVPixelBufferRef 43 | void pushBuffer(const uint8_t* const data, size_t size, IMetadata& metadata); 44 | 45 | public: 46 | /*! IEncoder */ 47 | void setBitrate(int bitrate) ; 48 | 49 | const int bitrate() const { return m_bitrate; }; 50 | 51 | void requestKeyframe(); 52 | 53 | public: 54 | void compressionSessionOutput(const uint8_t* data, size_t size, uint64_t pts, uint64_t dts); 55 | 56 | private: 57 | void setupCompressionSession( bool useBaseline ); 58 | void teardownCompressionSession(); 59 | 60 | private: 61 | 62 | 63 | 64 | std::mutex m_encodeMutex; 65 | std::weak_ptr m_output; 66 | void* m_compressionSession; 67 | int m_frameW; 68 | int m_frameH; 69 | int m_fps; 70 | int m_bitrate; 71 | 72 | int m_ctsOffset; 73 | 74 | bool m_baseline; 75 | bool m_forceKeyframe; 76 | }; 77 | } 78 | } -------------------------------------------------------------------------------- /transforms/Apple/MP4Multiplexer.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | 26 | #ifndef __videocore__MP4Multiplexer__ 27 | #define __videocore__MP4Multiplexer__ 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | namespace videocore { namespace Apple { 34 | 35 | enum { 36 | kMP4SessionFilename=0, 37 | kMP4SessionFPS, 38 | kMP4SessionWidth, 39 | kMP4SessionHeight 40 | }; 41 | typedef MetaData<'mp4 ', std::string, int, int, int> MP4SessionParameters_t; 42 | 43 | class MP4Multiplexer : public IOutputSession 44 | { 45 | 46 | public: 47 | MP4Multiplexer(); 48 | ~MP4Multiplexer(); 49 | 50 | void setSessionParameters(IMetadata & parameters) ; 51 | void pushBuffer(const uint8_t* const data, size_t size, IMetadata& metadata); 52 | void setEpoch(const std::chrono::steady_clock::time_point epoch) { m_epoch = epoch; }; 53 | 54 | private: 55 | void pushVideoBuffer(const uint8_t* const data, size_t size, IMetadata& metadata); 56 | void pushAudioBuffer(const uint8_t* const data, size_t size, IMetadata& metadata); 57 | void createAVCC(); 58 | 59 | private: 60 | void* m_assetWriter; 61 | void* m_videoInput; 62 | void* m_audioInput; 63 | 64 | void* m_videoFormat; 65 | void* m_audioFormat; 66 | 67 | std::vector m_sps; 68 | std::vector m_pps; 69 | 70 | std::chrono::steady_clock::time_point m_epoch; 71 | 72 | std::string m_filename; 73 | int m_fps; 74 | int m_width; 75 | int m_height; 76 | int m_framecount; 77 | }; 78 | 79 | } 80 | } 81 | #endif /* defined(__videocore__MP4Multiplexer__) */ 82 | -------------------------------------------------------------------------------- /transforms/AspectTransform.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | 32 | namespace videocore { 33 | 34 | AspectTransform::AspectTransform(int boundingWidth, 35 | int boundingHeight, 36 | AspectMode aspectMode) 37 | : m_boundingWidth(boundingWidth), 38 | m_boundingHeight(boundingHeight), 39 | m_aspectMode(aspectMode), 40 | m_boundingBoxDirty(true), 41 | m_prevWidth(0), 42 | m_prevHeight(0) 43 | { 44 | } 45 | 46 | AspectTransform::~AspectTransform() 47 | { 48 | } 49 | 50 | void 51 | AspectTransform::setOutput(std::shared_ptr output) 52 | { 53 | m_output = output; 54 | } 55 | 56 | void 57 | AspectTransform::setBoundingSize(int boundingWidth, 58 | int boundingHeight) 59 | { 60 | m_boundingWidth = boundingWidth; 61 | m_boundingHeight = boundingHeight; 62 | m_boundingBoxDirty = true; 63 | } 64 | 65 | void 66 | AspectTransform::setAspectMode(videocore::AspectTransform::AspectMode aspectMode) 67 | { 68 | m_boundingBoxDirty = true; 69 | m_aspectMode = aspectMode; 70 | } 71 | 72 | void 73 | AspectTransform::setBoundingBoxDirty() 74 | { 75 | m_boundingBoxDirty = true; 76 | } 77 | 78 | void 79 | AspectTransform::pushBuffer(const uint8_t *const data, 80 | size_t size, 81 | videocore::IMetadata &metadata) 82 | { 83 | auto output = m_output.lock(); 84 | 85 | if(output) { 86 | 87 | std::shared_ptr pb = *(std::shared_ptr*)data; 88 | 89 | pb->lock(true); 90 | 91 | float width = float(pb->width()); 92 | float height = float(pb->height()); 93 | 94 | if(width != m_prevWidth || height != m_prevHeight) { 95 | setBoundingBoxDirty(); 96 | m_prevHeight = height; 97 | m_prevWidth = width; 98 | } 99 | 100 | if(m_boundingBoxDirty) { 101 | 102 | float wfac = float(m_boundingWidth) / width; 103 | float hfac = float(m_boundingHeight) / height; 104 | 105 | const float mult = (m_aspectMode == kAspectFit ? (wfac < hfac) : (wfac > hfac)) ? wfac : hfac; 106 | 107 | wfac = width*mult / float(m_boundingWidth); 108 | hfac = height*mult / float(m_boundingHeight); 109 | 110 | m_scale = glm::vec3(wfac,hfac,1.f); 111 | 112 | m_boundingBoxDirty = false; 113 | } 114 | 115 | pb->unlock(true); 116 | 117 | videocore::VideoBufferMetadata& md = dynamic_cast(metadata); 118 | glm::mat4 & mat = md.getData(); 119 | 120 | mat = glm::scale(mat, m_scale); 121 | 122 | output->pushBuffer(data, size, metadata); 123 | } 124 | } 125 | 126 | } -------------------------------------------------------------------------------- /transforms/AspectTransform.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #ifndef __videocore__AspectTransform__ 26 | #define __videocore__AspectTransform__ 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | namespace videocore { 33 | 34 | /*! 35 | * 36 | */ 37 | class AspectTransform : public ITransform 38 | { 39 | public: 40 | enum AspectMode 41 | { 42 | kAspectFit, /*!< An aspect mode which shrinks the incoming video to fit in the supplied boundaries. */ 43 | kAspectFill /*!< An aspect mode which scales the video to fill the supplied boundaries and maintain aspect ratio. */ 44 | }; 45 | 46 | /*! Constructor. 47 | * 48 | * \param boundingWidth The width ouf the bounding box. 49 | * \param boundingHeight The height of the bounding box. 50 | * \param aspectMode The aspectMode to use. 51 | */ 52 | AspectTransform(int boundingWidth, 53 | int boundingHeight, 54 | AspectMode aspectMode); 55 | 56 | /*! Destructor */ 57 | ~AspectTransform(); 58 | 59 | /*! 60 | * Change the size of the target bounding box. 61 | * 62 | * \param boundingWidth The width ouf the bounding box. 63 | * \param boundingHeight The height of the bounding box. 64 | */ 65 | void setBoundingSize(int boundingWidth, 66 | int boundingHeight); 67 | 68 | /*! 69 | * Change the aspect mode 70 | * 71 | * \param aspectMode The aspectMode to use. 72 | */ 73 | void setAspectMode(AspectMode aspectMode); 74 | 75 | /*! 76 | * Mark the bounding box as dirty and force a refresh. This may be useful if the 77 | * dimensions of the pixel buffer change. 78 | */ 79 | void setBoundingBoxDirty() ; 80 | 81 | public: 82 | 83 | /*! ITransform::setOutput */ 84 | void setOutput(std::shared_ptr output); 85 | 86 | /*! IOutput::pushBuffer */ 87 | void pushBuffer(const uint8_t* const data, 88 | size_t size, 89 | IMetadata& metadata); 90 | 91 | 92 | private: 93 | 94 | glm::vec3 m_scale; 95 | 96 | std::weak_ptr m_output; 97 | 98 | int m_boundingWidth; 99 | int m_boundingHeight; 100 | int m_prevWidth; 101 | int m_prevHeight; 102 | AspectMode m_aspectMode; 103 | 104 | bool m_boundingBoxDirty; 105 | 106 | }; 107 | } 108 | 109 | #endif 110 | -------------------------------------------------------------------------------- /transforms/IEncoder.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | 26 | #ifndef __videocore_IEncoder_h 27 | #define __videocore_IEncoder_h 28 | 29 | #include 30 | 31 | namespace videocore { 32 | 33 | class IEncoder : public ITransform 34 | { 35 | public: 36 | virtual ~IEncoder() {}; 37 | 38 | virtual void setBitrate(int bitrate) = 0; 39 | virtual const int bitrate() const = 0; 40 | 41 | }; 42 | 43 | } 44 | #endif -------------------------------------------------------------------------------- /transforms/IMetaData.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (C) 2014 James G. Hurley 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public 17 | License along with this library; if not, write to the Free Software 18 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 19 | USA 20 | 21 | */ 22 | 23 | #ifndef videocore_IMetadata_hpp 24 | #define videocore_IMetadata_hpp 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | 32 | namespace videocore 33 | { 34 | struct IMetadata 35 | { 36 | IMetadata(double pts, double dts) : pts(pts), dts(dts) {}; 37 | IMetadata(double ts) : pts(ts), dts(ts) {}; 38 | IMetadata() : pts(0.), dts(0.) {}; 39 | 40 | virtual ~IMetadata() {}; 41 | 42 | virtual const int32_t type() const = 0; 43 | union { 44 | double pts; 45 | double timestampDelta;// __attribute__((deprecated)); 46 | }; 47 | double dts; 48 | }; 49 | 50 | template 51 | struct MetaData : public IMetadata 52 | { 53 | MetaData(double pts, double dts) : IMetadata(pts, dts) {}; 54 | MetaData(double ts) : IMetadata(ts) {}; 55 | MetaData() : IMetadata() {}; 56 | 57 | virtual const int32_t type() const { return MetaDataType; }; 58 | 59 | void setData(Types... data) 60 | { 61 | m_data = std::make_tuple(data...); 62 | }; 63 | template 64 | void setValue(typename std::tuple_element >::type value) 65 | { 66 | auto & v = getData(); 67 | v = value; 68 | } 69 | template 70 | typename std::tuple_element >::type & getData() const { 71 | return const_cast >::type &>(std::get(m_data)); 72 | } 73 | size_t size() const 74 | { 75 | return std::tuple_size >::value; 76 | } 77 | private: 78 | std::tuple m_data; 79 | }; 80 | 81 | } 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /transforms/IOutput.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #ifndef videocore_IOutput_hpp 26 | #define videocore_IOutput_hpp 27 | #include 28 | #include 29 | #include 30 | 31 | namespace videocore 32 | { 33 | 34 | class IOutput 35 | { 36 | public: 37 | virtual void setEpoch(const std::chrono::steady_clock::time_point epoch) {}; 38 | virtual void pushBuffer(const uint8_t* const data, size_t size, IMetadata& metadata) = 0; 39 | virtual ~IOutput() {}; 40 | }; 41 | 42 | } 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /transforms/IOutputSession.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | 26 | #ifndef videocore_IOutputSession_hpp 27 | #define videocore_IOutputSession_hpp 28 | 29 | #include 30 | 31 | namespace videocore { 32 | 33 | /*! 34 | * Called when the graph should make an adjustment to outgoing data rates 35 | * 36 | * \param rateVector A unit vector representing the direction for change. (-1 for reduce bitrate, 0 for no change, 1 for increase bitrate) 37 | * \param estimatedAvailableBandwidth An estimate of the available bandwidth [Bytes per second]. This may not be accurate. 38 | * 39 | */ 40 | using BandwidthCallback = std::function; 41 | 42 | class IOutputSession : public IOutput 43 | { 44 | public: 45 | 46 | virtual void setSessionParameters(IMetadata & parameters) = 0 ; 47 | virtual void setBandwidthCallback(BandwidthCallback callback) = 0; 48 | 49 | virtual ~IOutputSession() {}; 50 | 51 | }; 52 | } 53 | 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /transforms/ITransform.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | 26 | #ifndef videocore_ITransform_hpp 27 | #define videocore_ITransform_hpp 28 | 29 | #include 30 | #include 31 | 32 | namespace videocore { 33 | 34 | typedef MetaData<'tran'> TransformMetadata_t; 35 | 36 | class ITransform : public IOutput 37 | { 38 | public: 39 | virtual void setOutput(std::shared_ptr output) = 0; 40 | virtual ~ITransform() {}; 41 | }; 42 | } 43 | 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /transforms/PositionTransform.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | 26 | 27 | #include 28 | #include 29 | #include 30 | namespace videocore { 31 | 32 | PositionTransform::PositionTransform(int x, 33 | int y, 34 | int width, 35 | int height, 36 | int contextWidth, 37 | int contextHeight) 38 | : m_posX(x), 39 | m_posY(y), 40 | m_width(width), 41 | m_height(height), 42 | m_contextWidth(contextWidth), 43 | m_contextHeight(contextHeight), 44 | m_positionIsDirty(true) 45 | { 46 | 47 | } 48 | 49 | PositionTransform::~PositionTransform() 50 | { 51 | 52 | } 53 | 54 | void 55 | PositionTransform::setOutput(std::shared_ptr output) 56 | { 57 | m_output = output; 58 | } 59 | 60 | void 61 | PositionTransform::setPosition(int x, 62 | int y) 63 | { 64 | m_posX = x; 65 | m_posY = y; 66 | m_positionIsDirty = true; 67 | } 68 | void 69 | PositionTransform::setSize(int width, 70 | int height) 71 | { 72 | m_width = width; 73 | m_height = height; 74 | m_positionIsDirty = true; 75 | } 76 | 77 | void 78 | PositionTransform::pushBuffer(const uint8_t *const data, 79 | size_t size, 80 | videocore::IMetadata &metadata) 81 | { 82 | auto output = m_output.lock(); 83 | 84 | if(output) { 85 | 86 | if(m_positionIsDirty) { 87 | glm::mat4 mat(1.f); 88 | const float x (m_posX), y(m_posY), cw(m_contextWidth), ch(m_contextHeight), w(m_width), h(m_height); 89 | 90 | mat = glm::translate(mat, 91 | glm::vec3((x / cw) * 2.f - 1.f, // The compositor uses homogeneous coordinates. 92 | (y / ch) * 2.f - 1.f, // i.e. [ -1 .. 1 ] 93 | 0.f)); 94 | 95 | mat = glm::scale(mat, 96 | glm::vec3(w / cw, // 97 | h / ch, // size is a percentage for scaling. 98 | 1.f)); 99 | 100 | m_matrix = mat; 101 | 102 | m_positionIsDirty = false; 103 | } 104 | videocore::VideoBufferMetadata& md = dynamic_cast(metadata); 105 | glm::mat4 & mat = md.getData(); 106 | 107 | mat = mat * m_matrix; 108 | 109 | output->pushBuffer(data, size, metadata); 110 | } 111 | } 112 | 113 | } -------------------------------------------------------------------------------- /transforms/PositionTransform.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | 26 | #ifndef __videocore__PositionTransform__ 27 | #define __videocore__PositionTransform__ 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | namespace videocore { 34 | 35 | class PositionTransform : public ITransform 36 | { 37 | public: 38 | 39 | /*! Constructor. 40 | * 41 | * \param x The x position of the image in the video context. 42 | * \param y The y position of the image in the video context. 43 | * \param width The width of the image. 44 | * \param height The height of the image. 45 | * \param contextWidth The width of the video context. 46 | * \param contextHeight The height of the video context. 47 | */ 48 | PositionTransform(int x, 49 | int y, 50 | int width, 51 | int height, 52 | int contextWidth, 53 | int contextHeight); 54 | 55 | /*! Destructor */ 56 | ~PositionTransform(); 57 | 58 | /*! 59 | * Change the position of the image in the video context. 60 | * 61 | * \param x The x position of the image in the video context. 62 | * \param y The y position of the image in the video context. 63 | */ 64 | void setPosition(int x, 65 | int y); 66 | 67 | /*! 68 | * Change the size of the image in the video context. 69 | * 70 | * \param width The width of the image. 71 | * \param height The height of the image. 72 | */ 73 | void setSize(int width, 74 | int height); 75 | 76 | public: 77 | 78 | /*! ITransform::setOutput */ 79 | void setOutput(std::shared_ptr output); 80 | 81 | /*! IOutput::pushBuffer */ 82 | void pushBuffer(const uint8_t* const data, 83 | size_t size, 84 | IMetadata& metadata); 85 | 86 | private: 87 | 88 | glm::mat4 m_matrix; 89 | 90 | std::weak_ptr m_output; 91 | 92 | int m_posX; 93 | int m_posY; 94 | int m_width; 95 | int m_height; 96 | int m_contextWidth; 97 | int m_contextHeight; 98 | 99 | bool m_positionIsDirty; 100 | }; 101 | } 102 | 103 | #endif 104 | -------------------------------------------------------------------------------- /transforms/RTMP/AACPacketizer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | namespace videocore { namespace rtmp { 31 | 32 | AACPacketizer::AACPacketizer(float sampleRate, int channelCount, int ctsOffset) 33 | : m_audioTs(0), m_sentAudioConfig(false), m_sampleRate(sampleRate), m_channelCount(channelCount), m_ctsOffset(ctsOffset) 34 | { 35 | memset(m_asc, 0, sizeof(m_asc)); 36 | } 37 | 38 | void 39 | AACPacketizer::pushBuffer(const uint8_t* const inBuffer, size_t inSize, IMetadata& metadata) 40 | { 41 | std::vector & outBuffer = m_outbuffer; 42 | 43 | outBuffer.clear(); 44 | 45 | int flvStereoOrMono = (m_channelCount == 2 ? FLV_STEREO : FLV_MONO); 46 | int flvSampleRate = FLV_SAMPLERATE_44100HZ; // default 47 | if (m_sampleRate == 22050.0) { 48 | flvSampleRate = FLV_SAMPLERATE_22050HZ; 49 | } 50 | 51 | int flags = 0; 52 | const int flags_size = 2; 53 | 54 | 55 | int ts = metadata.timestampDelta + m_ctsOffset ; 56 | // DLog("AAC: %06d", ts); 57 | 58 | auto output = m_output.lock(); 59 | 60 | RTMPMetadata_t outMeta(ts); 61 | 62 | if(inSize == 2 && !m_asc[0] && !m_asc[1]) { 63 | m_asc[0] = inBuffer[0]; 64 | m_asc[1] = inBuffer[1]; 65 | } 66 | 67 | if(output) { 68 | 69 | flags = FLV_CODECID_AAC | flvSampleRate | FLV_SAMPLESSIZE_16BIT | flvStereoOrMono; 70 | 71 | outBuffer.reserve(inSize + flags_size); 72 | 73 | put_byte(outBuffer, flags); 74 | put_byte(outBuffer, m_sentAudioConfig); 75 | 76 | if(!m_sentAudioConfig) { 77 | m_sentAudioConfig = true; 78 | put_buff(outBuffer, (uint8_t*)m_asc, sizeof(m_asc)); 79 | 80 | } else { 81 | put_buff(outBuffer, inBuffer, inSize); 82 | } 83 | 84 | outMeta.setData(ts, static_cast(outBuffer.size()), RTMP_PT_AUDIO, kAudioChannelStreamId, false); 85 | 86 | output->pushBuffer(&outBuffer[0], outBuffer.size(), outMeta); 87 | } 88 | 89 | } 90 | 91 | void 92 | AACPacketizer::setOutput(std::shared_ptr output) 93 | { 94 | m_output = output; 95 | } 96 | 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /transforms/RTMP/AACPacketizer.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | 26 | #ifndef __videocore__AACPacketizer__ 27 | #define __videocore__AACPacketizer__ 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | namespace videocore { namespace rtmp { 34 | 35 | class AACPacketizer : public ITransform 36 | { 37 | public: 38 | AACPacketizer(float sampleRate=44100, int channelCount=2, int ctsOffset=0); 39 | 40 | void pushBuffer(const uint8_t* const data, size_t size, IMetadata& metadata); 41 | void setOutput(std::shared_ptr output); 42 | void setEpoch(const std::chrono::steady_clock::time_point epoch) { m_epoch = epoch; }; 43 | 44 | private: 45 | 46 | 47 | std::chrono::steady_clock::time_point m_epoch; 48 | std::weak_ptr m_output; 49 | std::vector m_outbuffer; 50 | 51 | double m_audioTs; 52 | char m_asc[2]; 53 | bool m_sentAudioConfig; 54 | float m_sampleRate; 55 | int m_ctsOffset; 56 | int m_channelCount; 57 | }; 58 | 59 | } 60 | } 61 | #endif /* defined(__videocore__AACPacketizer__) */ 62 | -------------------------------------------------------------------------------- /transforms/RTMP/H264Packetizer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | #include 26 | #include 27 | #include 28 | 29 | namespace videocore { namespace rtmp { 30 | 31 | H264Packetizer::H264Packetizer( int ctsOffset ) : m_videoTs(0), m_sentConfig(false), m_ctsOffset(ctsOffset) 32 | { 33 | 34 | } 35 | void 36 | H264Packetizer::setOutput(std::shared_ptr output) 37 | { 38 | m_output = output; 39 | } 40 | void H264Packetizer::pushBuffer(const uint8_t* const inBuffer, size_t inSize, IMetadata& inMetadata) 41 | { 42 | std::vector& outBuffer = m_outbuffer; 43 | 44 | outBuffer.clear(); 45 | 46 | uint8_t nal_type = inBuffer[4] & 0x1F; 47 | int flags = 0; 48 | const int flags_size = 5; 49 | int dts = inMetadata.dts ; 50 | int pts = inMetadata.pts + m_ctsOffset; // correct for pts < dts which some players (ffmpeg) don't like 51 | 52 | dts = dts > 0 ? dts : pts - m_ctsOffset ; 53 | 54 | bool is_config = (nal_type == 7 || nal_type == 8); 55 | 56 | flags = FLV_CODECID_H264; 57 | auto output = m_output.lock(); 58 | 59 | switch(nal_type) { 60 | case 7: 61 | if(m_sps.size() == 0) { 62 | m_sps.resize(inSize-4); 63 | memcpy(&m_sps[0], inBuffer+4, inSize-4); 64 | } 65 | break; 66 | case 8: 67 | if(m_pps.size() == 0) { 68 | m_pps.resize(inSize-4); 69 | memcpy(&m_pps[0], inBuffer+4, inSize-4); 70 | } 71 | flags |= FLV_FRAME_KEY; 72 | break; 73 | case 5: 74 | flags |= FLV_FRAME_KEY; 75 | 76 | break; 77 | default: 78 | flags |= FLV_FRAME_INTER; 79 | 80 | 81 | break; 82 | 83 | } 84 | 85 | 86 | if(output) { 87 | 88 | RTMPMetadata_t outMeta(dts); 89 | std::vector conf; 90 | 91 | if(is_config && m_sps.size() > 0 && m_pps.size() > 0 ) { 92 | conf = configurationFromSpsAndPps(); 93 | inSize = conf.size(); 94 | } 95 | outBuffer.reserve(inSize + flags_size); 96 | 97 | put_byte(outBuffer, flags); 98 | put_byte(outBuffer, !is_config); 99 | put_be24(outBuffer, pts - dts); // Decoder delay 100 | 101 | if(is_config) { 102 | // create modified SPS/PPS buffer 103 | if(m_sps.size() > 0 && m_pps.size() > 0 && !m_sentConfig) { 104 | put_buff(outBuffer, &conf[0], conf.size()); 105 | m_sentConfig = true; 106 | } else { 107 | return; 108 | } 109 | } else { 110 | put_buff(outBuffer, inBuffer, inSize); 111 | } 112 | 113 | outMeta.setData(dts, static_cast(outBuffer.size()), RTMP_PT_VIDEO, kVideoChannelStreamId, nal_type == 5); 114 | 115 | output->pushBuffer(&outBuffer[0], outBuffer.size(), outMeta); 116 | } 117 | 118 | } 119 | std::vector 120 | H264Packetizer::configurationFromSpsAndPps() 121 | { 122 | std::vector conf; 123 | 124 | put_byte(conf, 1); // version 125 | put_byte(conf, m_sps[1]); // profile 126 | put_byte(conf, m_sps[2]); // compat 127 | put_byte(conf, m_sps[3]); // level 128 | put_byte(conf, 0xff); // 6 bits reserved + 2 bits nal size length - 1 (11) 129 | put_byte(conf, 0xe1); // 3 bits reserved + 5 bits number of sps (00001) 130 | put_be16(conf, m_sps.size()); 131 | put_buff(conf, &m_sps[0], m_sps.size()); 132 | put_byte(conf, 1); 133 | put_be16(conf, m_pps.size()); 134 | put_buff(conf, &m_pps[0], m_pps.size()); 135 | 136 | return conf; 137 | 138 | } 139 | 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /transforms/RTMP/H264Packetizer.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | 26 | #ifndef videocore_H264Packetizer_h 27 | #define videocore_H264Packetizer_h 28 | 29 | #include 30 | 31 | #include 32 | 33 | namespace videocore { namespace rtmp { 34 | 35 | class H264Packetizer : public ITransform 36 | { 37 | public: 38 | 39 | H264Packetizer(int ctsOffset=0); 40 | 41 | public: 42 | void pushBuffer(const uint8_t* const data, size_t size, IMetadata& metadata); 43 | void setOutput(std::shared_ptr output); 44 | void setEpoch(const std::chrono::steady_clock::time_point epoch) { m_epoch = epoch; }; 45 | 46 | private: 47 | 48 | std::chrono::steady_clock::time_point m_epoch; 49 | std::weak_ptr m_output; 50 | std::vector m_sps; 51 | std::vector m_pps; 52 | std::vector m_outbuffer; 53 | 54 | double m_videoTs; 55 | 56 | std::vector configurationFromSpsAndPps(); 57 | 58 | int m_ctsOffset; 59 | 60 | bool m_sentConfig; 61 | }; 62 | 63 | } // rtmp 64 | } // videocore 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /transforms/Split.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | /* 26 | * Split transform :: push a buffer to multiple outputs. 27 | * 28 | * 29 | */ 30 | #include 31 | 32 | namespace videocore { 33 | Split::Split() {} 34 | Split::~Split() {} 35 | 36 | void 37 | Split::setOutput(std::shared_ptr output) 38 | { 39 | const auto inHash = std::hash>()(output); 40 | bool duplicate = false; 41 | for ( auto & it : m_outputs) { 42 | auto outp = it.lock(); 43 | if(outp) { 44 | auto hash = std::hash>()(outp); 45 | duplicate |= (hash == inHash); 46 | } 47 | } 48 | if(!duplicate) { 49 | m_outputs.push_back(output); 50 | } 51 | } 52 | void 53 | Split::removeOutput(std::shared_ptr output) 54 | { 55 | const auto inHash = std::hash>()(output); 56 | 57 | auto it = std::remove_if(m_outputs.begin(), m_outputs.end(), [=](const std::weak_ptr& rhs) { 58 | 59 | auto outp = rhs.lock(); 60 | if(outp) { 61 | return inHash == std::hash>()(outp); 62 | } 63 | return true; 64 | }); 65 | m_outputs.resize(std::distance(m_outputs.begin(), it)); 66 | } 67 | void 68 | Split::pushBuffer(const uint8_t* const data, size_t size, IMetadata& metadata) 69 | { 70 | for ( auto & it : m_outputs ) { 71 | auto outp = it.lock(); 72 | if(outp) { 73 | outp->pushBuffer(data, size, metadata); 74 | } 75 | } 76 | } 77 | } -------------------------------------------------------------------------------- /transforms/Split.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | /* 26 | * Split transform :: push a buffer to multiple outputs. 27 | * 28 | * 29 | */ 30 | 31 | #ifndef __videocore__Split__ 32 | #define __videocore__Split__ 33 | 34 | #include 35 | #include 36 | #include 37 | 38 | namespace videocore { 39 | 40 | class Split : public ITransform 41 | { 42 | public: 43 | Split(); 44 | ~Split(); 45 | 46 | void setOutput(std::shared_ptr output); 47 | void removeOutput(std::shared_ptr output); 48 | void pushBuffer(const uint8_t* const data, size_t size, IMetadata& metadata); 49 | 50 | private: 51 | 52 | std::vector> m_outputs; 53 | 54 | }; 55 | 56 | } 57 | #endif /* defined(__videocore__Split__) */ 58 | -------------------------------------------------------------------------------- /transforms/iOS/AACEncode.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | 26 | /* 27 | * iOS::AACEncode transform :: Takes a raw buffer of LPCM input and outputs AAC Packets. 28 | * 29 | * 30 | */ 31 | 32 | 33 | #ifndef __videocore__AACEncode__ 34 | #define __videocore__AACEncode__ 35 | 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | namespace videocore { namespace iOS { 42 | 43 | class AACEncode : public IEncoder 44 | { 45 | public: 46 | 47 | AACEncode(int frequencyInHz, int channelCount, int averageBitrate); 48 | 49 | ~AACEncode(); 50 | 51 | void setOutput(std::shared_ptr output) { m_output = output; }; 52 | void pushBuffer(const uint8_t* const data, size_t size, IMetadata& metadata); 53 | 54 | void setBitrate(int bitrate); 55 | const int bitrate() const { return m_bitrate; }; 56 | 57 | private: 58 | static OSStatus ioProc(AudioConverterRef audioConverter, UInt32 *ioNumDataPackets, AudioBufferList* ioData, AudioStreamPacketDescription** ioPacketDesc, void* inUserData ); 59 | void makeAsc(uint8_t sampleRateIndex, uint8_t channelCount); 60 | private: 61 | 62 | AudioStreamBasicDescription m_in, m_out; 63 | 64 | std::mutex m_converterMutex; 65 | AudioConverterRef m_audioConverter; 66 | std::weak_ptr m_output; 67 | size_t m_bytesPerSample; 68 | uint32_t m_outputPacketMaxSize; 69 | 70 | Buffer m_outputBuffer; 71 | 72 | int m_bitrate; 73 | uint8_t m_asc[2]; 74 | bool m_sentConfig; 75 | 76 | }; 77 | 78 | } 79 | } 80 | #endif /* defined(__videocore__AACEncode__) */ 81 | -------------------------------------------------------------------------------- /transforms/iOS/iOSH264Encode.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video Core 4 | Copyright (c) 2014 James G. Hurley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | */ 25 | 26 | /* 27 | * iOS::H264Encode transform :: Takes a CVPixelBufferRef input and outputs H264 NAL Units. 28 | * 29 | * 30 | */ 31 | 32 | #ifndef __videocore__H264Encode__ 33 | #define __videocore__H264Encode__ 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | namespace videocore { namespace iOS { 42 | 43 | class H264Encode : public IEncoder 44 | { 45 | public: 46 | H264Encode( int frame_w, int frame_h, int fps, int bitrate ); 47 | ~H264Encode(); 48 | 49 | void setOutput(std::shared_ptr output) { m_output = output; }; 50 | 51 | // Input is expecting a CVPixelBufferRef 52 | void pushBuffer(const uint8_t* const data, size_t size, IMetadata& metadata); 53 | 54 | public: 55 | /*! IEncoder */ 56 | void setBitrate(int bitrate) ; 57 | 58 | const int bitrate() const { return m_bitrate; }; 59 | 60 | private: 61 | bool setupWriters(); 62 | bool setupWriter(int writer); 63 | void swapWriters(bool force = false); 64 | void teardownWriter(int writer); 65 | void extractSpsAndPps(int writer); 66 | void releaseFrame(videocore::IMetadata& metadata); 67 | bool isFirstSlice(uint8_t* nalu, size_t size); 68 | 69 | 70 | 71 | private: 72 | void* m_assetWriters[2]; 73 | void* m_pixelBuffers[2]; 74 | 75 | Buffer m_sps; 76 | Buffer m_pps; 77 | 78 | std::deque> m_frameQueue; 79 | 80 | JobQueue m_queue; 81 | 82 | std::string m_tmpFile[2]; 83 | 84 | std::weak_ptr m_output; 85 | 86 | long m_lastFilePos; 87 | 88 | uint32_t m_frameCount; 89 | 90 | int m_frameW; 91 | int m_frameH; 92 | int m_fps; 93 | int m_bitrate; 94 | 95 | bool m_currentWriter; 96 | }; 97 | } 98 | } 99 | #endif /* defined(__videocore__H264Encode__) */ 100 | --------------------------------------------------------------------------------