Skip to content

Commit

Permalink
simplify execute methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tanersener committed Jan 2, 2022
1 parent f4b0510 commit 8a00606
Show file tree
Hide file tree
Showing 14 changed files with 253 additions and 157 deletions.
4 changes: 2 additions & 2 deletions flutter/flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
## 4.5.1
- Bugfix release
- Feature release based on native v4.5.1

## 4.5.1-LTS
- Bugfix LTS release
- Feature release based on native v4.5.1.LTS

## 4.5.0
- Initial release
Expand Down
2 changes: 1 addition & 1 deletion flutter/flutter/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ repositories {

dependencies {
implementation 'androidx.annotation:annotation:1.2.0'
implementation 'com.arthenica:ffmpeg-kit-https:4.5.1'
implementation 'com.arthenica:ffmpeg-kit-https:4.5.1-1'
}

Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,13 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
resultHandler.errorAsync(result, "INVALID_ARGUMENTS", "Invalid arguments array.");
}
break;
case "getMediaInformation":
if (sessionId != null) {
getMediaInformation(sessionId, result);
} else {
resultHandler.errorAsync(result, "INVALID_SESSION", "Invalid session id.");
}
break;
case "mediaInformationJsonParserFrom":
if (ffprobeJsonOutput != null) {
mediaInformationJsonParserFrom(ffprobeJsonOutput, result);
Expand Down Expand Up @@ -876,6 +883,21 @@ protected void mediaInformationSession(@NonNull final List<String> arguments, @N
resultHandler.successAsync(result, toMap(session));
}

protected void getMediaInformation(@NonNull final Integer sessionId, @NonNull final Result result) {
final Session session = FFmpegKitConfig.getSession(sessionId.longValue());
if (session == null) {
resultHandler.errorAsync(result, "SESSION_NOT_FOUND", "Session not found.");
} else {
if (session.isMediaInformation()) {
final MediaInformationSession mediaInformationSession = (MediaInformationSession) session;
final MediaInformation mediaInformation = mediaInformationSession.getMediaInformation();
resultHandler.successAsync(result, toMap(mediaInformation));
} else {
resultHandler.errorAsync(result, "NOT_MEDIA_INFORMATION_SESSION", "A session is found but it does not have the correct type.");
}
}
}

// MediaInformationJsonParser

protected void mediaInformationJsonParserFrom(@NonNull final String ffprobeJsonOutput, @NonNull final Result result) {
Expand Down Expand Up @@ -1332,19 +1354,17 @@ protected static Map<String, Object> toMap(final Session session) {
sessionMap.put(KEY_SESSION_START_TIME, toLong(session.getStartTime()));
sessionMap.put(KEY_SESSION_COMMAND, session.getCommand());

if (session.isFFprobe()) {
if (session.isMediaInformation()) {
final MediaInformationSession mediaInformationSession = (MediaInformationSession) session;
final MediaInformation mediaInformation = mediaInformationSession.getMediaInformation();
if (mediaInformation != null) {
sessionMap.put(KEY_SESSION_MEDIA_INFORMATION, toMap(mediaInformation));
}
sessionMap.put(KEY_SESSION_TYPE, SESSION_TYPE_MEDIA_INFORMATION);
} else {
sessionMap.put(KEY_SESSION_TYPE, SESSION_TYPE_FFPROBE);
}
} else {
if (session.isFFmpeg()) {
sessionMap.put(KEY_SESSION_TYPE, SESSION_TYPE_FFMPEG);
} else if (session.isFFprobe()) {
sessionMap.put(KEY_SESSION_TYPE, SESSION_TYPE_FFPROBE);
} else if (session.isMediaInformation()) {
final MediaInformationSession mediaInformationSession = (MediaInformationSession) session;
final MediaInformation mediaInformation = mediaInformationSession.getMediaInformation();
if (mediaInformation != null) {
sessionMap.put(KEY_SESSION_MEDIA_INFORMATION, toMap(mediaInformation));
}
sessionMap.put(KEY_SESSION_TYPE, SESSION_TYPE_MEDIA_INFORMATION);
}

return sessionMap;
Expand Down Expand Up @@ -1432,18 +1452,20 @@ protected static Map<String, Object> toMap(final Statistics statistics) {
}

protected static Map<String, Object> toMap(final MediaInformation mediaInformation) {
Map<String, Object> map = new HashMap<>();

if (mediaInformation != null) {
Map<String, Object> map = new HashMap<>();

if (mediaInformation.getAllProperties() != null) {
JSONObject allProperties = mediaInformation.getAllProperties();
if (allProperties != null) {
map = toMap(allProperties);
}
}
}

return map;
return map;
} else {
return null;
}
}

protected static Map<String, Object> toMap(final JSONObject jsonObject) {
Expand Down
36 changes: 27 additions & 9 deletions flutter/flutter/ios/Classes/FFmpegKitFlutterPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
} else {
result([FlutterError errorWithCode:@"INVALID_ARGUMENTS" message:@"Invalid arguments array." details:nil]);
}
} else if ([@"getMediaInformation" isEqualToString:call.method]) {
if (sessionId != nil) {
[self getMediaInformation:sessionId result:result];
} else {
result([FlutterError errorWithCode:@"INVALID_SESSION" message:@"Invalid session id." details:nil]);
}
} else if ([@"mediaInformationJsonParserFrom" isEqualToString:call.method]) {
if (ffprobeJsonOutput != nil) {
[self mediaInformationJsonParserFrom:ffprobeJsonOutput result:result];
Expand Down Expand Up @@ -609,6 +615,20 @@ - (void)mediaInformationSession:(NSArray*)arguments result:(FlutterResult)result
result([FFmpegKitFlutterPlugin toSessionDictionary:session]);
}

- (void)getMediaInformation:(NSNumber*)sessionId result:(FlutterResult)result {
AbstractSession* session = (AbstractSession*)[FFmpegKitConfig getSession:[sessionId longValue]];
if (session == nil) {
result([FlutterError errorWithCode:@"SESSION_NOT_FOUND" message:@"Session not found." details:nil]);
} else {
if ([session isMediaInformation]) {
MediaInformationSession *mediaInformationSession = (MediaInformationSession*)session;
result([FFmpegKitFlutterPlugin toMediaInformationDictionary:[mediaInformationSession getMediaInformation]]);
} else {
result([FlutterError errorWithCode:@"NOT_MEDIA_INFORMATION_SESSION" message:@"A session is found but it does not have the correct type." details:nil]);
}
}
}

// MediaInformationJsonParser

- (void)mediaInformationJsonParserFrom:(NSString*)ffprobeJsonOutput result:(FlutterResult)result {
Expand Down Expand Up @@ -1061,16 +1081,14 @@ + (NSDictionary*)toSessionDictionary:(id<Session>) session {
dictionary[KEY_SESSION_START_TIME] = [NSNumber numberWithLong:[[session getStartTime] timeIntervalSince1970]*1000];
dictionary[KEY_SESSION_COMMAND] = [session getCommand];

if ([session isFFprobe]) {
if ([session isMediaInformation]) {
MediaInformationSession *mediaInformationSession = (MediaInformationSession*)session;
dictionary[KEY_SESSION_MEDIA_INFORMATION] = [FFmpegKitFlutterPlugin toMediaInformationDictionary:[mediaInformationSession getMediaInformation]];
dictionary[KEY_SESSION_TYPE] = [NSNumber numberWithInt:SESSION_TYPE_MEDIA_INFORMATION];
} else {
dictionary[KEY_SESSION_TYPE] = [NSNumber numberWithInt:SESSION_TYPE_FFPROBE];
}
} else {
if ([session isFFmpeg]) {
dictionary[KEY_SESSION_TYPE] = [NSNumber numberWithInt:SESSION_TYPE_FFMPEG];
} else if ([session isFFprobe]) {
dictionary[KEY_SESSION_TYPE] = [NSNumber numberWithInt:SESSION_TYPE_FFPROBE];
} else if ([session isMediaInformation]) {
MediaInformationSession *mediaInformationSession = (MediaInformationSession*)session;
dictionary[KEY_SESSION_MEDIA_INFORMATION] = [FFmpegKitFlutterPlugin toMediaInformationDictionary:[mediaInformationSession getMediaInformation]];
dictionary[KEY_SESSION_TYPE] = [NSNumber numberWithInt:SESSION_TYPE_MEDIA_INFORMATION];
}

return dictionary;
Expand Down
19 changes: 6 additions & 13 deletions flutter/flutter/lib/ffmpeg_kit.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2021 Taner Sener
* Copyright (c) 2019-2022 Taner Sener
*
* This file is part of FFmpegKit.
*
Expand Down Expand Up @@ -34,21 +34,14 @@ class FFmpegKit {
/// Synchronously executes FFmpeg command provided. Space character is used
/// to split command into arguments. You can use single or double quote
/// characters to specify arguments inside your command.
static Future<FFmpegSession> execute(String command,
[FFmpegSessionCompleteCallback? completeCallback = null,
LogCallback? logCallback = null,
StatisticsCallback? statisticsCallback = null]) async =>
FFmpegKit.executeWithArguments(FFmpegKitConfig.parseArguments(command),
completeCallback, logCallback, statisticsCallback);
static Future<FFmpegSession> execute(String command) async =>
FFmpegKit.executeWithArguments(FFmpegKitConfig.parseArguments(command));

/// Synchronously executes FFmpeg with arguments provided.
static Future<FFmpegSession> executeWithArguments(
List<String> commandArguments,
[FFmpegSessionCompleteCallback? completeCallback = null,
LogCallback? logCallback = null,
StatisticsCallback? statisticsCallback = null]) async {
final session = await FFmpegSession.create(commandArguments,
completeCallback, logCallback, statisticsCallback, null);
List<String> commandArguments) async {
final session =
await FFmpegSession.create(commandArguments, null, null, null, null);

await FFmpegKitConfig.ffmpegExecute(session);

Expand Down
100 changes: 50 additions & 50 deletions flutter/flutter/lib/ffmpeg_kit_config.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2021 Taner Sener
* Copyright (c) 2019-2022 Taner Sener
*
* This file is part of FFmpegKit.
*
Expand Down Expand Up @@ -366,6 +366,55 @@ class FFmpegKitConfig {
}
}

/// Converts the given Structured Access Framework Uri ("content:…") into
/// an input url that can be used in FFmpeg and FFprobe commands.
///
/// Note that this method is Android only. It will fail if called on other
/// platforms. It also requires API Level &ge; 19. On older API levels it
/// returns an empty url.
static Future<String?> getSafParameterForRead(String uriString) async {
try {
await init();
return _platform.ffmpegKitConfigGetSafParameter(uriString, "r");
} on PlatformException catch (e, stack) {
print("Plugin getSafParameterForRead error: ${e.message}");
return Future.error("getSafParameterForRead failed.", stack);
}
}

/// Converts the given Structured Access Framework Uri ("content:…") into
/// an output url that can be used in FFmpeg and FFprobe commands.
///
/// Note that this method is Android only. It will fail if called on other
/// platforms. It also requires API Level &ge; 19. On older API levels it
/// returns an empty url.
static Future<String?> getSafParameterForWrite(String uriString) async {
try {
await init();
return _platform.ffmpegKitConfigGetSafParameter(uriString, "w");
} on PlatformException catch (e, stack) {
print("Plugin getSafParameterForWrite error: ${e.message}");
return Future.error("getSafParameterForWrite failed.", stack);
}
}

/// Converts the given Structured Access Framework Uri into an saf protocol
/// url opened with the given open mode.
///
/// Note that this method is Android only. It will fail if called on other
/// platforms. It also requires API Level &ge; 19. On older API levels it
/// returns an empty url.
static Future<String?> getSafParameter(
String uriString, String openMode) async {
try {
await init();
return _platform.ffmpegKitConfigGetSafParameter(uriString, openMode);
} on PlatformException catch (e, stack) {
print("Plugin getSafParameter error: ${e.message}");
return Future.error("getSafParameter failed.", stack);
}
}

/// Returns the session history size.
static Future<int?> getSessionHistorySize() async {
try {
Expand Down Expand Up @@ -763,53 +812,4 @@ class FFmpegKitConfig {
return Future.error("selectDocumentForWrite failed.", stack);
}
}

/// Converts the given Structured Access Framework Uri ("content:…") into
/// an input url that can be used in FFmpeg and FFprobe commands.
///
/// Note that this method is Android only. It will fail if called on other
/// platforms. It also requires API Level &ge; 19. On older API levels it
/// returns an empty url.
static Future<String?> getSafParameterForRead(String uriString) async {
try {
await init();
return _platform.ffmpegKitConfigGetSafParameter(uriString, "r");
} on PlatformException catch (e, stack) {
print("Plugin getSafParameterForRead error: ${e.message}");
return Future.error("getSafParameterForRead failed.", stack);
}
}

/// Converts the given Structured Access Framework Uri ("content:…") into
/// an output url that can be used in FFmpeg and FFprobe commands.
///
/// Note that this method is Android only. It will fail if called on other
/// platforms. It also requires API Level &ge; 19. On older API levels it
/// returns an empty url.
static Future<String?> getSafParameterForWrite(String uriString) async {
try {
await init();
return _platform.ffmpegKitConfigGetSafParameter(uriString, "w");
} on PlatformException catch (e, stack) {
print("Plugin getSafParameterForWrite error: ${e.message}");
return Future.error("getSafParameterForWrite failed.", stack);
}
}

/// Converts the given Structured Access Framework Uri into an saf protocol
/// url opened with the given open mode.
///
/// Note that this method is Android only. It will fail if called on other
/// platforms. It also requires API Level &ge; 19. On older API levels it
/// returns an empty url.
static Future<String?> getSafParameter(
String uriString, String openMode) async {
try {
await init();
return _platform.ffmpegKitConfigGetSafParameter(uriString, openMode);
} on PlatformException catch (e, stack) {
print("Plugin getSafParameter error: ${e.message}");
return Future.error("getSafParameter failed.", stack);
}
}
}
Loading

0 comments on commit 8a00606

Please sign in to comment.