123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- #import "IosTexturePlugin.h"
- #import <CoreVideo/CoreVideo.h>
- #import "OralCavityManager.h"
- #define WeakSelf __weak typeof(self) weakSelf = self;
- //FlutterTexture
- @interface MyTexture : NSObject <FlutterTexture>
- @property(nonatomic, assign) CVPixelBufferRef pixelBuffer;
- @end
- @implementation MyTexture
- // Flutter 会调用此方法来获取 CVPixelBuffer
- - (CVPixelBufferRef)copyPixelBuffer {
- //3.
- if (_pixelBuffer) {
- // CVPixelBufferRetain(_pixelBuffer);
- return _pixelBuffer;
- }
- return NULL;
-
-
- }
- // 你需要在不再需要 pixelBuffer 时释放它
- - (void)dealloc {
- if (_pixelBuffer) {
- CVPixelBufferRelease(_pixelBuffer);
- }
- }
- @end
- //FlutterPlatformView
- @interface MyCustomView : NSObject<FlutterPlatformView>
- - (instancetype)initWithFrame:(CGRect)frame view:(CALayer *)view;
- @end
- @implementation MyCustomView {
- UIView *_view;
- }
- - (instancetype)initWithFrame:(CGRect)frame view:(CALayer *)view {
- self = [super init];
- if (self) {
- _view = [[UIView alloc] initWithFrame:frame];
- [_view.layer addSublayer:view];
-
- }
- return self;
- }
- - (UIView*)view {
- return _view;
- }
- @end
- //FlutterPlatformViewFactory
- @interface MyCustomViewFactory : NSObject<FlutterPlatformViewFactory>
- @property(nonatomic, strong) CALayer * videoView;
- @end
- @implementation MyCustomViewFactory
- - (nonnull NSObject<FlutterPlatformView> *)createWithFrame:(CGRect)frame
- viewIdentifier:(int64_t)viewId
- arguments:(id _Nullable)args {
- return [[MyCustomView alloc] initWithFrame:frame view:_videoView];
- }
- @end
- @interface IosTexturePlugin ()
- @property(nonatomic, strong) MyTexture *myTexture;
- @property(nonatomic, strong) NSObject<FlutterTextureRegistry> *textureRegistry;
- @property(nonatomic, assign) int64_t textureId;
- @property (nonatomic, strong) FlutterMethodChannel *videoControlChannel;
- @end
- @implementation IosTexturePlugin
- + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
-
-
-
-
- //视频控制
- FlutterMethodChannel* videoControlChannel = [FlutterMethodChannel
- methodChannelWithName:@"videoControl"
- binaryMessenger:[registrar messenger]];
-
- //文件控制
- FlutterMethodChannel* fileControlChannel = [FlutterMethodChannel
- methodChannelWithName:@"fileControl"
- binaryMessenger:[registrar messenger]];
-
- IosTexturePlugin* instance = [[IosTexturePlugin alloc] initWithRegistrar:registrar Channel:videoControlChannel];
-
- [registrar addMethodCallDelegate:instance channel:videoControlChannel];
- [registrar addMethodCallDelegate:instance channel:fileControlChannel];
- //视频流
- [OralCavityManager sharedInstance].getVideoView = ^(CALayer *view) {
- MyCustomViewFactory* factory = [[MyCustomViewFactory alloc] init];
- factory.videoView = view;
- [registrar registerViewFactory:factory withId:@"VideoView"];
- };
- //设备拍照
- [OralCavityManager sharedInstance].onDeviceTakePhoto = ^{
- //sendToflutter
- NSLog(@"===onDeviceTakePhoto");
- [videoControlChannel invokeMethod:@"onDeviceTakePhoto" arguments:nil];
- };
-
-
- }
- - (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar Channel:(FlutterMethodChannel *)videoControlChannel {
-
- self = [super init];
- if (self) {
- _textureRegistry = [registrar textures];
- _myTexture = [[MyTexture alloc] init];
-
- // 注册纹理并获取纹理ID
- _textureId = [_textureRegistry registerTexture:_myTexture];
- //设置回调channel
- _videoControlChannel = videoControlChannel;
- }
- return self;
- }
- - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
-
- WeakSelf;
-
- //获取设备连接状态
- if ([@"getConnectStatus" isEqualToString:call.method]) {
-
- // NSLog(@"获取状态 === %d", [[OralCavityManager sharedInstance] connectToServer]);
- result(@([[OralCavityManager sharedInstance] connectToServer]));
-
- }
- //开始视频
- else if ([@"startVideo" isEqualToString:call.method]) {
-
- NSLog(@"开始视频textureId === %lld",self.textureId);
- result(@(self.textureId));
- [[OralCavityManager sharedInstance] startVideo];
-
- // [OralCavityManager sharedInstance].getPixelBuffer = ^(CVPixelBufferRef pixelBuffer) {
- //
- // if (pixelBuffer) {
- // // 在赋值之前释放旧的 pixel buffer,避免内存泄漏
- // if (weakSelf.myTexture.pixelBuffer != NULL) {
- // CVPixelBufferRelease(weakSelf.myTexture.pixelBuffer);
- // }
- //
- // // 保留新获取的 pixel buffer,确保它不会被释放
- // CVPixelBufferRetain(pixelBuffer);
- // weakSelf.myTexture.pixelBuffer = pixelBuffer;
- //
- // // 通知 Flutter 有新帧可以显示
- // [weakSelf.textureRegistry textureFrameAvailable:weakSelf.textureId];
- // }
- //
- // };
-
- }
- //停止视频
- else if ([@"stopVideo" isEqualToString:call.method]) {
-
- NSLog(@"===停止视频");
- [[OralCavityManager sharedInstance] stopVideo];
-
- }
- //拍照
- else if ([@"takePhoto" isEqualToString:call.method]) {
- NSLog(@"===拍照 %@",call.arguments);
- [[OralCavityManager sharedInstance] takePhoto:call.arguments];
- [OralCavityManager sharedInstance].getPhotoPath = ^(NSString *path) {
- if ([path containsString:@"jpg"]) {
- result(path);
- }else {
- result([FlutterError errorWithCode:call.method message:path details:@"appId"]);
- }
- NSLog(@"拍照结果photoPath ===%@",path);
-
- };
-
- }
- //savePhoto
- else if([@"savePhoto" isEqualToString:call.method]) {
- NSLog(@"savePhoto ===arguments %@",call.arguments);
- [[OralCavityManager sharedInstance] savePhoto:call.arguments];
- [OralCavityManager sharedInstance].getSavePhotoPath = ^(NSString *path) {
- if ([path containsString:@"error"]) {
- result([FlutterError errorWithCode:call.method message:path details:@"appId"]);
- }else {
- result(path);
- }
- NSLog(@"savePhoto ===%@",path);
- };
- }
- //开始录像
- else if ([@"startRecord" isEqualToString:call.method]) {
- NSLog(@"===开始录像 %@",call.arguments);
- [[OralCavityManager sharedInstance] startRecord:call.arguments];
- result(@"startRecord");
- }
- //停止录像
- else if ([@"stopRecord" isEqualToString:call.method]) {
- [[OralCavityManager sharedInstance] stopRecord];
- [OralCavityManager sharedInstance].getVideoPath = ^(NSString *path) {
- if ([path containsString:@"Documents"]) {
- result(path);
- }else {
- result([FlutterError errorWithCode:call.method message:path details:@"appId"]);
- }
- NSLog(@"停止录像VideoPath ===%@",path);
- };
- }
- //saveRecord
- else if([@"saveRecord" isEqualToString:call.method]) {
- NSLog(@"saveRecord ===arguments %@",call.arguments);
- [[OralCavityManager sharedInstance] saveRecord:call.arguments];
- [OralCavityManager sharedInstance].getSaveRecordPath = ^(NSString *path) {
- if ([path containsString:@"error"]) {
- result([FlutterError errorWithCode:call.method message:path details:@"appId"]);
- }else {
- result(path);
- }
- NSLog(@"saveRecord ===%@",path);
- };
- }
- //获取图片列表
- else if ([@"getPhotoList" isEqualToString:call.method]) {
-
- NSArray * arr = [[OralCavityManager sharedInstance] getPhotoList];
- // if (arr.count > 0) {
- result(arr);
- // }else {
- // result([FlutterError errorWithCode:call.method message:@"获取图片列表失败" details:@"appId"]);
- // }
-
- NSLog(@"===photolist = %@",arr);
- }
- //获取视频列表
- else if ([@"getVideoList" isEqualToString:call.method]) {
-
- NSArray *arr = [[OralCavityManager sharedInstance] getVideoList];
- // if (arr.count > 0) {
- result(arr);
- // }else {
- // result([FlutterError errorWithCode:call.method message:@"获取视频列表失败" details:@"appId"]);
- // }
- NSLog(@"===videoList = %@",arr);
- }
- //删除指定文件
- else if ([@"deleteFile" isEqualToString:call.method]) {
- NSLog(@"===deleteFile = %@",call.arguments);
- int success = [[OralCavityManager sharedInstance] deleteFile:call.arguments];
- result(@(success));
- }
- //横屏
- else if ([@"getLandscapeView" isEqualToString:call.method]) {
-
- NSArray *arr = call.arguments;
- NSLog(@"===fullScreen %@",arr);
- [[OralCavityManager sharedInstance] getLandscapeView:arr];
-
- }
- //竖屏
- else if ([@"getPortraitView" isEqualToString:call.method]) {
- NSLog(@"===smallScreen");
- [[OralCavityManager sharedInstance] getPortraitView];
-
- }
- else {
- result(FlutterMethodNotImplemented);
- }
-
- }
- @end
|