Initial commit
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
set(PROJECT_NAME "flutter_gstreamer_player")
|
||||
project(${PROJECT_NAME} LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
add_compile_options(-fPIC)
|
||||
|
||||
# This value is used when generating builds using this plugin, so it must
|
||||
# not be changed
|
||||
set(PLUGIN_NAME "flutter_gstreamer_player_plugin")
|
||||
|
||||
# GStreamer libraries
|
||||
find_package(PkgConfig)
|
||||
pkg_search_module(GLIB REQUIRED glib-2.0)
|
||||
pkg_check_modules(GSTREAMER REQUIRED gstreamer-1.0)
|
||||
pkg_check_modules(GST_APP REQUIRED gstreamer-app-1.0)
|
||||
pkg_check_modules(GST_VIDEO REQUIRED gstreamer-video-1.0)
|
||||
include_directories(${GSTREAMER_INCLUDE_DIRS} ${GLIB2_INCLUDE_DIRS})
|
||||
# Custom GStreamer player
|
||||
add_library(GST_PLAYER STATIC
|
||||
"../gst_player/gst_player.cc"
|
||||
)
|
||||
include_directories(GST_PLAYER STATIC
|
||||
"../gst_player"
|
||||
)
|
||||
|
||||
add_library(${PLUGIN_NAME} SHARED
|
||||
"flutter_gstreamer_player_plugin.cc"
|
||||
)
|
||||
apply_standard_settings(${PLUGIN_NAME})
|
||||
set_target_properties(${PLUGIN_NAME} PROPERTIES
|
||||
CXX_VISIBILITY_PRESET hidden)
|
||||
target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL)
|
||||
target_include_directories(${PLUGIN_NAME} INTERFACE
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/include")
|
||||
target_link_libraries(${PLUGIN_NAME} PRIVATE flutter)
|
||||
target_link_libraries(${PLUGIN_NAME} PRIVATE PkgConfig::GTK)
|
||||
target_link_libraries(${PLUGIN_NAME} PRIVATE ${GST_APP_LIBRARIES} ${GST_VIDEO_LIBRARIES})
|
||||
target_link_libraries(${PLUGIN_NAME} PRIVATE GST_PLAYER)
|
||||
|
||||
# List of absolute paths to libraries that should be bundled with the plugin
|
||||
set(flutter_gstreamer_player_bundled_libraries
|
||||
""
|
||||
PARENT_SCOPE
|
||||
)
|
||||
@@ -0,0 +1,124 @@
|
||||
#include "gst_player.h"
|
||||
|
||||
#include "include/flutter_gstreamer_player/flutter_gstreamer_player_plugin.h"
|
||||
#include "include/flutter_gstreamer_player/flutter_gstreamer_player_video_outlet.h"
|
||||
|
||||
#include <flutter_linux/flutter_linux.h>
|
||||
#include <gtk/gtk.h>
|
||||
#include <sys/utsname.h>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#define FLUTTER_GSTREAMER_PLAYER_PLUGIN(obj) \
|
||||
(G_TYPE_CHECK_INSTANCE_CAST((obj), flutter_gstreamer_player_plugin_get_type(), \
|
||||
FlutterGstreamerPlayerPlugin))
|
||||
|
||||
struct _FlutterGstreamerPlayerPlugin {
|
||||
GObject parent_instance;
|
||||
FlMethodChannel* method_channel;
|
||||
FlTextureRegistrar* texture_registrar;
|
||||
};
|
||||
|
||||
std::unordered_map<int32_t, VideoOutlet*> g_video_outlets;
|
||||
|
||||
G_DEFINE_TYPE(FlutterGstreamerPlayerPlugin, flutter_gstreamer_player_plugin, g_object_get_type())
|
||||
|
||||
// Called when a method call is received from Flutter.
|
||||
static void flutter_gstreamer_player_plugin_handle_method_call(
|
||||
FlutterGstreamerPlayerPlugin* self,
|
||||
FlMethodCall* method_call) {
|
||||
g_autoptr(FlMethodResponse) response = nullptr;
|
||||
|
||||
const gchar* method = fl_method_call_get_name(method_call);
|
||||
|
||||
// TODO properly handle these method calls
|
||||
if (strcmp(method, "getPlatformVersion") == 0) {
|
||||
struct utsname uname_data = {};
|
||||
uname(&uname_data);
|
||||
g_autofree gchar *version = g_strdup_printf("Linux %s", uname_data.version);
|
||||
g_autoptr(FlValue) result = fl_value_new_string(version);
|
||||
response = FL_METHOD_RESPONSE(fl_method_success_response_new(result));
|
||||
} else if (strcmp(method, "PlayerRegisterTexture") == 0) {
|
||||
auto arguments = fl_method_call_get_args(method_call);
|
||||
auto pipeline =
|
||||
fl_value_get_string(fl_value_lookup_string(arguments, "pipeline"));
|
||||
int32_t player_id =
|
||||
fl_value_get_int(fl_value_lookup_string(arguments, "playerId"));
|
||||
auto [it, added] = g_video_outlets.try_emplace(player_id, nullptr);
|
||||
|
||||
GstPlayer* gstPlayer = g_players->Get(player_id);
|
||||
|
||||
if (added) {
|
||||
it->second = video_outlet_new();
|
||||
|
||||
FL_PIXEL_BUFFER_TEXTURE_GET_CLASS(it->second)->copy_pixels =
|
||||
video_outlet_copy_pixels;
|
||||
fl_texture_registrar_register_texture(self->texture_registrar,
|
||||
FL_TEXTURE(it->second));
|
||||
auto video_outlet_private = (VideoOutletPrivate*) video_outlet_get_instance_private(it->second);
|
||||
video_outlet_private->texture_id = reinterpret_cast<int64_t>(FL_TEXTURE(it->second));
|
||||
|
||||
gstPlayer->onVideo([texture_registrar = self->texture_registrar,
|
||||
video_outlet_ptr = it->second,
|
||||
video_outlet_private = video_outlet_private]
|
||||
(uint8_t* frame, int32_t width, int32_t height) -> void {
|
||||
video_outlet_private->buffer = frame;
|
||||
video_outlet_private->video_width = width;
|
||||
video_outlet_private->video_height = height;
|
||||
fl_texture_registrar_mark_texture_frame_available(
|
||||
texture_registrar,
|
||||
FL_TEXTURE(video_outlet_ptr));
|
||||
});
|
||||
}
|
||||
|
||||
gstPlayer->play(pipeline);
|
||||
|
||||
response =
|
||||
FL_METHOD_RESPONSE(fl_method_success_response_new(fl_value_new_int(
|
||||
((VideoOutletPrivate*) video_outlet_get_instance_private(it->second))->texture_id
|
||||
)));
|
||||
} else if (strcmp(method, "dispose") == 0) {
|
||||
g_autoptr(FlValue) result = fl_value_new_bool(true);
|
||||
response = FL_METHOD_RESPONSE(fl_method_success_response_new(result));
|
||||
} else {
|
||||
response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
|
||||
}
|
||||
|
||||
fl_method_call_respond(method_call, response, nullptr);
|
||||
}
|
||||
|
||||
static void flutter_gstreamer_player_plugin_dispose(GObject* object) {
|
||||
G_OBJECT_CLASS(flutter_gstreamer_player_plugin_parent_class)->dispose(object);
|
||||
}
|
||||
|
||||
static void flutter_gstreamer_player_plugin_class_init(FlutterGstreamerPlayerPluginClass* klass) {
|
||||
G_OBJECT_CLASS(klass)->dispose = flutter_gstreamer_player_plugin_dispose;
|
||||
}
|
||||
|
||||
static void flutter_gstreamer_player_plugin_init(FlutterGstreamerPlayerPlugin* self) {}
|
||||
|
||||
static void method_call_cb(FlMethodChannel* channel, FlMethodCall* method_call,
|
||||
gpointer user_data) {
|
||||
FlutterGstreamerPlayerPlugin* plugin = FLUTTER_GSTREAMER_PLAYER_PLUGIN(user_data);
|
||||
flutter_gstreamer_player_plugin_handle_method_call(plugin, method_call);
|
||||
}
|
||||
|
||||
void flutter_gstreamer_player_plugin_register_with_registrar(FlPluginRegistrar* registrar) {
|
||||
FlutterGstreamerPlayerPlugin* plugin = FLUTTER_GSTREAMER_PLAYER_PLUGIN(
|
||||
g_object_new(flutter_gstreamer_player_plugin_get_type(), nullptr));
|
||||
|
||||
g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
|
||||
g_autoptr(FlMethodChannel) channel =
|
||||
fl_method_channel_new(fl_plugin_registrar_get_messenger(registrar),
|
||||
"flutter_gstreamer_player",
|
||||
FL_METHOD_CODEC(codec));
|
||||
|
||||
plugin->texture_registrar =
|
||||
fl_plugin_registrar_get_texture_registrar(registrar);
|
||||
|
||||
fl_method_channel_set_method_call_handler(channel, method_call_cb,
|
||||
g_object_ref(plugin),
|
||||
g_object_unref);
|
||||
|
||||
g_object_unref(plugin);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef FLUTTER_PLUGIN_FLUTTER_GSTREAMER_PLAYER_PLUGIN_H_
|
||||
#define FLUTTER_PLUGIN_FLUTTER_GSTREAMER_PLAYER_PLUGIN_H_
|
||||
|
||||
#include <flutter_linux/flutter_linux.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#ifdef FLUTTER_PLUGIN_IMPL
|
||||
#define FLUTTER_PLUGIN_EXPORT __attribute__((visibility("default")))
|
||||
#else
|
||||
#define FLUTTER_PLUGIN_EXPORT
|
||||
#endif
|
||||
|
||||
typedef struct _FlutterGstreamerPlayerPlugin FlutterGstreamerPlayerPlugin;
|
||||
typedef struct {
|
||||
GObjectClass parent_class;
|
||||
} FlutterGstreamerPlayerPluginClass;
|
||||
|
||||
FLUTTER_PLUGIN_EXPORT GType flutter_gstreamer_player_plugin_get_type();
|
||||
|
||||
FLUTTER_PLUGIN_EXPORT void flutter_gstreamer_player_plugin_register_with_registrar(
|
||||
FlPluginRegistrar* registrar);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif // FLUTTER_PLUGIN_FLUTTER_GSTREAMER_PLAYER_PLUGIN_H_
|
||||
@@ -0,0 +1,43 @@
|
||||
#ifndef VIDEO_OUTLET_H_
|
||||
#define VIDEO_OUTLET_H_
|
||||
|
||||
#include <flutter_linux/flutter_linux.h>
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
struct _VideoOutletClass {
|
||||
FlPixelBufferTextureClass parent_class;
|
||||
};
|
||||
|
||||
struct VideoOutletPrivate {
|
||||
int64_t texture_id = 0;
|
||||
uint8_t* buffer = nullptr;
|
||||
int32_t video_width = 0;
|
||||
int32_t video_height = 0;
|
||||
};
|
||||
|
||||
G_DECLARE_DERIVABLE_TYPE(VideoOutlet, video_outlet, MY_OPENGL, VIDEO_OUTLET,
|
||||
FlPixelBufferTexture)
|
||||
|
||||
G_DEFINE_TYPE_WITH_CODE(VideoOutlet, video_outlet, fl_pixel_buffer_texture_get_type(), G_ADD_PRIVATE(VideoOutlet))
|
||||
|
||||
static VideoOutlet* video_outlet_new() {
|
||||
return MY_OPENGL_VIDEO_OUTLET(g_object_new(video_outlet_get_type(), nullptr));
|
||||
}
|
||||
|
||||
static gboolean video_outlet_copy_pixels(
|
||||
FlPixelBufferTexture* texture, const uint8_t** out_buffer, uint32_t* width,
|
||||
uint32_t* height, GError** error) {
|
||||
auto video_outlet_private = (VideoOutletPrivate*) video_outlet_get_instance_private(MY_OPENGL_VIDEO_OUTLET(texture));
|
||||
*out_buffer = video_outlet_private->buffer;
|
||||
*width = video_outlet_private->video_width;
|
||||
*height = video_outlet_private->video_height;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void video_outlet_class_init(VideoOutletClass* klass) {
|
||||
FL_PIXEL_BUFFER_TEXTURE_CLASS(klass)->copy_pixels = video_outlet_copy_pixels;
|
||||
}
|
||||
|
||||
static void video_outlet_init(VideoOutlet* self) {}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user