CoolFlutterIJK.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //
  2. // Created by Caijinglong on 2019-03-08.
  3. //
  4. #import "CoolFlutterIJK.h"
  5. #import "CoolVideoInfo.h"
  6. #import "CoolIjkNotifyChannel.h"
  7. #import <IJKMediaFramework/IJKMediaFramework.h>
  8. #import <IJKMediaFramework/IJKMediaPlayer.h>
  9. #import <AVFoundation/AVFoundation.h>
  10. #import <libkern/OSAtomic.h>
  11. @interface CoolFlutterIJK () <FlutterTexture, KKIjkNotifyDelegate>
  12. @end
  13. @implementation CoolFlutterIJK {
  14. int64_t textureId;
  15. CADisplayLink *displayLink;
  16. NSObject <FlutterTextureRegistry> *textures;
  17. IJKFFMoviePlayerController *controller;
  18. CVPixelBufferRef latestPixelBuffer;
  19. FlutterMethodChannel *channel;
  20. CoolIjkNotifyChannel *notifyChannel;
  21. int degree;
  22. }
  23. - (instancetype)initWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
  24. self = [super init];
  25. if (self) {
  26. self.registrar = registrar;
  27. textures = [self.registrar textures];
  28. textureId = [textures registerTexture:self];
  29. NSString *channelName = [NSString stringWithFormat:@"top.kikt/ijkplayer/%lli", textureId];
  30. channel = [FlutterMethodChannel methodChannelWithName:channelName binaryMessenger:[registrar messenger]];
  31. [channel setMethodCallHandler:^(FlutterMethodCall *call, FlutterResult result) {
  32. [self handleMethodCall:call result:result];
  33. }];
  34. }
  35. return self;
  36. }
  37. - (void)dispose {
  38. [notifyChannel dispose];
  39. [[self.registrar textures] unregisterTexture:self.id];
  40. [controller stop];
  41. [controller shutdown];
  42. controller = nil;
  43. displayLink.paused = YES;
  44. [displayLink invalidate];
  45. }
  46. - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
  47. if ([@"play" isEqualToString:call.method]) {
  48. [self play];
  49. result(@(YES));
  50. } else if ([@"pause" isEqualToString:call.method]) {
  51. [self pause];
  52. result(@(YES));
  53. } else if ([@"stop" isEqualToString:call.method]) {
  54. [self stop];
  55. result(@(YES));
  56. } else if ([@"setNetworkDataSource" isEqualToString:call.method]) {
  57. @try {
  58. NSDictionary *params = call.arguments;
  59. NSString *uri = params[@"uri"];
  60. [self setDataSourceWithUri:uri];
  61. result(@(YES));
  62. }
  63. @catch (NSException *exception) {
  64. NSLog(@"Exception occurred: %@, %@", exception, [exception userInfo]);
  65. result([FlutterError errorWithCode:@"1" message:@"设置失败" details:nil]);
  66. }
  67. } else if ([@"setAssetDataSource" isEqualToString:call.method]) {
  68. @try {
  69. NSDictionary *params = [call arguments];
  70. NSString *name = params[@"name"];
  71. NSString *pkg = params[@"package"];
  72. IJKFFMoviePlayerController *playerController = [self createControllerWithAssetName:name pkg:pkg];
  73. [self setDataSourceWithController:playerController];
  74. result(@(YES));
  75. }
  76. @catch (NSException *exception) {
  77. NSLog(@"Exception occurred: %@, %@", exception, [exception userInfo]);
  78. result([FlutterError errorWithCode:@"1" message:@"设置失败" details:nil]);
  79. }
  80. } else if ([@"setFileDataSource" isEqualToString:call.method]) {
  81. NSDictionary *params = call.arguments;
  82. NSString *path = params[@"path"];
  83. IJKFFMoviePlayerController *playerController = [self createControllerWithPath:path];
  84. [self setDataSourceWithController:playerController];
  85. result(@(YES));
  86. } else if ([@"seekTo" isEqualToString:call.method]) {
  87. NSDictionary *params = call.arguments;
  88. double target = [params[@"target"] doubleValue];
  89. [self seekTo:target];
  90. result(@(YES));
  91. } else if ([@"getInfo" isEqualToString:call.method]) {
  92. CoolVideoInfo *info = [self getInfo];
  93. result([info toMap]);
  94. } else if ([@"setVolume" isEqualToString:call.method]) {
  95. NSDictionary *params = [self params:call];
  96. float v = [params[@"volume"] floatValue] / 100;
  97. controller.playbackVolume = v;
  98. result(@(YES));
  99. } else {
  100. result(FlutterMethodNotImplemented);
  101. }
  102. }
  103. - (NSDictionary *)params:(FlutterMethodCall *)call {
  104. return call.arguments;
  105. }
  106. + (instancetype)ijkWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
  107. return [[self alloc] initWithRegistrar:registrar];
  108. }
  109. - (int64_t)id {
  110. return textureId;
  111. }
  112. - (void)play {
  113. [controller play];
  114. }
  115. - (void)pause {
  116. [controller pause];
  117. }
  118. - (void)stop {
  119. [controller stop];
  120. }
  121. - (void)setDataSourceWithController:(IJKFFMoviePlayerController *)ctl {
  122. if (ctl) {
  123. controller = ctl;
  124. [self prepare];
  125. }
  126. }
  127. - (IJKFFOptions *)createOption {
  128. IJKFFOptions *options = [IJKFFOptions optionsByDefault];
  129. return options;
  130. }
  131. - (void)setDataSourceWithUri:(NSString *)uri {
  132. IJKFFOptions *options = [self createOption];
  133. controller = [[IJKFFMoviePlayerController alloc] initWithContentURLString:uri withOptions:options];
  134. [self prepare];
  135. }
  136. - (void)setDegree:(int)d {
  137. degree = d;
  138. }
  139. - (void)prepare {
  140. [controller prepareToPlay];
  141. if (displayLink) {
  142. displayLink.paused = YES;
  143. [displayLink invalidate];
  144. displayLink = nil;
  145. }
  146. displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(onDisplayLink:)];
  147. [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
  148. displayLink.paused = YES;
  149. notifyChannel = [CoolIjkNotifyChannel channelWithController:controller textureId:textureId registrar:self.registrar];
  150. notifyChannel.infoDelegate = self;
  151. }
  152. - (IJKFFMoviePlayerController *)createControllerWithAssetName:(NSString *)assetName pkg:(NSString *)pkg {
  153. NSString *asset;
  154. if (!pkg) {
  155. asset = [self.registrar lookupKeyForAsset:assetName];
  156. } else {
  157. asset = [self.registrar lookupKeyForAsset:assetName fromPackage:pkg];
  158. }
  159. NSString *path = [[NSBundle mainBundle] pathForResource:asset ofType:nil];
  160. NSURL *url = [NSURL fileURLWithPath:path];
  161. IJKFFOptions *options = [self createOption];
  162. return [[IJKFFMoviePlayerController alloc] initWithContentURL:url withOptions:options];
  163. }
  164. - (IJKFFMoviePlayerController *)createControllerWithPath:(NSString *)path {
  165. NSURL *url = [NSURL fileURLWithPath:path];
  166. IJKFFOptions *options = [self createOption];
  167. return [[IJKFFMoviePlayerController alloc] initWithContentURL:url withOptions:options];
  168. }
  169. - (void)seekTo:(double)target {
  170. [controller setCurrentPlaybackTime:target];
  171. }
  172. - (void)onDisplayLink:(CADisplayLink *)link {
  173. [textures textureFrameAvailable:textureId];
  174. }
  175. - (CVPixelBufferRef _Nullable)copyPixelBuffer {
  176. CVPixelBufferRef newBuffer = [controller framePixelbuffer];
  177. if (newBuffer) {
  178. CFRetain(newBuffer);
  179. CVPixelBufferRef pixelBuffer = latestPixelBuffer;
  180. while (!OSAtomicCompareAndSwapPtrBarrier(pixelBuffer, newBuffer, (void **) &latestPixelBuffer)) {
  181. pixelBuffer = latestPixelBuffer;
  182. }
  183. return pixelBuffer;
  184. }
  185. return NULL;
  186. }
  187. - (CoolVideoInfo *)getInfo {
  188. CoolVideoInfo *info = [CoolVideoInfo new];
  189. CGSize size = [controller naturalSize];
  190. NSTimeInterval duration = [controller duration];
  191. NSTimeInterval currentPlaybackTime = [controller currentPlaybackTime];
  192. info.size = size;
  193. info.duration = duration;
  194. info.currentPosition = currentPlaybackTime;
  195. info.isPlaying = [controller isPlaying];
  196. info.degree = degree;
  197. return info;
  198. }
  199. - (NSUInteger)degreeFromVideoFileWithURL:(NSURL *)url {
  200. NSUInteger mDegree = 0;
  201. AVAsset *asset = [AVAsset assetWithURL:url];
  202. NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeVideo];
  203. if ([tracks count] > 0) {
  204. AVAssetTrack *videoTrack = tracks[0];
  205. CGAffineTransform t = videoTrack.preferredTransform;
  206. if (t.a == 0 && t.b == 1.0 && t.c == -1.0 && t.d == 0) {
  207. // Portrait
  208. mDegree = 90;
  209. } else if (t.a == 0 && t.b == -1.0 && t.c == 1.0 && t.d == 0) {
  210. // PortraitUpsideDown
  211. mDegree = 270;
  212. } else if (t.a == 1.0 && t.b == 0 && t.c == 0 && t.d == 1.0) {
  213. // LandscapeRight
  214. mDegree = 0;
  215. } else if (t.a == -1.0 && t.b == 0 && t.c == 0 && t.d == -1.0) {
  216. // LandscapeLeft
  217. mDegree = 180;
  218. }
  219. }
  220. return mDegree;
  221. }
  222. @end