Skip to content

Commit 1cce991

Browse files
committed
Linux support
this uses the native (nativest) option on linux: ffmpeg. Because LGPL may be an issue, this adds support using calls to the ffmpeg binary, which is expected to be present on the user's system, instead of linking to libav. Encoding uses libx264 and ffmpeg's built-in AAC encoder for maximum compatibility with user installs. The code is written in C++ instead of Dart because i figured this way the main codebase stays untouched, and linux-specific code stays out of the way of the rest. Most of the code in `./linux` and `./example/linux` has been generated by flutter and left untouched. The only files written by hand are `./linux/video_compress_plugin.cc` and `./linux/video_compress_plugin_private.h`. I've also added a CMake directive in `./linux/CMakeLists.txt` to export a `compile_commands.json` file for development with clangd.
1 parent 69c0e3f commit 1cce991

18 files changed

+1210
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Compress videos, remove audio, manipulate thumbnails, and make your video compat
55
In addition, google chrome uses VP8/VP9, safari uses h264, and most of the time, it is necessary to encode the video in two formats, but not with this library.
66
All video files are encoded in an MP4 container with AAC audio that allows 100% compatibility with safari, mozila, chrome, android and iOS.
77

8-
Works on ANDROID, IOS and desktop (just MacOS for now).
8+
Works on ANDROID, IOS and desktop (just MacOS and Linux for now).
99

1010

1111

example/linux/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
flutter/ephemeral

