support flutter gstreamer player for iOS

This commit is contained in:
hpdragon
2022-12-09 01:02:56 +07:00
parent 9fd8c9f2da
commit af6f0e132c
56 changed files with 2839 additions and 81 deletions
+38
View File
@@ -0,0 +1,38 @@
.idea/
.vagrant/
.sconsign.dblite
.svn/
.DS_Store
*.swp
profile
DerivedData/
build/
GeneratedPluginRegistrant.h
GeneratedPluginRegistrant.m
.generated/
*.pbxuser
*.mode1v3
*.mode2v3
*.perspectivev3
!default.pbxuser
!default.mode1v3
!default.mode2v3
!default.perspectivev3
xcuserdata
*.moved-aside
*.pyc
*sync/
Icon?
.tags*
/Flutter/Generated.xcconfig
/Flutter/ephemeral/
/Flutter/flutter_export_environment.sh
View File
+7
View File
@@ -0,0 +1,7 @@
#ifndef __BRIDGINGHEADER_H__
#define __BRIDGINGHEADER_H__
#import "GStreamerBackend.h"
#import "gst_ios_init.h"
#endif
@@ -0,0 +1,4 @@
#import <Flutter/Flutter.h>
@interface FlutterGstreamerPlayerPlugin : NSObject<FlutterPlugin>
@end
@@ -0,0 +1,15 @@
#import "FlutterGstreamerPlayerPlugin.h"
#if __has_include(<flutter_gstreamer_player/flutter_gstreamer_player-Swift.h>)
#import <flutter_gstreamer_player/flutter_gstreamer_player-Swift.h>
#else
// Support project import fallback if the generated compatibility header
// is not copied when this plugin is created as a library.
// https://forums.swift.org/t/swift-static-libraries-dont-copy-generated-objective-c-header/19816
#import "flutter_gstreamer_player-Swift.h"
#endif
@implementation FlutterGstreamerPlayerPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
[SwiftFlutterGstreamerPlayerPlugin registerWithRegistrar:registrar];
}
@end
+13
View File
@@ -0,0 +1,13 @@
#ifndef __GSTREAMERBACKEND_H__
#define __GSTREAMERBACKEND_H__
#import <Foundation/Foundation.h>
@interface GStreamerBackend : NSObject
-(id) init:(NSString*) pipeline videoView:(UIView*) video_view;
@end
#endif
+185
View File
@@ -0,0 +1,185 @@
#import "GStreamerBackend.h"
#include <gst/gst.h>
#include <gst/video/video.h>
GST_DEBUG_CATEGORY_STATIC (debug_category);
#define GST_CAT_DEFAULT debug_category
@interface GStreamerBackend()
-(void)setUIMessage:(gchar*) message;
-(void)app_function:(gchar*) pipeline_string;
-(void)check_initialization_complete;
@end
@implementation GStreamerBackend {
/* id ui_delegate; /1* Class that we use to interact with the user interface *1/ */
GstElement *pipeline; /* The running pipeline */
GstElement *video_sink;/* The video sink element which receives XOverlay commands */
GMainContext *context; /* GLib context used to run the main loop */
GMainLoop *main_loop; /* GLib main loop */
gboolean initialized; /* To avoid informing the UI multiple times about the initialization */
UIView *ui_video_view; /* UIView that holds the video */
}
-(id) init:(NSString*) pipeline videoView:(UIView*) video_view
{
if (self = [super init])
{
/* self->ui_delegate = uiDelegate; */
self->ui_video_view = video_view;
GST_DEBUG_CATEGORY_INIT (debug_category, "tutorial-3", 0, "iOS tutorial 3");
gst_debug_set_threshold_for_name("tutorial-3", GST_LEVEL_DEBUG);
/* Start the bus monitoring task */
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self app_function: (gchar*)[pipeline UTF8String]];
});
}
return self;
}
-(void) dealloc
{
if (pipeline) {
NSLog(@"Setting the pipeline to NULL");
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(pipeline);
pipeline = NULL;
}
}
/*
* Private methods
*/
/* Change the message on the UI through the UI delegate */
-(void)setUIMessage:(gchar*) message
{
NSString *string = [NSString stringWithUTF8String:message];
/* if(ui_delegate && [ui_delegate respondsToSelector:@selector(gstreamerSetUIMessage:)]) */
/* { */
/* [ui_delegate gstreamerSetUIMessage:string]; */
/* } */
}
/* Retrieve errors from the bus and show them on the UI */
static void error_cb (GstBus *bus, GstMessage *msg, GStreamerBackend *self)
{
GError *err;
gchar *debug_info;
gchar *message_string;
gst_message_parse_error (msg, &err, &debug_info);
message_string = g_strdup_printf ("Error received from element %s: %s", GST_OBJECT_NAME (msg->src), err->message);
g_clear_error (&err);
g_free (debug_info);
[self setUIMessage:message_string];
g_free (message_string);
gst_element_set_state (self->pipeline, GST_STATE_NULL);
}
/* Notify UI about pipeline state changes */
static void state_changed_cb (GstBus *bus, GstMessage *msg, GStreamerBackend *self)
{
GstState old_state, new_state, pending_state;
gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);
/* Only pay attention to messages coming from the pipeline, not its children */
if (GST_MESSAGE_SRC (msg) == GST_OBJECT (self->pipeline)) {
gchar *message = g_strdup_printf("State changed to %s", gst_element_state_get_name(new_state));
[self setUIMessage:message];
g_free (message);
}
}
/* Check if all conditions are met to report GStreamer as initialized.
* These conditions will change depending on the application */
-(void) check_initialization_complete
{
if (!initialized && main_loop) {
NSLog (@"Initialization complete, notifying application.");
/* if (ui_delegate && [ui_delegate respondsToSelector:@selector(gstreamerInitialized)]) */
/* { */
/* [ui_delegate gstreamerInitialized]; */
/* } */
initialized = TRUE;
NSLog(@"Initialization complete, notifying application.");
if(gst_element_set_state(pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
[self setUIMessage:"Failed to set pipeline to playing"];
}
}
}
/* Main method for the bus monitoring code */
-(void) app_function:(gchar*) pipeline_string
{
GstBus *bus;
GSource *bus_source;
GError *error = NULL;
NSLog (@"Creating pipeline");
/* Create our own GLib Main Context and make it the default one */
context = g_main_context_new ();
g_main_context_push_thread_default(context);
/* Build pipeline */
/* pipeline = gst_parse_launch("videotestsrc ! warptv ! videoconvert ! autovideosink", &error); */
/* pipeline = gst_parse_launch("rtspsrc location= \ */
/* rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4 latency=0 ! \ */
/* decodebin ! \ */
/* autovideosink", &error); */
NSLog(@"Pipeline 1: %s", pipeline_string);
NSLog(@"Pipeline 2: %s", pipeline_string);
pipeline = gst_parse_launch(pipeline_string, &error);
if (error) {
NSLog (@"Unable to build pipeline: %s", error->message);
gchar *message = g_strdup_printf("Unable to build pipeline: %s", error->message);
g_clear_error (&error);
[self setUIMessage:message];
g_free (message);
return;
}
/* Set the pipeline to READY, so it can already accept a window handle */
gst_element_set_state(pipeline, GST_STATE_READY);
video_sink = gst_bin_get_by_interface(GST_BIN(pipeline), GST_TYPE_VIDEO_OVERLAY);
if (!video_sink) {
NSLog (@"Could not retrieve video sink");
return;
}
gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(video_sink), (guintptr) (id) ui_video_view);
/* Instruct the bus to emit signals for each received message, and connect to the interesting signals */
bus = gst_element_get_bus (pipeline);
bus_source = gst_bus_create_watch (bus);
g_source_set_callback (bus_source, (GSourceFunc) gst_bus_async_signal_func, NULL, NULL);
g_source_attach (bus_source, context);
g_source_unref (bus_source);
g_signal_connect (G_OBJECT (bus), "message::error", (GCallback)error_cb, (__bridge void *)self);
g_signal_connect (G_OBJECT (bus), "message::state-changed", (GCallback)state_changed_cb, (__bridge void *)self);
gst_object_unref (bus);
/* Create a GLib Main Loop and set it to run */
NSLog (@"Entering main loop...");
main_loop = g_main_loop_new (context, FALSE);
[self check_initialization_complete];
g_main_loop_run (main_loop);
NSLog (@"Exited main loop");
g_main_loop_unref (main_loop);
main_loop = NULL;
/* Free resources */
g_main_context_pop_thread_default(context);
g_main_context_unref (context);
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
return;
}
@end
@@ -0,0 +1,148 @@
import Flutter
import UIKit
class FLNativeViewFactory: NSObject, FlutterPlatformViewFactory {
private var messenger: FlutterBinaryMessenger
private var pipeline: String
init(messenger: FlutterBinaryMessenger, pipeline: String) {
self.messenger = messenger
self.pipeline = pipeline
super.init()
}
func create(
withFrame frame: CGRect,
viewIdentifier viewId: Int64,
arguments args: Any?
) -> FlutterPlatformView {
return FLNativeView(
frame: frame,
viewIdentifier: viewId,
arguments: args,
binaryMessenger: messenger,
pipeline: self.pipeline)
}
}
class FLNativeView: NSObject, FlutterPlatformView {
private var _view: UIView
//private var _pipeline: String = ""
private var _gStreamerBackend: GStreamerBackend
init(
frame: CGRect,
viewIdentifier viewId: Int64,
arguments args: Any?,
binaryMessenger messenger: FlutterBinaryMessenger?,
pipeline pipeline: String
) {
_view = UIView()
/*
print("[FLNativeView] viewId: " + String(viewId))
switch (viewId) {
case 0:
//_pipeline = "videotestsrc pattern=smpte ! warptv ! videoconvert ! autovideosink"
_pipeline = "rtspsrc location=rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4 retry=60000 ! decodebin ! autovideosink"
//_pipeline = "rtspsrc location=rtsp://192.168.55.1:9000/sony latency=0 ! decodebin ! autovideosink"
//_pipeline = "rtspsrc location=rtsp://192.168.0.103:9000/sony latency=0 ! decodebin ! autovideosink"
break
case 1:
//_pipeline = "videotestsrc pattern=circular ! warptv ! videoconvert ! autovideosink"
_pipeline = "rtspsrc location=rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4 retry=60000 ! decodebin ! autovideosink"
//_pipeline = "rtspsrc location=rtsp://192.168.55.1:9000/sony latency=0 ! decodebin ! autovideosink"
//_pipeline = "rtspsrc location=rtsp://192.168.0.103:9000/sony latency=0 ! decodebin ! autovideosink"
break
case 2:
//_pipeline = "videotestsrc pattern=checkers-8 ! warptv ! videoconvert ! autovideosink"
_pipeline = "rtspsrc location=rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4 retry=60000 ! decodebin ! autovideosink"
//_pipeline = "rtspsrc location=rtsp://192.168.55.1:9000/sony latency=0 ! decodebin ! autovideosink"
//_pipeline = "rtspsrc location=rtsp://192.168.0.103:9000/sony latency=0 ! decodebin ! autovideosink"
break
case 3:
//_pipeline = "videotestsrc pattern=smpte100 ! warptv ! videoconvert ! autovideosink"
_pipeline = "rtspsrc location=rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4 retry=60000 ! decodebin ! autovideosink"
//_pipeline = "rtspsrc location=rtsp://192.168.55.1:9000/sony latency=0 ! decodebin ! autovideosink"
//_pipeline = "rtspsrc location=rtsp://192.168.0.103:9000/sony latency=0 ! decodebin ! autovideosink"
break
default:
_pipeline = "rtspsrc location=rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4 latency=0 ! decodebin ! autovideosink"
//_pipeline = "rtspsrc location=rtsp://192.168.55.1:9000/sony latency=0 ! decodebin ! autovideosink"
//_pipeline = "rtspsrc location=rtsp://192.168.0.103:9000/sony latency=0 ! decodebin ! autovideosink"
break
}
*/
_gStreamerBackend = GStreamerBackend(
pipeline,
videoView: _view)
super.init()
// iOS views can be created here
//createNativeView(view: _view)
}
func view() -> UIView {
return _view
}
func createNativeView(view _view: UIView){
_view.backgroundColor = UIColor.blue
let nativeLabel = UILabel()
nativeLabel.text = "Native text from iOS: " + String("")
nativeLabel.textColor = UIColor.white
nativeLabel.textAlignment = .center
nativeLabel.frame = CGRect(x: 0, y: 0, width: 180, height: 48.0)
_view.addSubview(nativeLabel)
}
}
public class SwiftFlutterGstreamerPlayerPlugin: NSObject, FlutterPlugin {
static var registrar: FlutterPluginRegistrar? = nil;
/* static var factory: FLNativeViewFactory? = nil; */
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "flutter_gstreamer_player", binaryMessenger: registrar.messenger())
let instance = SwiftFlutterGstreamerPlayerPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
gst_ios_init()
SwiftFlutterGstreamerPlayerPlugin.registrar = registrar
/* SwiftFlutterGstreamerPlayerPlugin.factory = factory */
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch (call.method) {
case "getPlatformVersion":
result("iOS " + UIDevice.current.systemVersion)
break
case "PlayerRegisterTexture":
guard let args = call.arguments as? [String : Any] else {
result(" arguments error.... ")
return
}
let pipeline = args["pipeline"] as! String;
let playerId = args["playerId"] as! Int64;
/* guard let factory = SwiftFlutterGstreamerPlayerPlugin.factory as? FLNativeViewFactory else { */
/* print("Internal plugin error: factory does not initialized") */
/* return */
/* } */
guard let registrar = SwiftFlutterGstreamerPlayerPlugin.registrar as? FlutterPluginRegistrar else {
print("Internal plugin error: registrar does not initialized")
return
}
var factory = FLNativeViewFactory(messenger: registrar.messenger(), pipeline: pipeline)
registrar.register(factory, withId: String(playerId))
result(playerId)
break
default:
result(FlutterMethodNotImplemented)
break
}
}
}
+39
View File
@@ -0,0 +1,39 @@
#ifndef __GST_IOS_INIT_H__
#define __GST_IOS_INIT_H__
// #include <gst/gst.h>
// G_BEGIN_DECLS
#define GST_G_IO_MODULE_DECLARE(name) \
extern void G_PASTE(g_io_, G_PASTE(name, _load)) (gpointer module)
#define GST_G_IO_MODULE_LOAD(name) \
G_PASTE(g_io_, G_PASTE(name, _load)) (NULL)
/* Uncomment each line to enable the plugin categories that your application needs.
* You can also enable individual plugins. See gst_ios_init.c to see their names
*/
#define GST_IOS_PLUGINS_CORE
#define GST_IOS_PLUGINS_CODECS
#define GST_IOS_PLUGINS_ENCODING
#define GST_IOS_PLUGINS_NET
#define GST_IOS_PLUGINS_PLAYBACK
#define GST_IOS_PLUGINS_VIS
#define GST_IOS_PLUGINS_SYS
#define GST_IOS_PLUGINS_EFFECTS
#define GST_IOS_PLUGINS_CAPTURE
#define GST_IOS_PLUGINS_CODECS_GPL
#define GST_IOS_PLUGINS_CODECS_RESTRICTED
#define GST_IOS_PLUGINS_NET_RESTRICTED
#define GST_IOS_PLUGINS_GES
//#define GST_IOS_GIO_MODULE_GNUTLS
void gst_ios_init (void);
// G_END_DECLS
#endif
File diff suppressed because it is too large Load Diff
+37
View File
@@ -0,0 +1,37 @@
#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html.
# Run `pod lib lint flutter_gstreamer_player.podspec` to validate before publishing.
#
Pod::Spec.new do |s|
s.name = 'flutter_gstreamer_player'
s.version = '0.0.1'
s.summary = 'A new Flutter plugin project.'
s.description = <<-DESC
A new Flutter plugin project.
DESC
s.homepage = 'http://example.com'
s.license = { :file => '../LICENSE' }
s.author = { 'Your Company' => '[email protected]' }
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.dependency 'Flutter'
s.platform = :ios, '9.0'
# Flutter.framework does not contain a i386 slice.
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' }
s.swift_version = '5.0'
s.frameworks = 'CoreFoundation', 'Foundation', 'AVFoundation', 'CoreMedia', 'CoreVideo', 'CoreAudio', 'AudioToolbox', 'OpenGLES', 'AssetsLibrary', 'QuartzCore', 'AssetsLibrary', 'VideoToolBox', 'Metal', 'IOSurface'
s.vendored_frameworks = 'GStreamer.framework'
s.xcconfig = {
'HEADER_SEARCH_PATHS' => [
'"$(HOME)/Library/Developer/GStreamer/iPhone.sdk/GStreamer.framework/Headers"'
],
'FRAMEWORK_SEARCH_PATHS' => [
'"$(HOME)/Library/Developer/GStreamer/iPhone.sdk"',
'"$(SYSTEM_APPS_DIR)/Xcode.app/Contents/Developer/Library/Frameworks"'
],
'CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES' => 'YES'
}
s.library = 'iconv', 'resolv', 'c++', ''
end