This repository was archived by the owner on Oct 14, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathFlutterFFmpegPlugin.m
352 lines (248 loc) · 13.3 KB
/
FlutterFFmpegPlugin.m
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
* Copyright (c) 2019-2020 Taner Sener
*
* This file is part of FlutterFFmpeg.
*
* FlutterFFmpeg is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FlutterFFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FlutterFFmpeg. If not, see <http://www.gnu.org/licenses/>.
*/
#import "EmptyLogDelegate.h"
#import "FlutterExecuteDelegate.h"
#import "FlutterFFmpegPlugin.h"
#import <stdlib.h>
#import <mobileffmpeg/ArchDetect.h>
#import <mobileffmpeg/MobileFFmpegConfig.h>
#import <mobileffmpeg/MobileFFmpeg.h>
#import <mobileffmpeg/MobileFFprobe.h>
static NSString *const PLATFORM_NAME = @"ios";
static NSString *const KEY_VERSION = @"version";
static NSString *const KEY_RC = @"rc";
static NSString *const KEY_PLATFORM = @"platform";
static NSString *const KEY_PACKAGE_NAME = @"packageName";
static NSString *const KEY_LAST_RC = @"lastRc";
static NSString *const KEY_LAST_COMMAND_OUTPUT = @"lastCommandOutput";
static NSString *const KEY_PIPE = @"pipe";
static NSString *const KEY_LOG_EXECUTION_ID = @"executionId";
static NSString *const KEY_LOG_LEVEL = @"level";
static NSString *const KEY_LOG_MESSAGE = @"message";
static NSString *const KEY_STAT_EXECUTION_ID = @"executionId";
static NSString *const KEY_STAT_TIME = @"time";
static NSString *const KEY_STAT_SIZE = @"size";
static NSString *const KEY_STAT_BITRATE = @"bitrate";
static NSString *const KEY_STAT_SPEED = @"speed";
static NSString *const KEY_STAT_VIDEO_FRAME_NUMBER = @"videoFrameNumber";
static NSString *const KEY_STAT_VIDEO_QUALITY = @"videoQuality";
static NSString *const KEY_STAT_VIDEO_FPS = @"videoFps";
static NSString *const KEY_EXECUTION_ID = @"executionId";
static NSString *const KEY_EXECUTION_START_TIME = @"startTime";
static NSString *const KEY_EXECUTION_COMMAND = @"command";
static NSString *const EVENT_LOG = @"FlutterFFmpegLogCallback";
static NSString *const EVENT_STAT = @"FlutterFFmpegStatisticsCallback";
/**
* Flutter FFmpeg Plugin
*/
@implementation FlutterFFmpegPlugin {
FlutterEventSink _eventSink;
EmptyLogDelegate *_emptyLogDelegate;
}
- (instancetype)init {
self = [super init];
if (self) {
_emptyLogDelegate = [[EmptyLogDelegate alloc] init];
}
return self;
}
- (FlutterError *)onListenWithArguments:(id)arguments eventSink:(FlutterEventSink)eventSink {
_eventSink = eventSink;
return nil;
}
- (FlutterError *)onCancelWithArguments:(id)arguments {
_eventSink = nil;
return nil;
}
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
FlutterFFmpegPlugin* instance = [[FlutterFFmpegPlugin alloc] init];
FlutterMethodChannel* methodChannel = [FlutterMethodChannel methodChannelWithName:@"flutter_ffmpeg" binaryMessenger:[registrar messenger]];
[registrar addMethodCallDelegate:instance channel:methodChannel];
FlutterEventChannel* eventChannel = [FlutterEventChannel eventChannelWithName:@"flutter_ffmpeg_event" binaryMessenger:[registrar messenger]];
[eventChannel setStreamHandler:instance];
}
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
// ARGUMENTS
NSArray* arguments = call.arguments[@"arguments"];
if ([@"getPlatform" isEqualToString:call.method]) {
NSString *architecture = [ArchDetect getArch];
result([FlutterFFmpegPlugin toStringDictionary:KEY_PLATFORM :[NSString stringWithFormat:@"%@-%@", PLATFORM_NAME, architecture]]);
} else if ([@"getFFmpegVersion" isEqualToString:call.method]) {
result([FlutterFFmpegPlugin toStringDictionary:KEY_VERSION :[MobileFFmpegConfig getFFmpegVersion]]);
} else if ([@"executeFFmpegWithArguments" isEqualToString:call.method]) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
int rc = [MobileFFmpeg executeWithArguments:arguments];
result([FlutterFFmpegPlugin toIntDictionary:KEY_RC :[NSNumber numberWithInt:rc]]);
});
} else if ([@"executeFFmpegAsyncWithArguments" isEqualToString:call.method]) {
FlutterExecuteDelegate* executeDelegate = [[FlutterExecuteDelegate alloc] initWithEventSink:_eventSink];
long executionId = [MobileFFmpeg executeWithArgumentsAsync:arguments withCallback:executeDelegate];
result([FlutterFFmpegPlugin toIntDictionary:KEY_EXECUTION_ID :[NSNumber numberWithLong:executionId]]);
} else if ([@"executeFFprobeWithArguments" isEqualToString:call.method]) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
int rc = [MobileFFprobe executeWithArguments:arguments];
result([FlutterFFmpegPlugin toIntDictionary:KEY_RC :[NSNumber numberWithInt:rc]]);
});
} else if ([@"cancel" isEqualToString:call.method]) {
NSNumber* executionId = call.arguments[@"executionId"];
if (executionId == nil) {
[MobileFFmpeg cancel];
} else {
[MobileFFmpeg cancel:[executionId longValue]];
}
} else if ([@"enableRedirection" isEqualToString:call.method]) {
[MobileFFmpegConfig enableRedirection];
} else if ([@"disableRedirection" isEqualToString:call.method]) {
[MobileFFmpegConfig disableRedirection];
} else if ([@"getLogLevel" isEqualToString:call.method]) {
int logLevel = [MobileFFmpegConfig getLogLevel];
result([FlutterFFmpegPlugin toIntDictionary:KEY_LOG_LEVEL :[NSNumber numberWithInt:logLevel]]);
} else if ([@"setLogLevel" isEqualToString:call.method]) {
NSNumber* logLevel = call.arguments[@"level"];
[MobileFFmpegConfig setLogLevel:[logLevel intValue]];
} else if ([@"enableLogs" isEqualToString:call.method]) {
[MobileFFmpegConfig setLogDelegate:self];
} else if ([@"disableLogs" isEqualToString:call.method]) {
[MobileFFmpegConfig setLogDelegate:_emptyLogDelegate];
} else if ([@"enableStatistics" isEqualToString:call.method]) {
[MobileFFmpegConfig setStatisticsDelegate:self];
} else if ([@"disableStatistics" isEqualToString:call.method]) {
[MobileFFmpegConfig setStatisticsDelegate:nil];
} else if ([@"getLastReceivedStatistics" isEqualToString:call.method]) {
Statistics *statistics = [MobileFFmpegConfig getLastReceivedStatistics];
result([FlutterFFmpegPlugin toStatisticsDictionary:statistics]);
} else if ([@"resetStatistics" isEqualToString:call.method]) {
[MobileFFmpegConfig resetStatistics];
} else if ([@"setFontconfigConfigurationPath" isEqualToString:call.method]) {
NSString* path = call.arguments[@"path"];
[MobileFFmpegConfig setFontconfigConfigurationPath:path];
} else if ([@"setFontDirectory" isEqualToString:call.method]) {
NSString* fontDirectoryPath = call.arguments[@"fontDirectory"];
NSDictionary* fontNameMapping = call.arguments[@"fontNameMap"];
[MobileFFmpegConfig setFontDirectory:fontDirectoryPath with:fontNameMapping];
} else if ([@"getPackageName" isEqualToString:call.method]) {
NSString *packageName = [MobileFFmpegConfig getPackageName];
result([FlutterFFmpegPlugin toStringDictionary:KEY_PACKAGE_NAME :packageName]);
} else if ([@"getExternalLibraries" isEqualToString:call.method]) {
NSArray *externalLibraries = [MobileFFmpegConfig getExternalLibraries];
result(externalLibraries);
} else if ([@"getLastReturnCode" isEqualToString:call.method]) {
int lastReturnCode = [MobileFFmpegConfig getLastReturnCode];
result([FlutterFFmpegPlugin toIntDictionary:KEY_LAST_RC :[NSNumber numberWithInt:lastReturnCode]]);
} else if ([@"getLastCommandOutput" isEqualToString:call.method]) {
NSString *lastCommandOutput = [MobileFFmpegConfig getLastCommandOutput];
result([FlutterFFmpegPlugin toStringDictionary:KEY_LAST_COMMAND_OUTPUT :lastCommandOutput]);
} else if ([@"getMediaInformation" isEqualToString:call.method]) {
NSString* path = call.arguments[@"path"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
MediaInformation *mediaInformation = [MobileFFprobe getMediaInformation:path];
result([FlutterFFmpegPlugin toMediaInformationDictionary:mediaInformation]);
});
} else if ([@"registerNewFFmpegPipe" isEqualToString:call.method]) {
NSString *pipe = [MobileFFmpegConfig registerNewFFmpegPipe];
result([FlutterFFmpegPlugin toStringDictionary:KEY_PIPE :pipe]);
} else if ([@"closeFFmpegPipe" isEqualToString:call.method]) {
NSString* ffmpegPipePath = call.arguments[@"ffmpegPipePath"];
[MobileFFmpegConfig closeFFmpegPipe:ffmpegPipePath];
} else if ([@"setEnvironmentVariable" isEqualToString:call.method]) {
NSString* variableName = call.arguments[@"variableName"];
NSString* variableValue = call.arguments[@"variableValue"];
setenv([variableName UTF8String], [variableValue UTF8String], true);
} else if ([@"listExecutions" isEqualToString:call.method]) {
NSArray* executionsArray = [FlutterFFmpegPlugin toExecutionsArray:[MobileFFmpeg listExecutions]];
result(executionsArray);
} else {
result(FlutterMethodNotImplemented);
}
}
- (void)logCallback: (long)executionId :(int)level :(NSString*)message {
dispatch_async(dispatch_get_main_queue(), ^{
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
dictionary[KEY_LOG_EXECUTION_ID] = [NSNumber numberWithLong:executionId];
dictionary[KEY_LOG_LEVEL] = [NSNumber numberWithInt:level];
dictionary[KEY_LOG_MESSAGE] = message;
[self emitLogMessage: dictionary];
});
}
- (void)statisticsCallback:(Statistics *)statistics {
dispatch_async(dispatch_get_main_queue(), ^{
[self emitStatistics: statistics];
});
}
- (void)emitLogMessage:(NSDictionary*)logMessage{
_eventSink([FlutterFFmpegPlugin toStringDictionary:EVENT_LOG withDictionary:logMessage]);
}
- (void)emitStatistics:(Statistics*)statistics{
NSDictionary *dictionary = [FlutterFFmpegPlugin toStatisticsDictionary:statistics];
_eventSink([FlutterFFmpegPlugin toStringDictionary:EVENT_STAT withDictionary:dictionary]);
}
+ (NSDictionary *)toStringDictionary:(NSString*)key :(NSString*)value {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
dictionary[key] = value;
return dictionary;
}
+ (NSDictionary *)toStringDictionary:(NSString*)key withDictionary:(NSDictionary*)value {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
dictionary[key] = value;
return dictionary;
}
+ (NSDictionary *)toIntDictionary:(NSString*)key :(NSNumber*)value {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
dictionary[key] = value;
return dictionary;
}
+ (NSDictionary *)toStatisticsDictionary:(Statistics*)statistics {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
if (statistics != nil) {
dictionary[KEY_STAT_EXECUTION_ID] = [NSNumber numberWithLong: [statistics getExecutionId]];
dictionary[KEY_STAT_TIME] = [NSNumber numberWithInt: [statistics getTime]];
dictionary[KEY_STAT_SIZE] = [NSNumber numberWithLong: [statistics getSize]];
dictionary[KEY_STAT_BITRATE] = [NSNumber numberWithDouble: [statistics getBitrate]];
dictionary[KEY_STAT_SPEED] = [NSNumber numberWithDouble: [statistics getSpeed]];
dictionary[KEY_STAT_VIDEO_FRAME_NUMBER] = [NSNumber numberWithInt: [statistics getVideoFrameNumber]];
dictionary[KEY_STAT_VIDEO_QUALITY] = [NSNumber numberWithFloat: [statistics getVideoQuality]];
dictionary[KEY_STAT_VIDEO_FPS] = [NSNumber numberWithFloat: [statistics getVideoFps]];
}
return dictionary;
}
+ (NSArray *)toExecutionsArray:(NSArray*)ffmpegExecutions {
NSMutableArray *executions = [[NSMutableArray alloc] init];
for (int i = 0; i < [ffmpegExecutions count]; i++) {
FFmpegExecution* execution = [ffmpegExecutions objectAtIndex:i];
NSMutableDictionary *executionDictionary = [[NSMutableDictionary alloc] init];
executionDictionary[KEY_EXECUTION_ID] = [NSNumber numberWithLong: [execution getExecutionId]];
executionDictionary[KEY_EXECUTION_START_TIME] = [NSNumber numberWithDouble:[[execution getStartTime] timeIntervalSince1970]*1000];
executionDictionary[KEY_EXECUTION_COMMAND] = [execution getCommand];
[executions addObject: executionDictionary];
}
return executions;
}
+ (NSDictionary *)toMediaInformationDictionary:(MediaInformation*)mediaInformation {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
if (mediaInformation != nil) {
NSDictionary* allProperties = [mediaInformation getAllProperties];
if (allProperties != nil) {
for(NSString *key in [allProperties allKeys]) {
dictionary[key] = [allProperties objectForKey:key];
}
}
}
return dictionary;
}
@end