example/linux/CMakeLists.txt

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Project-level configuration.
2+
cmake_minimum_required(VERSION 3.13)
3+
project(runner LANGUAGES CXX)
4+
5+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
6+
7+
# The name of the executable created for the application. Change this to change
8+
# the on-disk name of your application.
9+
set(BINARY_NAME "video_compress_example")
10+
# The unique GTK application identifier for this application. See:
11+
# https://wiki.gnome.org/HowDoI/ChooseApplicationID
12+
set(APPLICATION_ID "com.example.video_compress_example")
13+
14+
# Explicitly opt in to modern CMake behaviors to avoid warnings with recent
15+
# versions of CMake.
16+
cmake_policy(SET CMP0063 NEW)
17+
18+
# Load bundled libraries from the lib/ directory relative to the binary.
19+
set(CMAKE_INSTALL_RPATH "$ORIGIN/lib")
20+
21+
# Root filesystem for cross-building.
22+
if(FLUTTER_TARGET_PLATFORM_SYSROOT)
23+
set(CMAKE_SYSROOT ${FLUTTER_TARGET_PLATFORM_SYSROOT})
24+
set(CMAKE_FIND_ROOT_PATH ${CMAKE_SYSROOT})
25+
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
26+
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
27+
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
28+
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
29+
endif()
30+
31+
# Define build configuration options.
32+
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
33+
set(CMAKE_BUILD_TYPE "Debug" CACHE
34+
STRING "Flutter build mode" FORCE)
35+
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
36+
"Debug" "Profile" "Release")
37+
endif()
38+
39+
# Compilation settings that should be applied to most targets.
40+
#
41+
# Be cautious about adding new options here, as plugins use this function by
42+
# default. In most cases, you should add new options to specific targets instead
43+
# of modifying this function.
44+
function(APPLY_STANDARD_SETTINGS TARGET)
45+
target_compile_features(${TARGET} PUBLIC cxx_std_14)
46+
target_compile_options(${TARGET} PRIVATE -Wall -Werror)
47+
target_compile_options(${TARGET} PRIVATE "$<$<NOT:$<CONFIG:Debug>>:-O3>")
48+
target_compile_definitions(${TARGET} PRIVATE "$<$<NOT:$<CONFIG:Debug>>:NDEBUG>")
49+
endfunction()
50+
51+
# Flutter library and tool build rules.
52+
set(FLUTTER_MANAGED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/flutter")
53+
add_subdirectory(${FLUTTER_MANAGED_DIR})
54+
55+
# System-level dependencies.
56+
find_package(PkgConfig REQUIRED)
57+
pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0)
58+
59+
# Application build; see runner/CMakeLists.txt.
60+
add_subdirectory("runner")
61+
62+
# Run the Flutter tool portions of the build. This must not be removed.
63+
add_dependencies(${BINARY_NAME} flutter_assemble)
64+
65+
# Only the install-generated bundle's copy of the executable will launch
66+
# correctly, since the resources must in the right relative locations. To avoid
67+
# people trying to run the unbundled copy, put it in a subdirectory instead of
68+
# the default top-level location.
69+
set_target_properties(${BINARY_NAME}
70+
PROPERTIES
71+
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/intermediates_do_not_run"
72+
)
73+
74+
75+
# Generated plugin build rules, which manage building the plugins and adding
76+
# them to the application.
77+
include(flutter/generated_plugins.cmake)
78+
79+
80+
# === Installation ===
81+
# By default, "installing" just makes a relocatable bundle in the build
82+
# directory.
83+
set(BUILD_BUNDLE_DIR "${PROJECT_BINARY_DIR}/bundle")
84+
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
85+
set(CMAKE_INSTALL_PREFIX "${BUILD_BUNDLE_DIR}" CACHE PATH "..." FORCE)
86+
endif()
87+
88+
# Start with a clean build bundle directory every time.
89+
install(CODE "
90+
file(REMOVE_RECURSE \"${BUILD_BUNDLE_DIR}/\")
91+
" COMPONENT Runtime)
92+
93+
set(INSTALL_BUNDLE_DATA_DIR "${CMAKE_INSTALL_PREFIX}/data")
94+
set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib")
95+
96+
install(TARGETS ${BINARY_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}"
97+
COMPONENT Runtime)
98+
99+
install(FILES "${FLUTTER_ICU_DATA_FILE}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}"
100+
COMPONENT Runtime)
101+
102+
install(FILES "${FLUTTER_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
103+
COMPONENT Runtime)
104+
105+
foreach(bundled_library ${PLUGIN_BUNDLED_LIBRARIES})
106+
install(FILES "${bundled_library}"
107+
DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
108+
COMPONENT Runtime)
109+
endforeach(bundled_library)
110+
111+
# Copy the native assets provided by the build.dart from all packages.
112+
set(NATIVE_ASSETS_DIR "${PROJECT_BUILD_DIR}native_assets/linux/")
113+
install(DIRECTORY "${NATIVE_ASSETS_DIR}"
114+
DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
115+
COMPONENT Runtime)
116+
117+
# Fully re-copy the assets directory on each build to avoid having stale files
118+
# from a previous install.
119+
set(FLUTTER_ASSET_DIR_NAME "flutter_assets")
120+
install(CODE "
121+
file(REMOVE_RECURSE \"${INSTALL_BUNDLE_DATA_DIR}/${FLUTTER_ASSET_DIR_NAME}\")
122+
" COMPONENT Runtime)
123+
install(DIRECTORY "${PROJECT_BUILD_DIR}/${FLUTTER_ASSET_DIR_NAME}"
124+
DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime)
125+
126+
# Install the AOT library on non-Debug builds only.
127+
if(NOT CMAKE_BUILD_TYPE MATCHES "Debug")
128+
install(FILES "${AOT_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
129+
COMPONENT Runtime)
130+
endif()
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# This file controls Flutter-level build steps. It should not be edited.
2+
cmake_minimum_required(VERSION 3.10)
3+
4+
set(EPHEMERAL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ephemeral")
5+
6+
# Configuration provided via flutter tool.
7+
include(${EPHEMERAL_DIR}/generated_config.cmake)
8+
9+
# TODO: Move the rest of this into files in ephemeral. See
10+
# https://github.com/flutter/flutter/issues/57146.
11+
12+
# Serves the same purpose as list(TRANSFORM ... PREPEND ...),
13+
# which isn't available in 3.10.
14+
function(list_prepend LIST_NAME PREFIX)
15+
set(NEW_LIST "")
16+
foreach(element ${${LIST_NAME}})
17+
list(APPEND NEW_LIST "${PREFIX}${element}")
18+
endforeach(element)
19+
set(${LIST_NAME} "${NEW_LIST}" PARENT_SCOPE)
20+
endfunction()
21+
22+
# === Flutter Library ===
23+
# System-level dependencies.
24+
find_package(PkgConfig REQUIRED)
25+
pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0)
26+
pkg_check_modules(GLIB REQUIRED IMPORTED_TARGET glib-2.0)
27+
pkg_check_modules(GIO REQUIRED IMPORTED_TARGET gio-2.0)
28+
29+
set(FLUTTER_LIBRARY "${EPHEMERAL_DIR}/libflutter_linux_gtk.so")
30+
31+
# Published to parent scope for install step.
32+
set(FLUTTER_LIBRARY ${FLUTTER_LIBRARY} PARENT_SCOPE)
33+
set(FLUTTER_ICU_DATA_FILE "${EPHEMERAL_DIR}/icudtl.dat" PARENT_SCOPE)
34+
set(PROJECT_BUILD_DIR "${PROJECT_DIR}/build/" PARENT_SCOPE)
35+
set(AOT_LIBRARY "${PROJECT_DIR}/build/lib/libapp.so" PARENT_SCOPE)
36+
37+
list(APPEND FLUTTER_LIBRARY_HEADERS
38+
"fl_basic_message_channel.h"
39+
"fl_binary_codec.h"
40+
"fl_binary_messenger.h"
41+
"fl_dart_project.h"
42+
"fl_engine.h"
43+
"fl_json_message_codec.h"
44+
"fl_json_method_codec.h"
45+
"fl_message_codec.h"
46+
"fl_method_call.h"
47+
"fl_method_channel.h"
48+
"fl_method_codec.h"
49+
"fl_method_response.h"
50+
"fl_plugin_registrar.h"
51+
"fl_plugin_registry.h"
52+
"fl_standard_message_codec.h"
53+
"fl_standard_method_codec.h"
54+
"fl_string_codec.h"
55+
"fl_value.h"
56+
"fl_view.h"
57+
"flutter_linux.h"
58+
)
59+
list_prepend(FLUTTER_LIBRARY_HEADERS "${EPHEMERAL_DIR}/flutter_linux/")
60+
add_library(flutter INTERFACE)
61+
target_include_directories(flutter INTERFACE
62+
"${EPHEMERAL_DIR}"
63+
)
64+
target_link_libraries(flutter INTERFACE "${FLUTTER_LIBRARY}")
65+
target_link_libraries(flutter INTERFACE
66+
PkgConfig::GTK
67+
PkgConfig::GLIB
68+
PkgConfig::GIO
69+
)
70+
add_dependencies(flutter flutter_assemble)
71+
72+
# === Flutter tool backend ===
73+
# _phony_ is a non-existent file to force this command to run every time,
74+
# since currently there's no way to get a full input/output list from the
75+
# flutter tool.
76+
add_custom_command(
77+
OUTPUT ${FLUTTER_LIBRARY} ${FLUTTER_LIBRARY_HEADERS}
78+
${CMAKE_CURRENT_BINARY_DIR}/_phony_
79+
COMMAND ${CMAKE_COMMAND} -E env
80+
${FLUTTER_TOOL_ENVIRONMENT}
81+
"${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.sh"
82+
${FLUTTER_TARGET_PLATFORM} ${CMAKE_BUILD_TYPE}
83+
VERBATIM
84+
)
85+
add_custom_target(flutter_assemble DEPENDS
86+
"${FLUTTER_LIBRARY}"
87+
${FLUTTER_LIBRARY_HEADERS}
88+
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// Generated file. Do not edit.
3+
//
4+
5+
// clang-format off
6+
7+
#include "generated_plugin_registrant.h"
8+
9+
#include <file_selector_linux/file_selector_plugin.h>
10+
#include <video_compress/video_compress_plugin.h>
11+
12+
void fl_register_plugins(FlPluginRegistry* registry) {
13+
g_autoptr(FlPluginRegistrar) file_selector_linux_registrar =
14+
fl_plugin_registry_get_registrar_for_plugin(registry, "FileSelectorPlugin");
15+
file_selector_plugin_register_with_registrar(file_selector_linux_registrar);
16+
g_autoptr(FlPluginRegistrar) video_compress_registrar =
17+
fl_plugin_registry_get_registrar_for_plugin(registry, "VideoCompressPlugin");
18+
video_compress_plugin_register_with_registrar(video_compress_registrar);
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// Generated file. Do not edit.
3+
//
4+
5+
// clang-format off
6+
7+
#ifndef GENERATED_PLUGIN_REGISTRANT_
8+
#define GENERATED_PLUGIN_REGISTRANT_
9+
10+
#include <flutter_linux/flutter_linux.h>
11+
12+
// Registers Flutter plugins.
13+
void fl_register_plugins(FlPluginRegistry* registry);
14+
15+
#endif // GENERATED_PLUGIN_REGISTRANT_
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#
2+
# Generated file, do not edit.
3+
#
4+
5+
list(APPEND FLUTTER_PLUGIN_LIST
6+
file_selector_linux
7+
video_compress
8+
)
9+
10+
list(APPEND FLUTTER_FFI_PLUGIN_LIST
11+
)
12+
13+
set(PLUGIN_BUNDLED_LIBRARIES)
14+
15+
foreach(plugin ${FLUTTER_PLUGIN_LIST})
16+
add_subdirectory(flutter/ephemeral/.plugin_symlinks/${plugin}/linux plugins/${plugin})
17+
target_link_libraries(${BINARY_NAME} PRIVATE ${plugin}_plugin)
18+
list(APPEND PLUGIN_BUNDLED_LIBRARIES $<TARGET_FILE:${plugin}_plugin>)
19+
list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${plugin}_bundled_libraries})
20+
endforeach(plugin)
21+
22+
foreach(ffi_plugin ${FLUTTER_FFI_PLUGIN_LIST})
23+
add_subdirectory(flutter/ephemeral/.plugin_symlinks/${ffi_plugin}/linux plugins/${ffi_plugin})
24+
list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${ffi_plugin}_bundled_libraries})
25+
endforeach(ffi_plugin)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
project(runner LANGUAGES CXX)
3+
4+
# Define the application target. To change its name, change BINARY_NAME in the
5+
# top-level CMakeLists.txt, not the value here, or `flutter run` will no longer
6+
# work.
7+
#
8+
# Any new source files that you add to the application should be added here.
9+
add_executable(${BINARY_NAME}
10+
"main.cc"
11+
"my_application.cc"
12+
"${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc"
13+
)
14+
15+
# Apply the standard set of build settings. This can be removed for applications
16+
# that need different build settings.
17+
apply_standard_settings(${BINARY_NAME})
18+
19+
# Add preprocessor definitions for the application ID.
20+
add_definitions(-DAPPLICATION_ID="${APPLICATION_ID}")
21+
22+
# Add dependency libraries. Add any application-specific dependencies here.
23+
target_link_libraries(${BINARY_NAME} PRIVATE flutter)
24+
target_link_libraries(${BINARY_NAME} PRIVATE PkgConfig::GTK)
25+
26+
target_include_directories(${BINARY_NAME} PRIVATE "${CMAKE_SOURCE_DIR}")

example/linux/runner/main.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "my_application.h"
2+
3+
int main(int argc, char** argv) {
4+
g_autoptr(MyApplication) app = my_application_new();
5+
return g_application_run(G_APPLICATION(app), argc, argv);
6+
}

0 commit comments

Comments
 (0)