/*
* Copyright (c) 2019 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 .
*/
import 'dart:async';
import 'package:flutter/services.dart';
class FlutterFFmpegConfig {
static const MethodChannel _methodChannel =
const MethodChannel('flutter_ffmpeg');
static const EventChannel _eventChannel =
const EventChannel('flutter_ffmpeg_event');
Function(int level, String message) logCallback;
Function(
int time,
int size,
double bitrate,
double speed,
int videoFrameNumber,
double videoQuality,
double videoFps) statisticsCallback;
FlutterFFmpegConfig() {
logCallback = null;
statisticsCallback = null;
print("Loading flutter-ffmpeg.");
_eventChannel.receiveBroadcastStream().listen(_onEvent, onError: _onError);
enableLogs();
enableStatistics();
enableRedirection();
getPlatform().then((name) => print("Loaded flutter-ffmpeg-$name."));
}
void _onEvent(Object event) {
if (event is Map) {
final Map eventMap = event.cast();
final Map logEvent =
eventMap['FlutterFFmpegLogCallback'];
final Map statisticsEvent =
eventMap['FlutterFFmpegStatisticsCallback'];
if (logEvent != null) {
int level = logEvent['level'];
String message = logEvent['log'];
if (this.logCallback == null) {
if (message.length > 0) {
// PRINT ALREADY ADDS NEW LINE. SO REMOVE THIS ONE
if (message.endsWith('\n')) {
print(message.substring(0, message.length - 1));
} else {
print(message);
}
}
} else {
this.logCallback(level, message);
}
}
if (statisticsEvent != null) {
if (this.statisticsCallback != null) {
int time = statisticsEvent['time'];
int size = statisticsEvent['size'];
double bitrate = _doublePrecision(statisticsEvent['bitrate'], 2);
double speed = _doublePrecision(statisticsEvent['speed'], 2);
int videoFrameNumber = statisticsEvent['videoFrameNumber'];
double videoQuality =
_doublePrecision(statisticsEvent['videoQuality'], 2);
double videoFps = _doublePrecision(statisticsEvent['videoFps'], 2);
this.statisticsCallback(time, size, bitrate, speed, videoFrameNumber,
videoQuality, videoFps);
}
}
}
}
void _onError(Object error) {
print('Event error: $error');
}
double _doublePrecision(double value, int precision) {
if (value == null) {
return 0;
} else {
return num.parse(value.toStringAsFixed(precision));
}
}
/// Returns FFmpeg version bundled within the library.
Future getFFmpegVersion() async {
try {
final Map result =
await _methodChannel.invokeMethod('getFFmpegVersion');
return result['version'];
} on PlatformException catch (e) {
print("Plugin error: ${e.message}");
return null;
}
}
/// Returns platform name where library is loaded.
Future getPlatform() async {
try {
final Map result =
await _methodChannel.invokeMethod('getPlatform');
return result['platform'];
} on PlatformException catch (e) {
print("Plugin error: ${e.message}");
return null;
}
}
/// Enables redirection
Future enableRedirection() async {
try {
await _methodChannel.invokeMethod('enableRedirection');
} on PlatformException catch (e) {
print("Plugin error: ${e.message}");
}
}
/// Disables log and statistics redirection. By default redirection is enabled in constructor.
/// When redirection is enabled FFmpeg logs are printed to console and can be routed further to a callback function.
/// By disabling redirection, logs are redirected to stderr.
/// Statistics redirection behaviour is similar. Statistics are not printed at all if redirection is not enabled.
/// If it is enabled then it is possible to define a statistics callback function but if you don't, they are not
/// printed anywhere and only saved as codelastReceivedStatistics data which can be polled with
/// [getLastReceivedStatistics()].
Future disableRedirection() async {
try {
await _methodChannel.invokeMethod('disableRedirection');
} on PlatformException catch (e) {
print("Plugin error: ${e.message}");
}
}
/// Returns log level.
Future getLogLevel() async {
try {
final Map result =
await _methodChannel.invokeMethod('getLogLevel');
return result['level'];
} on PlatformException catch (e) {
print("Plugin error: ${e.message}");
return -1;
}
}
/// Sets log level.
Future setLogLevel(int logLevel) async {
try {
await _methodChannel.invokeMethod('setLogLevel', {'level': logLevel});
} on PlatformException catch (e) {
print("Plugin error: ${e.message}");
}
}
/// Enables log events
Future enableLogs() async {
try {
await _methodChannel.invokeMethod('enableLogs');
} on PlatformException catch (e) {
print("Plugin error: ${e.message}");
}
}
/// Disables log functionality of the library. Logs will not be printed to console and log callback will be disabled.
/// Note that log functionality is enabled by default.
Future disableLogs() async {
try {
await _methodChannel.invokeMethod('disableLogs');
} on PlatformException catch (e) {
print("Plugin error: ${e.message}");
}
}
/// Enables statistics events.
Future enableStatistics() async {
try {
await _methodChannel.invokeMethod('enableStatistics');
} on PlatformException catch (e) {
print("Plugin error: ${e.message}");
}
}
/// Disables statistics functionality of the library. Statistics callback will be disabled but the last received
/// statistics data will be still available.
/// Note that statistics functionality is enabled by default.
Future disableStatistics() async {
try {
await _methodChannel.invokeMethod('disableStatistics');
} on PlatformException catch (e) {
print("Plugin error: ${e.message}");
}
}
/// Sets a callback to redirect FFmpeg logs. [newCallback] is a new log callback function, use null to disable a previously defined callback
void enableLogCallback(Function(int level, String message) newCallback) {
try {
this.logCallback = newCallback;
} on PlatformException catch (e) {
print("Plugin error: ${e.message}");
}
}
/// Sets a callback to redirect FFmpeg statistics. [newCallback] is a new statistics callback function, use null to disable a previously defined callback
void enableStatisticsCallback(
Function(int time, int size, double bitrate, double speed,
int videoFrameNumber, double videoQuality, double videoFps)
newCallback) {
try {
this.statisticsCallback = newCallback;
} on PlatformException catch (e) {
print("Plugin error: ${e.message}");
}
}
/// Returns the last received statistics data stored in bitrate, size, speed, time, videoFps, videoFrameNumber and
/// videoQuality fields
Future