CoolFlutterIJK.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. //
  2. // Created by Caijinglong on 2019-03-08.
  3. //
  4. #import "CoolFlutterIJK.h"
  5. #import "CoolVideoInfo.h"
  6. #import "CoolFlutterResult.h"
  7. #import <AVFoundation/AVFoundation.h>
  8. #import <libkern/OSAtomic.h>
  9. @interface CoolFlutterIJK () <FlutterTexture, KKIjkNotifyDelegate>
  10. @end
  11. @implementation CoolFlutterIJK {
  12. int64_t textureId;
  13. CADisplayLink *displayLink;
  14. NSObject <FlutterTextureRegistry> *textures;
  15. IJKFFMoviePlayerController *controller;
  16. CVPixelBufferRef latestPixelBuffer;
  17. FlutterMethodChannel *channel;
  18. CoolIjkNotifyChannel *notifyChannel;
  19. int degree;
  20. CoolFlutterResult *prepareResult;
  21. }
  22. - (instancetype)initWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
  23. self = [super init];
  24. if (self) {
  25. self.registrar = registrar;
  26. textures = [self.registrar textures];
  27. textureId = [textures registerTexture:self];
  28. NSString *channelName = [NSString stringWithFormat:@"top.kikt/ijkplayer/%lli", textureId];
  29. channel = [FlutterMethodChannel methodChannelWithName:channelName binaryMessenger:[registrar messenger]];
  30. __weak typeof(&*self) weakSelf = self;
  31. [channel setMethodCallHandler:^(FlutterMethodCall *call, FlutterResult result) {
  32. [weakSelf 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. NSDictionary *headers = params[@"headers"];
  61. [self setDataSourceWithUri:uri headers:headers result:[CoolFlutterResult resultWithResult:result]];
  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 result:[CoolFlutterResult resultWithResult:result]];
  74. }
  75. @catch (NSException *exception) {
  76. NSLog(@"Exception occurred: %@, %@", exception, [exception userInfo]);
  77. result([FlutterError errorWithCode:@"1" message:@"设置失败" details:nil]);
  78. }
  79. } else if ([@"setFileDataSource" isEqualToString:call.method]) {
  80. NSDictionary *params = call.arguments;
  81. NSString *path = params[@"path"];
  82. IJKFFMoviePlayerController *playerController = [self createControllerWithPath:path];
  83. [self setDataSourceWithController:playerController result:[CoolFlutterResult resultWithResult:result]];
  84. } else if ([@"seekTo" isEqualToString:call.method]) {
  85. NSDictionary *params = call.arguments;
  86. double target = [params[@"target"] doubleValue];
  87. [self seekTo:target];
  88. result(@(YES));
  89. } else if ([@"getInfo" isEqualToString:call.method]) {
  90. CoolVideoInfo *info = [self getInfo];
  91. result([info toMap]);
  92. } else if ([@"setVolume" isEqualToString:call.method]) {
  93. NSDictionary *params = [self params:call];
  94. float v = [params[@"volume"] floatValue] / 100;
  95. controller.playbackVolume = v;
  96. result(@(YES));
  97. } else if ([@"screenShot" isEqualToString:call.method]) {
  98. __weak typeof(&*self) weakSelf = self;
  99. dispatch_async(dispatch_get_main_queue(), ^{
  100. NSData *data = [weakSelf screenShot];
  101. result(data);
  102. });
  103. } else if ([@"setSpeed" isEqualToString:call.method]) {
  104. float speedValue = [call.arguments floatValue];
  105. if (controller) {
  106. [controller setPlaybackRate:speedValue];
  107. }
  108. result(@YES);
  109. } else {
  110. result(FlutterMethodNotImplemented);
  111. }
  112. }
  113. - (NSDictionary *)params:(FlutterMethodCall *)call {
  114. return call.arguments;
  115. }
  116. + (instancetype)ijkWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
  117. return [[self alloc] initWithRegistrar:registrar];
  118. }
  119. - (int64_t)id {
  120. return textureId;
  121. }
  122. - (void)play {
  123. [controller play];
  124. if (displayLink) {
  125. displayLink.paused = NO;
  126. }
  127. }
  128. - (void)pause {
  129. [controller pause];
  130. if (displayLink) {
  131. displayLink.paused = YES;
  132. }
  133. }
  134. - (void)stop {
  135. [controller stop];
  136. if (displayLink) {
  137. displayLink.paused = NO;
  138. }
  139. }
  140. - (void)setDataSourceWithController:(IJKFFMoviePlayerController *)ctl result:(CoolFlutterResult *)result {
  141. if (ctl) {
  142. controller = ctl;
  143. [self prepare:result];
  144. }
  145. }
  146. - (IJKFFOptions *)createOption {
  147. IJKFFOptions *options = [IJKFFOptions optionsByDefault];
  148. // see https://www.jianshu.com/p/843c86a9e9ad
  149. // [options setFormatOptionValue:@"fastseek" forKey:@"fflags"];
  150. // [options setFormatOptionIntValue:100 forKey:@"analyzemaxduration"];
  151. // [options setFormatOptionIntValue:1 forKey:@"analyzeduration"];
  152. // [options setFormatOptionIntValue:10240 forKey:@"probesize"];
  153. // [options setFormatOptionIntValue:1 forKey:@"flush_packets"];
  154. // [options setFormatOptionIntValue:5 forKey:@"reconnect"];
  155. // [options setFormatOptionIntValue:5 forKey:@"framedrop"];
  156. // [options setFormatOptionIntValue:1 forKey:@"enable-accurate-seek"];
  157. // [options setPlayerOptionIntValue:0 forKey:@"video-max-frame-width-default"];
  158. // [options setPlayerOptionIntValue:1 forKey:@"videotoolbox"];
  159. for (CoolIjkOption *opt in self.options) {
  160. if (opt) {
  161. NSString* key = opt.key;
  162. BOOL isString = [opt.value isKindOfClass:[NSString class]];
  163. BOOL isInt;
  164. if([opt.value isKindOfClass:[NSNumber class]]){
  165. isInt = strcmp([opt.value objCType], @encode(int)) == 0;
  166. }else{
  167. isInt = NO;
  168. }
  169. switch (opt.type) {
  170. case 0:
  171. if(isString){
  172. [options setFormatOptionValue:opt.value forKey:key];
  173. }else if(isInt){
  174. [options setFormatOptionIntValue:[opt.value intValue] forKey:key];
  175. }
  176. break;
  177. case 1:
  178. if(isString){
  179. [options setCodecOptionValue:opt.value forKey:key];
  180. }else if(isInt){
  181. [options setCodecOptionIntValue:[opt.value intValue] forKey:key];
  182. }
  183. break;
  184. case 2:
  185. if(isString){
  186. [options setSwsOptionValue:opt.value forKey:key];
  187. }else if(isInt){
  188. [options setSwsOptionIntValue:[opt.value intValue] forKey:key];
  189. }
  190. break;
  191. case 3:
  192. if(isString){
  193. [options setPlayerOptionValue:opt.value forKey:key];
  194. }else if(isInt){
  195. [options setPlayerOptionIntValue:[opt.value intValue] forKey:key];
  196. }
  197. break;
  198. default:
  199. break;
  200. }
  201. }
  202. }
  203. return options;
  204. }
  205. - (void)setDataSourceWithUri:(NSString *)uri headers:(NSDictionary *)headers result:(CoolFlutterResult *)result {
  206. IJKFFOptions *options = [self createOption];
  207. if (headers) {
  208. NSMutableString *headerString = [NSMutableString new];
  209. for (NSString *key in headers.allKeys) {
  210. NSString *value = headers[key];
  211. [headerString appendFormat:@"%@:%@", key, value];
  212. [headerString appendString:@"\r\n"];
  213. }
  214. [options setFormatOptionValue:headerString forKey:@"headers"];
  215. }
  216. controller = [[IJKFFMoviePlayerController alloc] initWithContentURLString:uri withOptions:options];
  217. [self prepare:result];
  218. }
  219. - (void)setDegree:(int)d {
  220. degree = d;
  221. }
  222. - (void)prepare:(CoolFlutterResult *)result {
  223. prepareResult = result;
  224. [controller prepareToPlay];
  225. if (displayLink) {
  226. displayLink.paused = YES;
  227. [displayLink invalidate];
  228. displayLink = nil;
  229. }
  230. displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(onDisplayLink:)];
  231. [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
  232. displayLink.paused = YES;
  233. notifyChannel = [CoolIjkNotifyChannel channelWithController:controller textureId:textureId registrar:self.registrar];
  234. notifyChannel.infoDelegate = self;
  235. }
  236. - (void)onLoadStateChange {
  237. // IJKMPMovieLoadState loadState = controller.loadState;
  238. if (prepareResult) {
  239. [prepareResult replyResult:@YES];
  240. }
  241. prepareResult = nil;
  242. }
  243. - (IJKFFMoviePlayerController *)createControllerWithAssetName:(NSString *)assetName pkg:(NSString *)pkg {
  244. NSString *asset;
  245. if (!pkg) {
  246. asset = [self.registrar lookupKeyForAsset:assetName];
  247. } else {
  248. asset = [self.registrar lookupKeyForAsset:assetName fromPackage:pkg];
  249. }
  250. NSString *path = [[NSBundle mainBundle] pathForResource:asset ofType:nil];
  251. NSURL *url = [NSURL fileURLWithPath:path];
  252. IJKFFOptions *options = [self createOption];
  253. return [[IJKFFMoviePlayerController alloc] initWithContentURL:url withOptions:options];
  254. }
  255. - (IJKFFMoviePlayerController *)createControllerWithPath:(NSString *)path {
  256. NSURL *url = [NSURL fileURLWithPath:path];
  257. IJKFFOptions *options = [self createOption];
  258. return [[IJKFFMoviePlayerController alloc] initWithContentURL:url withOptions:options];
  259. }
  260. - (void)seekTo:(double)target {
  261. [controller setCurrentPlaybackTime:target];
  262. }
  263. - (void)onDisplayLink:(CADisplayLink *)link {
  264. [textures textureFrameAvailable:textureId];
  265. }
  266. - (CVPixelBufferRef _Nullable)copyPixelBuffer {
  267. CVPixelBufferRef newBuffer = [controller framePixelbuffer];
  268. if (newBuffer) {
  269. CFRetain(newBuffer);
  270. CVPixelBufferRef pixelBuffer = latestPixelBuffer;
  271. while (!OSAtomicCompareAndSwapPtrBarrier(pixelBuffer, newBuffer, (void **) &latestPixelBuffer)) {
  272. pixelBuffer = latestPixelBuffer;
  273. }
  274. return pixelBuffer;
  275. }
  276. return NULL;
  277. }
  278. - (CoolVideoInfo *)getInfo {
  279. CoolVideoInfo *info = [CoolVideoInfo new];
  280. CGSize size = [controller naturalSize];
  281. NSTimeInterval duration = [controller duration];
  282. NSTimeInterval currentPlaybackTime = [controller currentPlaybackTime];
  283. info.size = size;
  284. info.duration = duration;
  285. info.currentPosition = currentPlaybackTime;
  286. info.isPlaying = [controller isPlaying];
  287. info.degree = degree;
  288. info.tcpSpeed = [controller tcpSpeed];
  289. info.outputFps = [controller fpsAtOutput];
  290. return info;
  291. }
  292. - (NSUInteger)degreeFromVideoFileWithURL:(NSURL *)url {
  293. NSUInteger mDegree = 0;
  294. AVAsset *asset = [AVAsset assetWithURL:url];
  295. NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeVideo];
  296. if ([tracks count] > 0) {
  297. AVAssetTrack *videoTrack = tracks[0];
  298. CGAffineTransform t = videoTrack.preferredTransform;
  299. if (t.a == 0 && t.b == 1.0 && t.c == -1.0 && t.d == 0) {
  300. // Portrait
  301. mDegree = 90;
  302. } else if (t.a == 0 && t.b == -1.0 && t.c == 1.0 && t.d == 0) {
  303. // PortraitUpsideDown
  304. mDegree = 270;
  305. } else if (t.a == 1.0 && t.b == 0 && t.c == 0 && t.d == 1.0) {
  306. // LandscapeRight
  307. mDegree = 0;
  308. } else if (t.a == -1.0 && t.b == 0 && t.c == 0 && t.d == -1.0) {
  309. // LandscapeLeft
  310. mDegree = 180;
  311. }
  312. }
  313. return mDegree;
  314. }
  315. - (NSData*) screenShot{
  316. CVPixelBufferRef ref = [self copyPixelBuffer];
  317. if(!ref){
  318. return nil;
  319. }
  320. UIImage *img = [self convertPixeclBufferToUIImage:ref];
  321. return UIImageJPEGRepresentation(img, 1.0);
  322. }
  323. -(UIImage*)convertPixeclBufferToUIImage:(CVPixelBufferRef)pixelBuffer{
  324. CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];
  325. CIContext *temporaryContext = [CIContext contextWithOptions:nil];
  326. CGImageRef videoImage = [temporaryContext
  327. createCGImage:ciImage
  328. fromRect:CGRectMake(0, 0, CVPixelBufferGetWidth(pixelBuffer), CVPixelBufferGetHeight(pixelBuffer))];
  329. UIImage *uiImage = [UIImage imageWithCGImage:videoImage];
  330. CGImageRelease(videoImage);
  331. return uiImage;
  332. }
  333. @end